繁体   English   中英

R 代码 - 使用 ggplot 帮助创建一个 100% 堆叠条形图,如下图所示:

[英]R code - help creating a 100% stacked bar chart like the below image with ggplot:

我有以下 dataframe 可重现代码:

dput(u) <-
structure(list(name = c("rankings_message_1", "rankings_message_2", 
"rankings_message_3", "rankings_message_4", "rankings_message_5", 
"rankings_message_6", "rankings_message_7", "rankings_message_8", 
"rankings_message_9", "rankings_message_10", "rankings_message_11", 
"rankings_message_12", "rankings_message_13", "rankings_message_1", 
"rankings_message_2", "rankings_message_3", "rankings_message_4", 
"rankings_message_5", "rankings_message_6", "rankings_message_7", 
"rankings_message_8", "rankings_message_9", "rankings_message_10", 
"rankings_message_11", "rankings_message_12", "rankings_message_13", 
"rankings_message_1", "rankings_message_2", "rankings_message_3", 
"rankings_message_4", "rankings_message_5", "rankings_message_6", 
"rankings_message_7", "rankings_message_8", "rankings_message_9", 
"rankings_message_10", "rankings_message_11", "rankings_message_12", 
"rankings_message_13", "rankings_message_1", "rankings_message_2", 
"rankings_message_3", "rankings_message_4", "rankings_message_5", 
"rankings_message_6", "rankings_message_7", "rankings_message_8", 
"rankings_message_9", "rankings_message_10", "rankings_message_11", 
"rankings_message_12", "rankings_message_13", "rankings_message_1", 
"rankings_message_2", "rankings_message_3", "rankings_message_4", 
"rankings_message_5", "rankings_message_6", "rankings_message_7", 
"rankings_message_8", "rankings_message_9", "rankings_message_10", 
"rankings_message_11", "rankings_message_12", "rankings_message_13", 
"rankings_message_1", "rankings_message_2", "rankings_message_3", 
"rankings_message_4", "rankings_message_5", "rankings_message_6", 
"rankings_message_7", "rankings_message_8", "rankings_message_9", 
"rankings_message_10", "rankings_message_11", "rankings_message_12", 
"rankings_message_13", "rankings_message_1", "rankings_message_2", 
"rankings_message_3", "rankings_message_4", "rankings_message_5", 
"rankings_message_6", "rankings_message_7", "rankings_message_8", 
"rankings_message_9", "rankings_message_10", "rankings_message_11", 
"rankings_message_12", "rankings_message_13", "rankings_message_1", 
"rankings_message_2", "rankings_message_3", "rankings_message_4", 
"rankings_message_5", "rankings_message_6", "rankings_message_7", 
"rankings_message_8", "rankings_message_9", "rankings_message_10", 
"rankings_message_11", "rankings_message_12", "rankings_message_13", 
"rankings_message_1", "rankings_message_2", "rankings_message_3", 
"rankings_message_4", "rankings_message_5", "rankings_message_6", 
"rankings_message_7", "rankings_message_8", "rankings_message_9", 
"rankings_message_10", "rankings_message_11", "rankings_message_12", 
"rankings_message_13", "rankings_message_1", "rankings_message_2", 
"rankings_message_3", "rankings_message_4", "rankings_message_5", 
"rankings_message_6", "rankings_message_7", "rankings_message_8", 
"rankings_message_9", "rankings_message_10", "rankings_message_11", 
"rankings_message_12", "rankings_message_13", "rankings_message_1", 
"rankings_message_2", "rankings_message_3", "rankings_message_4", 
"rankings_message_5", "rankings_message_6", "rankings_message_7", 
"rankings_message_8", "rankings_message_9", "rankings_message_10", 
"rankings_message_11", "rankings_message_12", "rankings_message_13", 
"rankings_message_1", "rankings_message_2", "rankings_message_3", 
"rankings_message_4", "rankings_message_5", "rankings_message_6", 
"rankings_message_7", "rankings_message_8", "rankings_message_9", 
"rankings_message_10", "rankings_message_11", "rankings_message_12", 
"rankings_message_13", "rankings_message_1", "rankings_message_2", 
"rankings_message_3", "rankings_message_4", "rankings_message_5", 
"rankings_message_6", "rankings_message_7", "rankings_message_8", 
"rankings_message_9", "rankings_message_10", "rankings_message_11", 
"rankings_message_12", "rankings_message_13"), value = c(5, 6, 
3, 13, 10, 12, 7, 19, 3, 7, 3, 5, 6, 6, 3, 6, 13, 7, 15, 9, 11, 
7, 6, 6, 4, 8, 7, 8, 4, 11, 6, 11, 11, 11, 6, 6, 5, 8, 6, 8, 
7, 6, 9, 6, 11, 9, 7, 9, 7, 7, 7, 6, 8, 6, 8, 7, 9, 7, 7, 10, 
9, 6, 7, 9, 7, 7, 11, 7, 7, 8, 7, 6, 7, 9, 10, 7, 8, 7, 8, 7, 
11, 5, 6, 4, 7, 4, 11, 9, 6, 10, 11, 9, 8, 9, 5, 7, 6, 6, 6, 
8, 9, 9, 8, 10, 9, 9, 9, 5, 5, 4, 7, 6, 9, 11, 10, 9, 8, 10, 
7, 10, 6, 4, 6, 7, 5, 7, 8, 8, 10, 10, 8, 7, 8, 7, 6, 7, 7, 4, 
9, 8, 11, 8, 9, 9, 8, 9, 9, 7, 4, 7, 5, 8, 9, 10, 7, 6, 5, 13, 
10, 4, 18, 5, 11, 4, 4, 5, 9, 6, 5)), row.names = c(NA, -169L
), class = c("tbl_df", "tbl", "data.frame"))

