如何生成堆积条形图?
How to gganimate a stacked bar graph?
我正在尝试在四个堆叠条形图之间进行转换。输出与我的预期不完全相同,我无法确定这是我的代码中的错误还是 gganimate
R 包中的错误。
这是我使用的数据框:
df <- structure(list(name = c("variable", "variable", "variable", "variable",
"variable", "variable", "variable", "variable", "variable", "variable",
"variable", "variable", "variable"), groups = structure(c(3L,
3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L), .Label = c("group 1",
"group 2", "group 3", "group 4"), class = "factor"), score = structure(c(4L,
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("4",
"3", "2", "1"), class = c("ordered", "factor")), percentage = c(8,
38, 38, 16, 17.1428571428571, 40, 42.8571428571429, 40, 20, 40,
5, 65, 30), percentage2 = c("8%", "38%", "38%", "16%", "17.1%",
"40%", "42.9%", "40%", "20%", "40%", "5%", "65%", "30%"), label = c(0.04,
0.27, 0.65, 0.92, 0.0857142857142857, 0.371428571428571, 0.785714285714286,
0.2, 0.5, 0.8, 0.025, 0.375, 0.85)), row.names = c(NA, -13L), class = "data.frame")
当我制作组变量的一个阶段的堆叠条形图时,我得到例如这个:
library(ggplot2)
library(dplyr)
ggplot(filter(df, groups == "group 3"),
aes(x = name, y = percentage, fill = score)) +
geom_bar(stat = "identity", position = "fill", width = 0.8) +
geom_text(aes(y = label, label = percentage2), color = "grey25") +
coord_flip() +
scale_fill_manual(values=c("darkgreen", "lightgreen", "yellow", "red"),
guide = guide_legend(reverse = TRUE), drop=FALSE)
但是当我尝试添加四个不同小组阶段的 gganimate
动画时,我得到了这个:
library(gganimate)
ggplot(df, aes(x = name, y = percentage, fill = score)) +
geom_bar(stat = "identity", position = "fill", width = 0.8) +
geom_text(aes(y = label, label = percentage2), color = "grey25") +
coord_flip() +
scale_fill_manual(values = c("darkgreen", "lightgreen", "yellow", "red"),
guide= guide_legend(reverse = TRUE), drop = FALSE) +
transition_states(groups, transition_length = 2, state_length = 1)
好像是把所有组的所有百分比(条长)同时加到动画里。我希望四个不同组的堆叠条形图之间没有间隙的过渡。我怎样才能让这个动画在条形图之间无间隙地过渡?
当然可以,但在当前版本中 gganimate
您需要编辑数据框。
代码
g <- ggplot(df, aes(x = name, y = c, fill = score, group = score)) +
geom_col(position = "identity", width = 0.8) +
coord_flip() +
labs(title = "{closest_state}") +
geom_label(aes(y = c, label = percentage2)) +
scale_fill_manual(values = c("darkgreen", "lightgreen", "yellow", "red"),
guide= guide_legend(reverse = TRUE), drop = FALSE) +
transition_states(groups, transition_length = 2, state_length = 1)
animate(g, nframes = 100)
数据
df$c <- ave(df$percentage, df$group, FUN=cumsum)
df <- df[order(df$groups, df$score, df$c), ]
df
name groups score percentage percentage2 label c
10 variable group 1 4 40.00000 40% 0.80000000 100.00000
9 variable group 1 3 20.00000 20% 0.50000000 60.00000
8 variable group 1 2 40.00000 40% 0.20000000 40.00000
7 variable group 2 4 42.85714 42.9% 0.78571429 100.00000
6 variable group 2 3 40.00000 40% 0.37142857 57.14286
5 variable group 2 2 17.14286 17.1% 0.08571429 17.14286
4 variable group 3 4 16.00000 16% 0.92000000 100.00000
3 variable group 3 3 38.00000 38% 0.65000000 84.00000
2 variable group 3 2 38.00000 38% 0.27000000 46.00000
1 variable group 3 1 8.00000 8% 0.04000000 8.00000
13 variable group 4 4 30.00000 30% 0.85000000 100.00000
12 variable group 4 3 65.00000 65% 0.37500000 70.00000
11 variable group 4 2 5.00000 5% 0.02500000 5.00000
==========================
说明
为什么?在 gganimate
版本 "0.9.9.9999"
中,动画情节将无法正确分组和堆叠(正如您正确指出的那样,这是一个错误)。这就是为什么你需要
- 对条形图的位置进行特征设计(变量
c
)
- 按
c
大小降序排列条形图(这样较大的条形图不会与较小的条形图重叠)
真正有用的是:将代码分解成最基本的部分,只保留重要的东西:
g <- ggplot(df, aes(x = "", y = c, fill = score, group = score)) +
geom_col(position = "identity") +
labs(title = "{closest_state}") +
transition_states(groups, transition_length = 2, state_length = 1)
animate(g, nframes = 10)
这比原始代码更容易使用。很明显,问题出在 y =
(例如,c、百分比)、group =
(例如,分数、组)和 position =
(例如,堆栈、闪避,闪避 2,身份,填充)。
如果您有任何问题,请随时给我留言。
我正在尝试在四个堆叠条形图之间进行转换。输出与我的预期不完全相同,我无法确定这是我的代码中的错误还是 gganimate
R 包中的错误。
这是我使用的数据框:
df <- structure(list(name = c("variable", "variable", "variable", "variable",
"variable", "variable", "variable", "variable", "variable", "variable",
"variable", "variable", "variable"), groups = structure(c(3L,
3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L), .Label = c("group 1",
"group 2", "group 3", "group 4"), class = "factor"), score = structure(c(4L,
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("4",
"3", "2", "1"), class = c("ordered", "factor")), percentage = c(8,
38, 38, 16, 17.1428571428571, 40, 42.8571428571429, 40, 20, 40,
5, 65, 30), percentage2 = c("8%", "38%", "38%", "16%", "17.1%",
"40%", "42.9%", "40%", "20%", "40%", "5%", "65%", "30%"), label = c(0.04,
0.27, 0.65, 0.92, 0.0857142857142857, 0.371428571428571, 0.785714285714286,
0.2, 0.5, 0.8, 0.025, 0.375, 0.85)), row.names = c(NA, -13L), class = "data.frame")
当我制作组变量的一个阶段的堆叠条形图时,我得到例如这个:
library(ggplot2)
library(dplyr)
ggplot(filter(df, groups == "group 3"),
aes(x = name, y = percentage, fill = score)) +
geom_bar(stat = "identity", position = "fill", width = 0.8) +
geom_text(aes(y = label, label = percentage2), color = "grey25") +
coord_flip() +
scale_fill_manual(values=c("darkgreen", "lightgreen", "yellow", "red"),
guide = guide_legend(reverse = TRUE), drop=FALSE)
但是当我尝试添加四个不同小组阶段的 gganimate
动画时,我得到了这个:
library(gganimate)
ggplot(df, aes(x = name, y = percentage, fill = score)) +
geom_bar(stat = "identity", position = "fill", width = 0.8) +
geom_text(aes(y = label, label = percentage2), color = "grey25") +
coord_flip() +
scale_fill_manual(values = c("darkgreen", "lightgreen", "yellow", "red"),
guide= guide_legend(reverse = TRUE), drop = FALSE) +
transition_states(groups, transition_length = 2, state_length = 1)
好像是把所有组的所有百分比(条长)同时加到动画里。我希望四个不同组的堆叠条形图之间没有间隙的过渡。我怎样才能让这个动画在条形图之间无间隙地过渡?
当然可以,但在当前版本中 gganimate
您需要编辑数据框。
代码
g <- ggplot(df, aes(x = name, y = c, fill = score, group = score)) +
geom_col(position = "identity", width = 0.8) +
coord_flip() +
labs(title = "{closest_state}") +
geom_label(aes(y = c, label = percentage2)) +
scale_fill_manual(values = c("darkgreen", "lightgreen", "yellow", "red"),
guide= guide_legend(reverse = TRUE), drop = FALSE) +
transition_states(groups, transition_length = 2, state_length = 1)
animate(g, nframes = 100)
数据
df$c <- ave(df$percentage, df$group, FUN=cumsum)
df <- df[order(df$groups, df$score, df$c), ]
df
name groups score percentage percentage2 label c
10 variable group 1 4 40.00000 40% 0.80000000 100.00000
9 variable group 1 3 20.00000 20% 0.50000000 60.00000
8 variable group 1 2 40.00000 40% 0.20000000 40.00000
7 variable group 2 4 42.85714 42.9% 0.78571429 100.00000
6 variable group 2 3 40.00000 40% 0.37142857 57.14286
5 variable group 2 2 17.14286 17.1% 0.08571429 17.14286
4 variable group 3 4 16.00000 16% 0.92000000 100.00000
3 variable group 3 3 38.00000 38% 0.65000000 84.00000
2 variable group 3 2 38.00000 38% 0.27000000 46.00000
1 variable group 3 1 8.00000 8% 0.04000000 8.00000
13 variable group 4 4 30.00000 30% 0.85000000 100.00000
12 variable group 4 3 65.00000 65% 0.37500000 70.00000
11 variable group 4 2 5.00000 5% 0.02500000 5.00000
==========================
说明
为什么?在 gganimate
版本 "0.9.9.9999"
中,动画情节将无法正确分组和堆叠(正如您正确指出的那样,这是一个错误)。这就是为什么你需要
- 对条形图的位置进行特征设计(变量
c
) - 按
c
大小降序排列条形图(这样较大的条形图不会与较小的条形图重叠)
真正有用的是:将代码分解成最基本的部分,只保留重要的东西:
g <- ggplot(df, aes(x = "", y = c, fill = score, group = score)) +
geom_col(position = "identity") +
labs(title = "{closest_state}") +
transition_states(groups, transition_length = 2, state_length = 1)
animate(g, nframes = 10)
这比原始代码更容易使用。很明显,问题出在 y =
(例如,c、百分比)、group =
(例如,分数、组)和 position =
(例如,堆栈、闪避,闪避 2,身份,填充)。
如果您有任何问题,请随时给我留言。