gpt4 book ai didi

r - 使用 R Plotly 组合分组条形图和堆积条形图

转载 作者:行者123 更新时间:2023-12-02 19:12:01 25 4
gpt4 key购买 nike

我正在尝试使用 plot_ly() 组合堆积条形图和分组条形图。我遇到了一些问题,但无法针对这种情况获得完美的解决方案。

我遇到了solution使用ggplot2。但我需要使用 plot_ly

来实现

这是我试图绘制的数据框

structure(list(QuarterYear = c("Q4 2019", "Q4 2019", "Q4 2019", 
"Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019",
"Q4 2019", "Q4 2019", "Q4 2019", "Q1 2020", "Q1 2020", "Q1 2020",
"Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020",
"Q1 2020", "Q1 2020", "Q1 2020", "Q2 2020", "Q2 2020", "Q2 2020",
"Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020",
"Q2 2020", "Q2 2020", "Q2 2020", "Q3 2020", "Q3 2020", "Q3 2020",
"Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020",
"Q3 2020", "Q3 2020", "Q3 2020"), Grade = c("Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11"), Type = c("overallAverage",
"CT", "RT", "overallAverage", "CT", "RT", "overallAverage", "CT",
"RT", "overallAverage", "CT", "RT", "overallAverage", "CT", "RT",
"overallAverage", "CT", "RT", "overallAverage", "CT", "RT", "overallAverage",
"CT", "RT", "overallAverage", "CT", "RT", "overallAverage", "CT",
"RT", "overallAverage", "CT", "RT", "overallAverage", "CT", "RT",
"overallAverage", "CT", "RT", "overallAverage", "CT", "RT", "overallAverage",
"CT", "RT", "overallAverage", "CT", "RT"), value = c(2.48, 2.21,
0.27, 3.48, 3.03, 0.45, 4.6, 4, 0.6, 2.8, 2.4, 0.4, 2.54, 2.28,
0.26, 3.45, 3, 0.45, 4.46, 3.88, 0.58, 3.56, 2.81, 0.75, 2.47,
2.14, 0.33, 2.96, 2.54, 0.41, 4.1, 3.69, 0.41, 3.44, 2.61, 0.83,
2, 1.81, 0.19, 2.54, 2.26, 0.28, 4.11, 3.68, 0.43, 2.67, 2.11,
0.56)), row.names = c(NA, -48L), class = "data.frame")

我知道我们需要将 barmode 定义为 stack(用于堆叠条形图)和 group(用于分组条形图)。我现在不知道使用 plot_ly

将这些 barmode 合并到一个图中

任何人都可以在 R 中提供合适的解决方案吗?

提前致谢!!

最佳答案

这是plot_ly 可以做的最好的事情:

library(plotly)
library(tidyverse)

dat <- tibble(QuarterYear = c("Q4 2019", "Q4 2019", "Q4 2019",
"Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019", "Q4 2019",
"Q4 2019", "Q4 2019", "Q4 2019", "Q1 2020", "Q1 2020", "Q1 2020",
"Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020", "Q1 2020",
"Q1 2020", "Q1 2020", "Q1 2020", "Q2 2020", "Q2 2020", "Q2 2020",
"Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020", "Q2 2020",
"Q2 2020", "Q2 2020", "Q2 2020", "Q3 2020", "Q3 2020", "Q3 2020",
"Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020", "Q3 2020",
"Q3 2020", "Q3 2020", "Q3 2020"),
Grade = c("Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11", "Grade 8", "Grade 8",
"Grade 8", "Grade 9", "Grade 9", "Grade 9", "Grade 10", "Grade 10",
"Grade 10", "Grade 11", "Grade 11", "Grade 11"),
Type = c("overallAverage",
"CT", "RT", "overallAverage", "CT", "RT", "overallAverage", "CT",
"RT", "overallAverage", "CT", "RT", "overallAverage", "CT", "RT",
"overallAverage", "CT", "RT", "overallAverage", "CT", "RT", "overallAverage",
"CT", "RT", "overallAverage", "CT", "RT", "overallAverage", "CT",
"RT", "overallAverage", "CT", "RT", "overallAverage", "CT", "RT",
"overallAverage", "CT", "RT", "overallAverage", "CT", "RT", "overallAverage",
"CT", "RT", "overallAverage", "CT", "RT"),
value = c(2.48, 2.21,
0.27, 3.48, 3.03, 0.45, 4.6, 4, 0.6, 2.8, 2.4, 0.4, 2.54, 2.28,
0.26, 3.45, 3, 0.45, 4.46, 3.88, 0.58, 3.56, 2.81, 0.75, 2.47,
2.14, 0.33, 2.96, 2.54, 0.41, 4.1, 3.69, 0.41, 3.44, 2.61, 0.83,
2, 1.81, 0.19, 2.54, 2.26, 0.28, 4.11, 3.68, 0.43, 2.67, 2.11,
0.56))


q4_2019 <- dat %>% filter(QuarterYear == "Q4 2019") %>%
group_by(Grade) %>%
arrange(Grade) %>%
plot_ly(
x = ~Type,
y = ~value,
color= ~Grade,
colors = 'Reds',
type = 'bar',
legendgroup=~Grade) %>%
layout(xaxis = list(title = "Q4 2019"))

q1_2020 <- dat %>% filter(QuarterYear == "Q1 2020") %>%
group_by(Grade) %>%
arrange(Grade) %>%
plot_ly(
x = ~Type,
y = ~value,
color= ~Grade,
colors = 'Reds',
type = 'bar',
legendgroup=~Grade,
showlegend = FALSE) %>%
layout(xaxis = list(title = "Q1 2020"))


q2_2020 <- dat %>% filter(QuarterYear == "Q2 2020") %>%
group_by(Grade) %>%
arrange(Grade) %>%
plot_ly(
x = ~Type,
y = ~value,
color= ~Grade,
colors = 'Reds',
type = 'bar',
legendgroup=~Grade,
showlegend = FALSE) %>%
layout(xaxis = list(title = "Q2 2020"))

q3_2020 <- dat %>% filter(QuarterYear == "Q3 2020") %>%
group_by(Grade) %>%
arrange(Grade) %>%
plot_ly(
x = ~Type,
y = ~value,
color= ~Grade,
colors = 'Reds',
type = 'bar',
legendgroup =~Grade,
showlegend = FALSE) %>%
layout(xaxis = list(title = "Q3 2020"))

subplot(q4_2019, q1_2020, q2_2020, q3_2020, titleX = TRUE, shareY = T) %>%
layout(barmode = 'stack', showlegend = TRUE)

subplots and stacked bars in plot_ly

关于r - 使用 R Plotly 组合分组条形图和堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64154396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com