我试图在一个多面图上得到部分透明的矩形。下面的例子说明了我所要做的,除了alpha
参数不起作用:
library(dplyr)
library(ggplot2)
df <- iris %>%
filter(Species == "setosa" | Species == "versicolor") %>%
group_by(Species) %>%
mutate(plq = cut(Petal.Length,
quantile(Petal.Length,
probs = seq(0, 1, 0.5)),
labels = seq(1:(length(seq(0, 1, 0.5)) - 1)),
include.lowest = TRUE)) %>%
ungroup() %>%
group_by(Species, plq) %>%
mutate(max = max(Petal.Length),
min = min(Petal.Length)) %>%
ungroup() %>%
mutate(min = if_else(plq == 2,
min, NA),
max = if_else(plq == 2,
max, NA))
df %>%
ggplot(aes(x = Petal.Length)) +
stat_density(geom = "line", position = "identity", adjust = 3) +
geom_rect(aes(xmin = min, xmax = max, ymin = -Inf, ymax = Inf), alpha = 0.2) +
facet_wrap(vars(Species)) +
theme_classic()
除了alpha
参数不起作用之外,这个图正是我想要的。我找到了其他几个相关的问答(here和here),但我不认为其中任何一个直接适用于我的问题。
1条答案
按热度按时间a14dhokn1#