使用 R Plotly 组合分组条形图和堆叠条形图
Combining both grouped bar chart and stacked bar chart using R Plotly
我正在尝试使用 plot_ly()
组合堆叠条形图和分组条形图。我遇到了一些问题,但无法找到适合这种情况的完美解决方案。
我使用 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 中提供合适的解决方案吗?
提前致谢!!
如果你有两个图并希望它们显示在一个图中,我猜你可以使用以下内容:
fig <- subplot(fig1, fig2)
这是 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)
我正在尝试使用 plot_ly()
组合堆叠条形图和分组条形图。我遇到了一些问题,但无法找到适合这种情况的完美解决方案。
我使用 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 中提供合适的解决方案吗?
提前致谢!!
如果你有两个图并希望它们显示在一个图中,我猜你可以使用以下内容:
fig <- subplot(fig1, fig2)
这是 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)