What am I doing wrong here please? I'm trying to shade alternate 24-hr daily rectangles with transparent gray. But only the last rectangle from the for-loop gets drawn(?!?) If I do things manually instead of by for-loop it works fine.
我在这里做错了什么?我试着用透明的灰色来遮挡24小时不间断的矩形。但是只有for循环的最后一个矩形被绘制(?!?)如果我用手工而不是for循环来做事情,它会很好地工作。
Is there a way to vectorize this to avoid the for-loop? (And can it be done with qplot?) I'm new to ggplot2 and yes I read through Hadley's site, book and examples.
是否有一种方法来向量化它以避免for循环?(qplot可以实现吗?)我刚接触ggplot2,是的,我读了哈德利的网站,书和例子。
Second issue: the alpha setting on the aesthetic doesn't prevent the rectangles occluding the background. How to get transparency?
第二个问题:在美学上的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
1 个解决方案
#1
26
To plot your rectangles, create a data frame where each row contains the coordinates for a single rectangle. This construct works for all polygons, not just rectangles. Once you know this, it's easy to avoid the loop.
要绘制矩形,请创建一个数据框架,其中每行包含单个矩形的坐标。这个构造适用于所有的多边形,而不仅仅是矩形。一旦知道了这个,就很容易避免循环。
Then, just be careful whether you map a variable to an aesthetic or not. In your case, you need to set alpha
to whatever value you wish, so it does not form part of the aes()
settings.
然后,无论是否将变量映射到美学上,都要小心。在您的示例中,您需要将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))
#1
26
To plot your rectangles, create a data frame where each row contains the coordinates for a single rectangle. This construct works for all polygons, not just rectangles. Once you know this, it's easy to avoid the loop.
要绘制矩形,请创建一个数据框架,其中每行包含单个矩形的坐标。这个构造适用于所有的多边形,而不仅仅是矩形。一旦知道了这个,就很容易避免循环。
Then, just be careful whether you map a variable to an aesthetic or not. In your case, you need to set alpha
to whatever value you wish, so it does not form part of the aes()
settings.
然后,无论是否将变量映射到美学上,都要小心。在您的示例中,您需要将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))