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

Tyl*_*ker 5 r ggplot2

我试图在绘图中显示较差的设计选择。如果出现阴影效果,则可能是人们浪费的一种墨水,可能会分散人们的注意力。我想让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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

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
Run Code Online (Sandbox Code Playgroud)

jor*_*ran 5

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

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Tyl*_*ker 5

使用@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")
Run Code Online (Sandbox Code Playgroud)

关键是:

  1. 在与颜色条相同的透明层之前添加另一个层,但不要填充或着色它们(使用NA
  2. 制作因子的数字版本(在我的情况下,我已经制作sch但没有上一步就不起作用
  3. 不要使用weight而是使用y&stat = "identity"

在此处输入图片说明


arv*_*000 5

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

在此处输入图片说明