【问题标题】:barplot data does not correspond to legend条形图数据与图例不对应
【发布时间】:2017-01-31 20:17:20
【问题描述】:

我有以下代码在 ggplot 中生成堆叠条形图

data_age <- data.frame(age = as.factor(c("16 to 20", "21 to 24", "25 to 30", "31 to 40", "40+")),
                   total = c(740, 1092, 855, 525, 182),
                   perc_total = c(22, 32, 25, 15, 5))

g_age <- ggplot(data_age , aes(1, perc_total, fill = age, label = perc_total)) +
 geom_bar(stat ="identity") +
 geom_text(size = 4, position = position_stack(vjust = 0.5), colour = "black") + 
 coord_flip() +
 scale_y_continuous(limits = c(0, 100)) +
 scale_x_continuous(limits = c(0, 2), breaks = 1) +
 scale_fill_manual(guide = guide_legend(title = NULL, keyheight = 0.5, keywidth = 0.5, direction = "horizontal"), values = c("#FFFFB2", "#FECC5C", "#FD8D3C", "#F03B20", "#BD0026")) +
 theme_bw() +
 theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), 
    axis.text.x = element_blank(), axis.ticks.x = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
    legend.position="bottom")

Which results in this barplot

但是我需要颠倒数据的顺序。我可以通过执行以下操作来实现这一点

g_age_2 <- ggplot(data_age , aes(1, perc_total, fill = levels(age)[5:1], label = perc_total)) +
  geom_bar(stat ="identity") +
  geom_text(size = 4, position = position_stack(vjust = 0.5), colour = "black") + 
  coord_flip() +
  scale_y_continuous(limits = c(0, 100)) +
  scale_x_continuous(limits = c(0, 2), breaks = 1) +
  scale_fill_manual(guide = guide_legend(title = NULL, keyheight = 0.5, keywidth = 0.5, direction = "horizontal"), values = c("#BD0026", "#F03B20", "#FD8D3C", "#FECC5C", "#FFFFB2")) + ## Inverted the colors too
  theme_bw() +
  theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), 
    axis.text.x = element_blank(), axis.ticks.x = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
    legend.position="bottom")

And this plot follows the order and the colors that I want, but the legend does not match

我该如何解决这个问题?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你可以这样做:

    g_age <- ggplot(data_age , aes(1, perc_total, fill = age, label = perc_total)) +
      geom_bar(stat ="identity",position = position_stack(reverse = TRUE)) +
      geom_text(size = 4, position = position_stack(vjust = 0.5,reverse = TRUE), colour = "black") + 
      coord_flip() +
      scale_y_continuous(limits = c(0, 100)) +
      scale_x_continuous(limits = c(0, 2), breaks = 1) +
      scale_fill_manual(guide = guide_legend(title = NULL, keyheight = 0.5, keywidth = 0.5, direction = "horizontal"),
                        values = c("#FFFFB2", "#FECC5C", "#FD8D3C", "#F03B20", "#BD0026")) +
      theme_void() +
      theme(legend.position="bottom")
    

    注意将position = position_stack(reverse = TRUE) 添加到geom_bar()geom_text()
    我还将theme 替换为theme_void() 以删除所有内容,只使用theme() 设置图例的位置。

    【讨论】:

    • 效果很好!我适应了我的原始代码,它按照我的预期工作
    猜你喜欢
    • 2016-07-12
    • 2018-07-15
    • 1970-01-01
    • 2020-05-15
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2016-05-03
    相关资源
    最近更新 更多