r - 使用 R 中的 plotly() 对齐堆叠图中的标签
问题描述
我有一个数据框
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))
在绘制图表之前,我按特定顺序对数据框进行排序
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))
当我对数据进行排序和绘制时,标签变得一团糟
如何在 R 中解决这个问题?
提前致谢!!
解决方案
推荐阅读
- linux - 使用 Henry 清理工具进行 Looker 清理 - yaml.parser.ParserError: expected '
',但发现' ' - vhdl - ModelSim:Altera On-Chip Flash 在 readdata 中没有响应
- c# - 具有多个应用程序的洋葱架构设计
- python - jinja2 模板的片段缓存
- javascript - 如何在 Angular 的 Highcharts 中管理 data-highcharts-chart 属性
- c# - MetadataTypeAttribute 验证被忽略
- javascript - 如何在材质ui滑块中的标签悬停上获取工具提示
- node.js - "TypeError: Employee is not a constructor" while building a MEAN app
- javascript - 以混合方式使用 PHP 和 Javascript,遍历循环
- xml - XSD - Input1 检查的数量低于 Input2 的 10%