[英]Alignment of labels in stacked chart using plotly() in R
我有一个 dataframe
df <- data.frame("QuarterYear" = c("Q1 2019","Q1 2019","Q2 2019","Q2 2019","Q3 2019","Q3 2019","Q3 2019"), "Size" = c("Medium","Small","Large","Medium","Large","Medium","Small"),
"percentage" = c(98,2,29,71,13,74,13))
在绘制图表之前,我按特定顺序对 dataframe 进行排序
quarterYear <- c("Q1 2019","Q2 2019","Q3 2019","Q4 2019")
projSize <- c("Small", "Medium","Large")
df <- df[order(match(df$QuarterYear, quarterYear), match(df$Size, projSize)), ]
plot_ly(df, x = df$QuarterYear,
y = df$percentage,
type = 'bar',
name = df$Size,
text = paste(df$percentage,"%"),
textposition = 'top',
hoverinfo = 'text',
hovertext = paste('Size: ', df$Size,
'<br> % of Total count: ', paste(df$percentage,"%")),
color = df$Size) %>%
layout(yaxis = list(title = "% of Count", zeroline = FALSE,
showline = FALSE, ticksuffix = "%"), barmode = 'stack',hoverlabel = list(bgcolor= 'white')) %>%
layout(legend = list(orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.13))%>%
add_annotations(text = paste0(df$percentage, "%"),
x = df$QuarterYear,
y = unlist(tapply(df$percentage, df$QuarterYear, FUN=cumsum))-(df$percentage/2),
showarrow = FALSE) %>%
layout(xaxis = list(categoryorder = 'array',
categoryarray = df$QuarterYear))
当我对数据进行排序和 plot 时,标签变得一团糟
如何解决 R 中的这个问题?
提前致谢!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.