【问题标题】:ggplot: stacked barplot in reverse orderggplot:以相反顺序堆叠的条形图
【发布时间】:2017-07-21 06:51:17
【问题描述】:

所以我有数据框

dput(df)
structure(list(Frequency = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), .Label = c("2", "3", "4", "5"), class = "factor"), Prcentage = c(1, 
33, 58, 8, 2, 40, 53, 5), label = list("Insufficient", "Average", 
    "Good", "Excellent", "Insufficient", "Average", "Good", "Excellent"), 
    name = c("implementation", "implementation", "implementation", 
    "implementation", "energy", "energy", "energy", "energy")), .Names = c("Frequency", 
"Prcentage", "label", "name"), row.names = c(NA, 8L), class = "data.frame")

还有下面的代码

# Get the levels for type in the required order
df$label = factor(df$label, levels = c("Unacceptable","Insufficient", "Average","Good","Excellent"))
df = arrange(df, name, desc(label))

# Format the labels and calculate their positions
df = ddply(df, .(name), transform, pos = (cumsum(Prcentage) - 0.5 * Prcentage))
df$label1 = paste0(sprintf("%.0f", df$Prcentage), "%")


# Plot
ggplot(df, aes(x = factor(name), y = Prcentage, fill = label, order=desc(label))) +
  geom_bar(stat = "identity", width = 0.5) +
  geom_text(aes(y = pos, label = label1), size = 4) +  theme_classic() + 
  scale_y_continuous(position = "top",expand = c(0, 0),breaks = seq(min(0), max(0,102), by = 10),limits = c(0,102),labels = dollar_format(suffix = "%", prefix = "")) + 
  coord_flip() +
  xlab("") + ylab("") + 
  theme(legend.position="bottom",legend.title = element_blank()) +
  scale_fill_manual(values = c("#ff0000","#fff68f","#b2b2b2","#1baf05","#006080"),drop = FALSE) 

我制作了以下情节

但现在我正在努力让这些条按相反的顺序排列。 Sm 我的输出 应该用条形图的正确值反向堆叠(例如,1% 黄色应该首先位于图的左侧,然后是 33%,然后是 56%,最右边是 8% )。我已经尝试通过添加来做到这一点

+ geom_col(position = position_stack(reverse = TRUE)) (after geom_bar)

这是由谁制作的

但这并不正确,因为条形图中的值不正确。

我也看过这里

How to control ordering of stacked bar chart using identity on ggplot2

Reverse fill order for histogram bars in ggplot2

Order Stacked Bar Graph in ggplot

Reverse fill order for histogram bars in ggplot2

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    标签的位置直接由pos值设置,如果你颠倒堆栈顺序,你需要颠倒那个:

    ggplot(df, aes(x = factor(name))) +
      geom_col(aes(y = Prcentage, fill = label), 
               position = position_stack(reverse = TRUE),
               width = .5) +
      # Set the position to its complementary
      geom_text(aes(y = 100 - pos, label = label1)) +
    
      # Rest of theme
      coord_flip() +
      scale_y_continuous(position = "top", 
                         expand = c(0, 0),
                         breaks = seq(min(0), max(0,102), by = 10),
                         limits = c(0,102),
                         labels = dollar_format(suffix = "%", prefix = "")) + 
      scale_fill_manual(values = c("#ff0000","#fff68f","#b2b2b2","#1baf05","#006080"), drop = FALSE) +
      xlab("") + ylab("") + 
      theme_classic() +
      theme(legend.position="bottom",legend.title = element_blank())
    

    【讨论】:

    • 谢谢!完美运行。
    • 一个问题:今天我尝试从不同的计算机(R 版本 3.2.2)运行您的代码,但不断出现错误:Error: could not find function "geom_col"。有什么想法吗?
    • 我想geom_col最近被介绍了。如果使用旧版本的ggplot2 并且无法升级geom_bar(..., stat='identity') 是等效的
    • 谢谢,我已经把问题卖掉了,只是手动更新了ggplot 包。现在geom_col 工作。
    • @Miha 另请注意,现在可以选择在 ggplot2 中堆叠文本,而不需要创建 pos 变量。示例:geom_text(aes(y = Prcentage, label = label1), position = position_stack(reverse = TRUE, vjust = .5))
    猜你喜欢
    • 2020-10-16
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2018-04-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多