带 ggplot2 的堆积条形图

Stacked bar plot with ggplot2

我有一个这种形式的 R 数据框:(第一、第二、... 是行名;A、B、... 是列名)

              A   B   C    D  E
 first        30  0   0    0  0
 second       0   20  120  0  0
 third        0   40  100  0  0
 fourth       0   0   0    30 60

我想要 ggplot() 做的是绘制一个条形图,在 x 轴上显示行名称,在 y 轴上显示行总和,这些行总和应按列标题类别 color-stacked 和带有数字标签,所以上面的数据是这样的:

您应该在数据框中添加行名作为变量,并将数据转换为长格式,以便 ggplot 可以处理它。我认为这样的东西接近你的意思:

yourDataFrame %>% 
    mutate(Label = rownames(df)) %>% # add row names as a variable
    reshape2::melt(.) %>% # melt to long format
    ggplot(., aes(x = Label, y = value, fill = variable)) + 
        geom_bar(stat='identity')

我想你正在寻找这样的东西:

df %>%
  rownames_to_column(var = 'x') %>%
  pivot_longer(-x) %>%
  filter(value > 0) %>% 
  mutate(x = factor(x, levels = c('first', 'second', 'third', 'forth'))) %>% 
  ggplot(aes(fill = forcats::fct_rev(name), y = value, x = x, label = value)) +
  geom_bar(position="stack", stat="identity") +
  geom_text(aes(label=value)) +
  theme(legend.title = element_blank())

数据:

structure(list(A = c(30L, 0L, 0L, 0L), B = c(0L, 20L, 40L, 0L
), C = c(0L, 120L, 100L, 0L), D = c(0L, 0L, 0L, 30L), E = c(0L, 
0L, 0L, 60L)), class = "data.frame", row.names = c("first", "second", 
"third", "forth")) -> df