1

Here is the data I am working with:

df= structure(list(effect = c(-0.183660494380325, -0.286711283030125, 
0.144420824373155, 0.107958002095135, -0.236028784063033, -0.160120624560128, 
-0.241734940173219, -0.301094909534684, 0.080319901278758, 0.166386486331364, 
2.18044670333557, 0.150214406648656, 0.101218219003822, -0.382893498692355, 
-0.131525337511054, -0.0963503379451771, -0.266851983630642, 
-0.237371391841929, -0.0718562471672148, -0.833148477215979, 
-0.31532073921026, -0.666010175452128, 0.128074208388128, 0.105220228400002, 
0.212888583788477, 0.173837214351567, 0.0723268793073809, 0.227657131980834, 
0.219566925261995, -0.0577587298548685), lower = c(-0.378799556647341, 
-0.758072446650849, 0.0273385116378299, -0.0239857116078122, 
-0.595394742028366, -0.392378190445114, -1.07812282304258, -0.870284236088555, 
-0.117427834790597, 0.0202865790073198, -0.0309968796929358, 
-0.115160638753749, -0.0518695893701463, -1.77050833334992, -0.413234955839275, 
-0.715635813448282, -0.731134011308218, -0.688660305462684, -0.743460375678667, 
-1.87822726521072, -1.69792593382383, -2.31272677544801, -0.028273151895772, 
-0.247721998059409, -0.0799161658359572, -0.110588127178339, 
-0.114331814482118, -0.127583669808567, -0.114524635314751, -0.393815864368995
), upper = c(0.0119292041841525, 0.188761936680526, 0.261375601855666, 
0.239780243784321, 0.125309603839398, 0.0726943910130132, 0.606383207664197, 
0.274457379488013, 0.277860600000933, 0.31225850014966, 4.28541221179007, 
0.414903371934528, 0.254152061126853, 1.06207219690377, 0.150862559742933, 
0.525447262952953, 0.201162598075368, 0.217070141774486, 0.601971540143289, 
0.260535057064083, 1.11526641785088, 1.10904764279685, 0.284217288085084, 
0.457299344432491, 0.504523841925775, 0.45735105097433, 0.258818976847914, 
0.58105825758076, 0.552088686400803, 0.278729034684897), Subject = structure(c(1L, 
2L, 3L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("BIOL112_2019W1", 
"CHEM100_2019W1", "CHEM121_2019W1", "PHYS100_2019W1"), class = "factor"), 
    item = c("Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", 
    "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", 
    "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", 
    "Anx", "Anx", "Anx", "Anx", "Anx"), Dem = c("All", "All", 
    "All", "All", "Man", "Woman", "Man", "Woman", "Man", "Woman", 
    "Non-binary", "Man", "Woman", "Non-binary", "On-campus", 
    "<30 min", "<60 min", ">60 min", "On-campus", "<30 min", 
    "<60 min", ">60 min", "On-campus", "<30 min", "<60 min", 
    ">60 min", "On-campus", "<30 min", "<60 min", ">60 min"), 
    cat = c("Class", "Class", "Class", "Class", "Gender", "Gender", 
    "Gender", "Gender", "Gender", "Gender", "Gender", "Gender", 
    "Gender", "Gender", "Commute", "Commute", "Commute", "Commute", 
    "Commute", "Commute", "Commute", "Commute", "Commute", "Commute", 
    "Commute", "Commute", "Commute", "Commute", "Commute", "Commute"
    )), row.names = c(NA, -30L), class = "data.frame")

When I plot a facet_grid ggplot object and add rectangles to the plot with alpha =0.2, when faceting my rectangles have different alpha values. And I don't understand why:

ggplot(df, aes(x=Dem) )+
      theme_bw()+
      geom_rect(aes(ymin = 0,ymax = 0.2,xmin = -Inf, xmax = Inf, fill = 'negligible'), alpha = .2)+
      geom_rect(aes(ymin = 0.2,ymax = 0.5, xmin = -Inf, xmax = Inf, fill = 'small'), alpha = .2)+
      geom_rect(aes(ymin = 0.5, ymax = 0.8,xmin = -Inf, xmax = Inf,fill = 'medium'), alpha = .2)+
       geom_rect(aes(ymin = 0.8, ymax = Inf,xmin = -Inf, xmax = Inf,fill = 'large'), alpha = .2)+
      geom_rect(aes(ymin = 0,ymax = -0.2,xmin = -Inf, xmax = Inf, fill = 'negligible'), alpha = .2)+
      geom_rect(aes(ymin = -0.2,ymax = -0.5, xmin = -Inf, xmax = Inf, fill = 'small'), alpha = .2)+
      geom_rect(aes(ymin = -0.5, ymax = -0.8,xmin = -Inf, xmax = Inf,fill = 'medium'), alpha = .2)+
       geom_rect(aes(ymin = -0.8, ymax = -Inf,xmin = -Inf, xmax = Inf,fill = 'large'), alpha = .2)+
         geom_point(aes(y=effect),stat="identity")+
      #scale_x_discrete(position = "top") +
      geom_errorbar(aes(ymin=lower, ymax=upper), width=.2)+
      facet_grid(Subject~cat,scales="free")+ 
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

I would like to have all rectangles having a consistent alpha of 0.2, so they all look same for each facet plot

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
yuliaUU
  • 1,581
  • 2
  • 12
  • 33

1 Answers1

1

The issue is overplotting. The way you added the geom_rect means that a rectangle is drawn for each (!!) observation or row of your data, i.e. multiple rects are plotted on top of each other. As the number of observations varies by facet

  1. the number of rects drawn per facet varies
  2. you get a different shading per facet, i.e. the more observations the darker is the shading.

To solve your issue make a data frame with the coordinates of the rects which also allows to add them via one geom_rect.

library(ggplot2)

d_rect <- data.frame(
  ymin = c(0, .2, .5, .8, 0, -.2, -.5, -.8),
  ymax = c(.2, .5, .8, Inf, -.2, -.5, -.8, -Inf),
  xmin = -Inf,
  xmax = Inf,
  fill = rep(c("negligible", "small", "medium", "large"), 4)
)
ggplot(df, aes(x = Dem)) +
  theme_bw() +
  geom_rect(data = d_rect, aes(ymin = ymin, ymax = ymax, xmin = xmin, xmax = xmax, fill = fill), alpha = .2, inherit.aes = FALSE) +
  geom_point(aes(y = effect), stat = "identity") +
  # scale_x_discrete(position = "top") +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = .2) +
  facet_grid(Subject ~ cat, scales = "free") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Created on 2021-06-07 by the reprex package (v2.0.0)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • thank you for your detailed answer! I did not know about the overplotting. Thanks for teh detailed explanations!. It makes sense now! – yuliaUU Jun 07 '21 at 14:12