简体   繁体   English

添加阴影效果ggplot2条形图(barplot)

[英]Add shadow effect ggplot2 bars (barplot)

I'm attempting to show poorer design choices in plots.我试图在图中显示较差的设计选择。 One of the ink wasting, potentially distracting effects people use if a shadow effect of bars.如果条形的阴影效果,人们使用的一种墨水浪费,可能会分散人们的注意力。 I'd like to make ggplot2 do this.我想让ggplot2做到这一点。 The basic thought I had was to make a first semitransparent layer of bars slightly higher and shifted to the right.我的基本想法是将第一个半透明的条形层稍微高一点并向右移动。 I can get the slightly higher but not the slightly to the right:我可以得到稍高但不能稍微偏右的:

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

在此处输入图片说明

This attempt gives the following warning and that transparent layer is ignored (which is sensible):此尝试给出以下警告,并且忽略透明层(这是明智的):

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
   

I think maybe this is what you're looking for...?我想也许这就是你正在寻找的......?

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

在此处输入图片说明

Using what @joran gave me this works (Thanks Joran):使用@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")

The key is:关键是:

  1. Add another layer before the transparent layer that is identical to the color bars, but don't fill or color them (use NA )在与颜色条相同的透明层之前添加另一个层,但不要填充或着色它们(使用NA
  2. Make a numeric version of the factor (in my case I had made sch but didn't work without previous step制作该因子的数字版本(在我的情况下,我制作了sch但没有上一步就无法工作
  3. Don't use weight but instead use y & stat = "identity"不要使用weight ,而是使用y & stat = "identity"

在此处输入图片说明

Oh someone already answered this, but here's mine anyway.哦,有人已经回答了这个问题,但无论如何这是我的。 Since you are just drawing pictures here, you could use geom_rect:既然你只是在这里画图,你可以使用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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM