I haven't found anyone else with this issue. Here is my plot:
Like OP's case, we expect all rectangles to be the same alpha value, but they are not. Also, note the alpha value is ridiculously low (0.01
) for the color you see there. What's going on should be more obvious if we check number of observations in mtcars
that falls within each facet:
> library(dplyr)
> mtcars %>% group_by(cyl) %>% tally()
# A tibble: 3 x 2
cyl n
<dbl> <int>
1 4 11
2 6 7
3 8 14
There's a lower number of observations where cyl==6
and cyl==4
has lower observations than cyl==8
. This corresponds precisely to the alpha values we see for the geoms in the plot, so this is what's going on. For each observation, a rectangle is drawn over the same position and so there are 7 rectangles drawn in the middle facet, 14 on the right facet, and 11 on the left facet.
Fixing the Issue: Summarize the Data
To fix the issue, you should summarize your data and use the summarized dataset for plotting the rectangles.
summary_df <- df %>%
group_by(cyl) %>%
summarize(mean_d = mean(disp))
p geom_rect(
data = summary_df,
aes(x=1, y=1, xmin=mean_d-50, xmax=mean_d 50, ymin=-Inf, ymax=Inf),
alpha=0.2, fill='red')
Since summary_df
has only 3 observations (one for each group of cyl
), the rectangles are drawn correctly and now alpha=0.2
with fill="red"
gives the expected result. One thing to note here is that we still have to define x
and y
in the aes()
. I set them both to 1
because although geom_rect()
doesn't use them, ggplot2
still expects to find them in the dataset summary_df
because we stated that they are assigned to that plot globally up in ggplot(df, aes(x=..., y=...))
. The fix is to either move the aes()
declaration into geom_point()
or just assign both to be constant values in geom_rect()
.