如何绘制 ggplot 树图?

编程入门 行业动态 更新时间:2024-10-10 14:30:13
本文介绍了如何绘制 ggplot 树图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望在地图上获得这种渐变颜色:

I am looking to get this gradient colors on the map:

ramp <- colorRamp(c("royalblue4", "white")) ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)

而且,更重要的是,我希望在图表中添加情节特征(特别是悬停文本输出).这是我的数据:

But also, and more important, I am looking to add plotly charactheristics to the graph (specially hovering text output). This is my data:

structure(list(V1 = structure(c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L, 5L, 10L, 13L, 11L, 12L), .Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango", "Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"), class = "factor"), V2 = structure(c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L, 7L, 8L, 1L), .Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"), class = "factor")), class = "data.frame", row.names = c(NA, -13L))

这是我尝试过的:

library(ggplot2) library(plotly) ramp <- colorRamp(c("royalblue4", "white")) ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255) g <- ggplot(dtd7, aes(area = n, fill = topic, label = as.character(topic))) + geom_treemap()+ geom_treemap_text(fontface = "italic", colour = "white", place = "centre") + theme(legend.position = "none") ggplotly(p)

推荐答案

使用树形图跟踪可以显示分层数据集.

Using treemap traces you can display hierarchical datasets.

因此,您的代码片段来自评论 plot_ly(dtd7, ids = ~topic, values = ~n, parents = ~topic, type = 'treemap') 的问题是,你是将相同的数据分配给 ids 和 parents.

Accordingly the problem with your code snippet from the comments plot_ly(dtd7, ids = ~topic, values = ~n, parents = ~topic, type = 'treemap') is, that you are assigning the same data to ids and parents.

请检查以下内容:

library(plotly) dtd7 <- structure( list( topic = structure( c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L, 5L, 10L, 13L, 11L, 12L), .Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango","Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"), class = "factor" ), n = structure( c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L, 7L, 8L, 1L), .Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"), class = "factor" ) ), class = "data.frame", row.names = c(NA,-13L) ) p <- plot_ly( dtd7, labels = ~ topic, parents = NA, values = ~ n, type = 'treemap', hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>" ) p

更多推荐

如何绘制 ggplot 树图?

本文发布于:2023-07-17 18:21:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1136027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ggplot   树图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!