有没有办法对其进行矢量化以避免 for 循环? (可以用 qplot 来完成吗?) 我是 ggplot2 的新手,是的,我阅读了 Hadley 的网站、书籍和示例。
第二个问题:美观上的 alpha 设置并不能阻止矩形遮挡背景。如何获得透明度?
dat <- data.frame(my_x_series=1:192, my_y_series=5.0*runif(192))
# (ymin, ymax are computed for this series using min/max(na.rm==TRUE))
ymax <- 5.0
ymin <- 0.0
p <- ggplot(dat, aes(x=my_x_series,alpha=0.9))
alternate_daily_bars_xmin <- c(4,52,100,148)
for (shade_xmin in alternate_daily_bars_xmin) {
shade_xmax <- min(shade_xmin+24, 192) # clamp at end of x-range
p <- p + geom_rect(aes(alpha=0.5,xmin=shade_xmin,xmax=shade_xmax,ymin=ymin,ymax=ymax), fill='gray80')
}
p <- p + geom_point(aes(y=my_y_series))
p
然后,请小心是否将变量映射到美学。在您的情况下,您需要将
alpha
设置为您想要的任何值,因此它不构成
aes()
设置的一部分。
library(ggplot2)
dat <- data.frame(my_x_series=1:192, my_y_series=5.0*runif(192))
rect_left <- c(4,52,100,148)
rectangles <- data.frame(
xmin = rect_left,
xmax = rect_left + 24,
ymin = 0,
ymax = 5
)
ggplot() +
geom_rect(data=rectangles, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
fill='gray80', alpha=0.8) +
geom_point(data=dat, aes(x=my_x_series, y=my_y_series))