【问题标题】:ggplot2: How to plot barplot, where bars signify percentage and are colored according to percentage value?ggplot2:如何绘制条形图,条形表示百分比并根据百分比值着色?
【发布时间】:2016-02-15 22:15:18
【问题描述】:

我需要制作特定类型的条形图,并且需要使用 ggplot2 - 遗憾的是我对这个包知之甚少 - 我今天开始学习它。

我有这样的数据框:

name <- letters[1:10]
percentage <- c(0.74, 0.856, 0.14, 0.97, 0, 0.99, 0.862, 0.5, 0.234, 0.76)
df <- data.frame(name = name, percentage = percentage)

       name percentage
1     a      0.740
2     b      0.856
3     c      0.140
4     d      0.970
5     e      0.000
6     f      0.990
7     g      0.862
8     h      0.500
9     i      0.234
10    j      0.760

y 轴上应该是名称,x 轴上应该是百分比。应为每个名称绘制水平条。栏应该分为两部分:

  • 首先长度等于百分比,根据百分比值着色:如果百分比 > 0.95,则为绿色,如果百分比 > 0.85,则为橙色,否则为红色
  • 长度为 1 的秒 - 百分比,灰色

我用 barplot 做了一些类似于规范的东西(但它很丑,不是所有的名字都是可见的,也没有第二部分):

barplot(df$percentage , main = "Percentage per letter", 
        horiz = TRUE,    names.arg = df$name, xlim = c(0,1),
        col = ifelse(df$percentage > 0.95, "green", 
        ifelse(df$percentage > .85,'orange','red')))

使用 ggplot2 我设法实现了这一点:

ggplot( data = df, aes( x = name, y = percentage)) +
  geom_bar(stat = "identity") + 
  coord_flip() + theme_minimal() 

有人可以给我一些提示吗?

编辑:

感谢@lukeA 的帮助,我设法构建了非常漂亮的条形图。这是我的代码:

# Set colours (nice red, nice orange, nice green)
colours <- c("#D73027", "#FDAE61","#1A9850")

# Transform table:
df <- rbind(
  transform(df, type = 1, fill = cut(percentage, breaks = c(-Inf, 0.85, 0.95, Inf), right = TRUE, labels = colours)),
  transform(df, percentage = 1 - percentage, type = 2, fill = "#EEEEEE")
)

# Name as alphabetically ordered factor (to ensure, that names will be placed in 
# alphabetical order on y axis)
df <- within( df, name <- ordered(name, levels = rev(sort(unique(name)))))

ggplot(data = df, 
       aes( x = name, y = percentage, fill = fill)) +
  geom_bar(stat = "identity", position = "stack", width = 0.75) + 
  scale_fill_identity(guide = "none")  + 
  labs(x = NULL, y = NULL) + 
  scale_y_continuous(expand = c(0,0)) +
  scale_x_discrete(expand = c(0,0)) +
  coord_flip() +
  theme_classic() +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_text(size = 11, colour = "black" ),
        axis.text.x = element_text(size = 11, colour = "black" ),
        axis.line = element_blank(),
        plot.margin = unit(c(10,10,10,10),"mm")
  )

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以以此为基础:

    dat <- rbind(
      transform(df, fill = cut(percentage, breaks=c(-Inf,.85, .95, Inf), right = T, labels = c("red", "orange", "green"))),
      transform(df, percentage=1-percentage, fill="grey")
    )
    ggplot( data = dat, aes( x = name, y = percentage, fill = fill)) +
      geom_bar(stat = "identity", position="stack") + 
      scale_fill_identity(guide="none")+
      coord_flip() + theme_minimal() 
    

    【讨论】:

    • 如果你想要并排的栏,请使用position="dodge"
    猜你喜欢
    • 2019-09-27
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多