【发布时间】:2020-08-14 13:25:44
【问题描述】:
我有一个数据框,从中在 R 中绘制堆积条形图
myDF <- data.frame("group" = c("A","B","C","A","B","C"),
"date" = c("25-May","25-May","25-May","01-Jun","01-Jun","01-Jun"),
"hours" = c(42,48,51,32,25,48),
stringsAsFactors = FALSE)
p <- plot_ly(myDF, x = myDF$date,
y = myDF$group,
type = 'bar',
name = myDF$group,
text = myDF$Hours,
color = myDF$group,
colors = brewer.pal(length(unique(myDF$group)),
"Paired"))%>%
layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
layout(xaxis = list(categoryorder = 'array',
categoryarray = myDF$date), showlegend = F)
我有另一个数据框,其中我每周的总小时数如下:
totalHours <- data.frame(
"date" = c("25-May","01-Jun"),
"hours" = c(141,105))
我正在尝试像这样在栏的顶部显示堆栈的总和
annotations <- list()
for (i in 1:length(totalHours$hours)) {
annotations[[i]] <- list(x = totalHours$date[[i]],
text = totalHours$hours[[i]],
yanchor='bottom',
showarrow = FALSE)
}
在绘图布局中添加注释
p <- p %>% layout(annotations = annotations)
我将标签放在中心而不是显示在顶部。我不清楚代码中哪里有遗漏。谁能为此提供解决方案?
提前致谢!!
【问题讨论】:
标签: r annotations plotly