[英]How can I combine group and stack bar chart with layout.shape in background using R Plotly?
我正在嘗試創建一個可視化,我需要同時使用堆棧和組條形圖。 我使用的數據有數千行,下面的數據是其中的一部分。
Month Week Cat n
_____________________________
2019-Dec 4 A 17
2019-Dec 4 B 6
2019-Dec 5 A 21
2019-Dec 5 C 10
2020-Jan 1 A 19
2020-Jan 1 B 20
2020-Jan 1 C 12
它按 Month 分組,按 Cat 堆疊並區分不同的Week我想在不同周的背景中添加矩形形狀( https://plotly.com/r/shapes/ )。
提前致謝!
正如評論中提到的,堆疊+分組條形圖尚未在 plotly 中實現。 這是一個ggplot2
/ ggplotly
解決方法:
library(plotly)
library(ggplot2)
DF <- data.frame(
stringsAsFactors = FALSE,
Month = c("2019-Dec","2019-Dec",
"2019-Dec","2019-Dec","2020-Jan","2020-Jan","2020-Jan"),
Week = c(4L, 4L, 5L, 5L, 1L, 1L, 1L),
Cat = c("A", "B", "A", "C", "A", "B", "C"),
n = c(17L, 6L, 21L, 10L, 19L, 20L, 12L)
)
p <- ggplot(DF, aes(x = Week, y = n, fill = Cat)) + geom_bar(stat = 'identity', position = 'stack') + facet_grid( ~ Month) + theme_bw()
ggplotly(p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.