这是我的ggplot代码:

ggplot(u, aes(value, factor(value), fill = name)) +
  geom_bar(stat = "identity", position = "fill")+ # #position fill makes them all 100%
  geom_text(aes(label=value), position = position_stack(vjust = .5), color = "White", size =7)

这使得这张图表:

在此处输入图像描述

但我想创建这样的东西:

在此处输入图像描述

我在 ggplot 中尝试了不同的组合,但似乎无法正确显示。 我相信我需要在某处使用因子函数,但可能会被弄错......总的来说,每个条都是一个“消息”,每个消息/条被分解成 13 个块,即 %'s。 我希望每个 % 块都是不同的颜色,如我尝试创建的图像所示。 任何帮助,将不胜感激!

您的数据包含 y 轴变量(“名称”)和 x 轴变量(“值”),但不包含用于堆叠或填充条形的分组变量(原始有一个名为“rank”的变量你没有的)。

您可以通过按“名称”变量分组并沿组创建一个序列来创建一个等效变量,然后将其转换为一个因子。 您需要在文本和条形上使用 position = "fill"。

library(tidyverse)

u %>%
  group_by(name) %>%
  mutate(element = factor(row_number(name))) %>%
  ggplot(aes(value, name, fill = element)) +
  geom_col(position = "fill") +
  geom_text(aes(label=value), position = position_fill(vjust = .5), 
            color = "White", size = 7) +
  scale_fill_discrete(guide = "none")

在此处输入图像描述

或者,为了让它看起来更像原版,使用构面、虚拟 y 变量、适当的调色板和一些主题更改:

u %>%
  group_by(name) %>%
  mutate(element = factor(row_number(name)),
         name = factor(name,
                       paste("rankings_message", 13:1, sep = "_"))) %>%
  
  ggplot(aes(value, "1", fill = element)) +
  geom_col(position = "fill", orientation = "y", size = 0.5, col = "white") +
  geom_text(aes(label=value), position = position_fill(vjust = .5), 
            color = "White", size = 7) +
  scale_fill_manual(guide = "none",
                    values = rev(c("#ffb04d", "#ff8a1e", "#e5573e", "#af0020",
                                   "#3e84ac", "#64baff", "#96a7dd", "#765ab1", 
                                   "#483374", "#ce74a5", "#ffb04d", "#ff8a1e",
                                   "#e3583f"))) +
  facet_wrap(vars(name), nrow = 13) +
  theme_void() +
  theme(strip.text = element_text(hjust = 0.05))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM