如何在R中的堆积条形图的顶部显示总值
How to display the total value at the top of the stacked bar chart in R
我有一个数据框,在 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)
我得到的标签位于中心,而不是显示在顶部。我不清楚代码中哪里有遗漏。谁能为此提供解决方案?
提前致谢!!
你可以试试这个。这是标签位置的问题:
library(plotly)
library(RColorBrewer)
#Data
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)
#Plot
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)
#Data2
totalHours <- data.frame(
"date" = c("25-May","01-Jun"),
"hours" = c(141,105),stringsAsFactors = F)
#Create global coordinate
labs <- myDF %>% group_by(group) %>% mutate(Tot = sum(hours),Prop=hours/Tot) %>%
ungroup() %>% summarise(Val=sum(Prop))
#Annotate
annotations <- list()
for (i in 1:length(totalHours$hours)) {
annotations[[i]] <- list(x = totalHours$date[[i]],
y = labs$Val,
text = totalHours$hours[[i]],
yanchor='bottom',
showarrow = FALSE)
}
#Final plot
p <- p %>% layout(annotations = annotations)
输出:
我有一个数据框,在 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)
我得到的标签位于中心,而不是显示在顶部。我不清楚代码中哪里有遗漏。谁能为此提供解决方案?
提前致谢!!
你可以试试这个。这是标签位置的问题:
library(plotly)
library(RColorBrewer)
#Data
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)
#Plot
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)
#Data2
totalHours <- data.frame(
"date" = c("25-May","01-Jun"),
"hours" = c(141,105),stringsAsFactors = F)
#Create global coordinate
labs <- myDF %>% group_by(group) %>% mutate(Tot = sum(hours),Prop=hours/Tot) %>%
ungroup() %>% summarise(Val=sum(Prop))
#Annotate
annotations <- list()
for (i in 1:length(totalHours$hours)) {
annotations[[i]] <- list(x = totalHours$date[[i]],
y = labs$Val,
text = totalHours$hours[[i]],
yanchor='bottom',
showarrow = FALSE)
}
#Final plot
p <- p %>% layout(annotations = annotations)
输出: