如何绘制 ggplot 树状图?
How can I plotly a ggplot treemap?
我希望在地图上获得这种渐变颜色:
ramp <- colorRamp(c("royalblue4", "white"))
ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)
而且,更重要的是,我正在寻找向图表添加绘图特征(特别是悬停文本输出)。这是我的数据:
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)
使用树图跟踪可以显示分层数据集。
因此,评论 plot_ly(dtd7, ids = ~topic, values = ~n, parents = ~topic, type = 'treemap')
中的代码片段存在的问题是,您将相同的数据分配给 ids
和 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
我希望在地图上获得这种渐变颜色:
ramp <- colorRamp(c("royalblue4", "white"))
ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)
而且,更重要的是,我正在寻找向图表添加绘图特征(特别是悬停文本输出)。这是我的数据:
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)
使用树图跟踪可以显示分层数据集。
因此,评论 plot_ly(dtd7, ids = ~topic, values = ~n, parents = ~topic, type = 'treemap')
中的代码片段存在的问题是,您将相同的数据分配给 ids
和 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