我一直在使用自定义悬停文本(https://plot.ly/r/text-and-annotations/)上的示例来更改我正在处理的条形图中的悬停文本。如果我的条形图只包含一个堆叠的条形图,则不会显示新的悬停文本。示例(在缺少hoverinfo的最后一张图中):
library(plotly)
year <- c(2015,2015,2016,2016)
type <- c('A','B','A','B')
perc <- c(10,90,20,80)
data <- data.frame(year,type,perc)
# Plot all the data ... default hoverinfo shown
plot_ly(data,x=~year,y=~perc,color=~type) %>%
add_bars()%>%
layout(barmode = "stack")
# Plot all the data ... custom hoverinfo shown
plot_ly(data,x=~year,y=~perc,color=~type,
text = ~paste('Type',type,': ',perc,'%'),hoverinfo = 'text') %>%
add_bars()%>%
layout(barmode = "stack")
# Plot part of the data ... default hoverinfo shown
plot_ly(data[data$year == 2015,],x=~year,y=~perc,color=~type) %>%
add_bars()%>%
layout(barmode = "stack")
# Plot part of the data ... custom hoverinfo does not appear!
plot_ly(data[data$year == 2015,],x=~year,y=~perc,color=~type,
text = ~paste('Type',type,': ',perc,'%'),hoverinfo = 'text') %>%
add_bars()%>%
layout(barmode = "stack")使用R版本3.3.2和plotly版本4.5.6。
发布于 2016-11-29 20:48:04
得到了一些帮助,解决方案是将文本定义为列表。
plot_ly(data[data$year == 2015,],x=~year,y=~perc,color=~type,
text = ~list(paste('Type',type,': ',perc,'%')),hoverinfo = 'text') %>%
add_bars()%>%
layout(barmode = "stack")只要我知道只有一个条形,并且~类型的顺序在整个数据集中都是相同的,那么这个方法就可以工作。如果我将类型更改为
type <- c('A','B','B','A')然后运行
data <- data.frame(year,type,perc)
plot_ly(data[data$year == 2016,],x=~year,y=~perc,color=~type,
text = ~list(paste(year,'Type',type,': ',perc,'%')),hoverinfo = 'text') %>%
add_bars()%>%
layout(barmode = "stack") 我没有得到正确的hoverinfo顺序。所以这不是一个完美的解决方案。
https://stackoverflow.com/questions/40850415
复制相似问题