【问题标题】:Add shadow effect ggplot2 bars (barplot)添加阴影效果ggplot2酒吧(Barplot)
【发布时间】:2015-05-13 21:40:34
【问题描述】:

我试图在情节中展示较差的设计选择。人们使用的一种浪费墨水的、可能会分散注意力的效果,如果是条的阴影效果。我想让 ggplot2 这样做。我的基本想法是使第一层半透明的条稍微高一点并向右移动。我可以得到稍高的,但不能得到稍向右的:

dat <- data_frame(
    School =c("Franklin", "Washington", "Jefferson", "Adams", "Madison", "Monroe"),
    sch = seq_along(School),
    count = sort(c(13, 17, 12, 14, 3, 22), TRUE),
    Percent = 100*round(count/sum(count), 2)
)

dat[["School"]] <- factor(dat[["School"]], levels = c("Franklin", 
    "Washington", "Jefferson", "Adams", "Madison", "Monroe"))

ggplot(dat) +
   geom_bar(aes(x = School, weight=Percent + .5), alpha=.1, width = .6) +
   geom_bar(aes(x = School, weight=Percent, fill = School), width = .6) +
   theme_bw()

此尝试给出以下警告,并且透明层被忽略(这是明智的):

ggplot(dat) +
   geom_bar(aes(x = School + .2, weight=Percent + .5), alpha=.1, width = .6) +
   geom_bar(aes(x = School, weight=Percent, fill = School), width = .6) +
   theme_bw()

## Warning messages:
## 1: In Ops.factor(School, 0.2) : ‘+’ not meaningful for factors
## 2: In Ops.factor(School, 0.2) : ‘+’ not meaningful for factors
   

【问题讨论】:

  • 我认为这不是一个完整的答案,但请尝试颠倒geom_bar 层的顺序并将School 显式转换为第二层的整数(在数据框中或@987654327 @调用)。
  • ...使用ystat = "identity" 而不是weight 似乎更接近你想要的。
  • ...如果我对两层都使用连续比例,我可以在后面(即首先)做灰色矩形,但当然这会破坏 x 轴标签,你会解决这个问题手动。 (很抱歉在 cmets 中对此进行了草拟……)
  • @joran 你的想法也帮助我实现了目标。这比你给我一个解决方案要好得多。

标签: r ggplot2


【解决方案1】:

我想这也许就是你要找的……?

ggplot(dat) +
    geom_bar(aes(x = as.integer(School) + .2, y= Percent - .5),stat = "identity", alpha=.2,width = 0.6) +
    geom_bar(aes(x = as.integer(School), y=Percent, fill = School),stat = "identity",width = 0.6) +
    scale_x_continuous(breaks = 1:6,labels = as.character(dat$School)) +
    theme_bw()

【讨论】:

  • 非常感谢乔兰。你的提示把我带到了那里,这封印了它。
  • 无论如何我更喜欢顶部视角,所以这对查看很有帮助。
【解决方案2】:

使用 @joran 给我的作品(感谢 Joran):

ggplot(dat) +
    geom_bar(aes(x = School, y=Percent), fill=NA, color=NA, width = .6, stat = "identity") +
    geom_bar(aes(x = sch + .075, y=Percent + .5), alpha=.3, width = .6, stat = "identity") +
    geom_bar(aes(x = School, y=Percent, fill = School), width = .6, stat = "identity")

关键是:

  1. 在与彩条相同的透明层之前添加另一个层,但不要填充或着色(使用NA
  2. 制作因子的数字版本(在我的例子中,我制作了sch,但没有上一步就无法工作
  3. 不要使用weight,而是使用y & stat = "identity"

【讨论】:

    【解决方案3】:

    哦,有人已经回答了这个问题,但无论如何这是我的。因为这里只是画图,所以可以使用geom_rect:

    xwidth <- 0.5
    xoffset <- 0.05
    yoffset <- 0.05
    
    my_dat <- data.frame(x=1:5, y=5:1, labels=letters[1:5])
    
    ggplot(my_dat) +
      geom_rect(aes(xmin=x+xoffset, xmax=x+xwidth+xoffset, 
                    ymin=0, ymax=y+yoffset), 
                fill='grey', alpha=0.8) +
    
      geom_rect(aes(xmin=x, xmax=x+xwidth, 
                    ymin=0, ymax=y, fill=labels)) +
    
      scale_x_discrete(labels=my_dat$labels, breaks=my_dat$x) +
      theme_bw()
    

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多