ggplot2:如何在阴谋中透明地遮阴 [英] ggplot2: how to transparently shade alternate days on a plot
问题描述
我在这里做错了什么?
我试图用透明灰色替代24小时每日矩形。但是只有for-loop的最后一个矩形才会被绘制(?!?)如果我手动执行而不是通过for-loop,它可以正常工作。
是否有矢量化这个方法来避免for循环?
(可以用qplot来完成吗?)
我是ggplot2的新手,是的,我通过Hadley的网站,书籍和例子阅读。
第二个问题:审美上的阿尔法设置不会阻止矩形遮挡背景。如何获得透明度?
dat < - data.frame(my_x_series = 1:192,my_y_series = 5.0 * runif(192))$ b $使用最小/最大(na.rm == TRUE)为该系列计算b#(ymin,ymax))
$ p $为了绘制您的矩形,创建一个数据框,其中每行包含一个矩形的坐标。这个构造适用于所有的多边形,而不仅仅是矩形。一旦你知道了这一点,很容易避免循环。
ymax < - 5.0
ymin < - 0.0
p < - ggplot (alternate_daily_bars_xmin中的shade_xmin){
shade_xmax <-c(4,52,100,148)
(dat_aes(x = my_x_series,alpha = 0.9))
alternate_daily_bars_xmin < (aes(alpha = 0.5,xmin = shade_xmin,xmax = shade_xmax,ymin = ymin,ymax = ymax)中的最小值(shade_xmin + 24,192)#clamp#xb_p < ,fill ='gray80')
}
p < - p + geom_point(aes(y = my_y_series))
p
然后,小心你是否将变量映射到审美。在你的情况下,你需要设置
alpha
为你希望的值,所以它不构成aes()$ c $的一部分($ g $ p
$ data $ lt; 1:192,my_y_series = 5.0 * runif(192))
rect_left <-c(4,52,100,148)
矩形< - 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))
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.
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.
Second issue: the alpha setting on the aesthetic doesn't prevent the rectangles occluding the background. How to get transparency?
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
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.
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))
这篇关于ggplot2:如何在阴谋中透明地遮阴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!