添加阴影效果 ggplot2 bars (barplot)

Add shadow effect ggplot2 bars (barplot)

我试图在绘图中展示较差的设计选择。如果条形的阴影效果,人们使用的一种墨水浪费,可能会分散注意力。我想让 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
   

我想也许这就是您要找的...?

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()

使用@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"

哦,有人已经回答了这个问题,但无论如何这是我的。由于你只是在这里画图,你可以使用 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()