Add shadow effect ggplot2 bars (barplot)

I'm trying to show the worst design choices on the plots. One of the wasting ink is the potentially distracting effects that people use if the shadow effect is bars. I would like to do ggplot2 . The main one, although I had to make the first translucent layer of bars a little higher and moved to the right. I can get a little higher, but not a little 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()

enter image description here

This attempt gives the following warning and the transparent layer is ignored (which is reasonable):

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
+4
source share
3 answers

I think maybe this is what you are 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()

enter image description here

+4
source

Using what @joran gave me this work (thanks 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")

Key:

  • , , ( NA)
  • ( sch,
  • weight, y stat = "identity"

enter image description here

+2

Oh, someone already answered that, but here it’s all the same. Since you just draw the pictures here, you can use 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()

enter image description here

+2
source

All Articles