我正在使用 Plotly 创建树形图。下面你可以看到我的数据和我的代码
library(plotly)
df1<-structure(list(
item=c("item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10",
"item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20", "item21","item22"),
manuf = c("AMC", "Cadillac", "Camaro", "Chrysler",
"Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda",
"Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac",
"Porsche", "Toyota", "Valiant", "Volvo"), count = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L,
2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl",
"data.frame"))
df1$test<-c("test")
treemap<-plot_ly(data = df1,
type= "treemap",
values= ~count,
labels= ~item,
parents= ~test,
domain= list(column=0),
name = " ",
textinfo="label+value+percent parent") %>%
layout(title="",
annotations =
list(x = 0, y = -0.1,
title = "",
text = " ",
showarrow = F,
xref='paper',
yref='paper'))
treemap
到目前为止,一切都很好。但是,当我将鼠标悬停在树形图上时,我看不到汽车的名称。例如,项目 17 是一辆 Mercedes (Merc),但当我将鼠标悬停在它上面时看不到它。谁能帮我解决这个问题并在我将鼠标悬停在汽车上时显示汽车的名称
正如@Kat 所指出的,您可以使用
hovertext = df1$manuf
,如下所示:
该代码适用于plotly v4.10.2
library(plotly)
df1<-structure(list(
item=c("item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10",
"item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20", "item21","item22"),
manuf = c("AMC", "Cadillac", "Camaro", "Chrysler",
"Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda",
"Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac",
"Porsche", "Toyota", "Valiant", "Volvo"), count = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L,
2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl",
"data.frame"))
df1$test<-c("test")
treemap<-plot_ly(data = df1,
type= "treemap",
values= ~count,
labels= ~item,
parents= ~test,
domain= list(column=0),
name = " ",
hovertext = ~manuf, ## This allows you to add any text to your plots
textinfo="label+value+percent parent") %>%
layout(title="",
annotations =
list(x = 0, y = -0.1,
title = "",
text = " ",
showarrow = F,
xref='paper',
yref='paper'))
treemap