如何突出显示时间范围? [英] How to highlight time ranges on a plot?
问题描述
我有一些随时间采样的信号,我使用连续线在R中绘制。另外,我想重点介绍剧情中的几个具体时间范围。
我目前的做法是绘制具有与时间范围相匹配的适当宽度和水平位置的全高透明矩形。我认为这是一个很好的表现形式,因为它明确地将范围内的点与范围外的点分开;但有没有更好的?
第二个问题的实际部分。现在我正在绘制这样的信号:
p < - ggplot(data = gs,mapping = aes(x = frameno,y = value,col = variable))+
geom_line()
p
我试图手动绘制一个alpha混合矩形:
p + geom_rect(aes(xmin = 600,xmax = 650 ,ymin = -3,ymax = 3),
color = alpha(grey20,0.5),fill.alpha = 0.5)
- 但迄今尚未成功。任何提示?
我认为绘制矩形工作正常,我不知道更好的解决方案,如果一条简单的垂直线或线是不够的。
只需使用 alpha = 0.5
而不是 fill.alpha = 0.5
用于透明度问题,同时指定 inherit.aes =
。例如。根据钻石数据绘制一张图: geom_rect()
中的= FALSE
p < - ggplot(钻石,aes(x =价格,y =克拉) )+
geom_line(aes(color = color))
rect < - data.frame(xmin = 5000,xmax = 10000,ymin = -Inf,ymax = Inf)
p + geom_rect(data = rect,aes(xmin = xmin,xmax = xmax,ymin = ymin,ymax = ymax),
color =grey20,
alpha = 0.5,
inherit.aes = FALSE)
还要注意 ymin
和<$ c $可轻松将c> ymax 设置为 -Inf
和 Inf
。
I have a few signals sampled over time which I plot in R using continuous lines. Additionally, I would like to highlight several specific time ranges on the plot.
My current approach is to draw full-height transparent rectangles with appropriate width and horizontal position which match the time range. I think this is a good representation, as it clearly separates points inside the range from those outside of it; but are there better ones?
And the second, practical part of the question. Now I'm plotting the signals like this:
p <- ggplot(data=gs, mapping=aes(x=frameno, y=value, col=variable)) +
geom_line()
p
I have tried to draw an alpha blended rectangle manually:
p + geom_rect(aes(xmin=600, xmax=650, ymin=-3, ymax=3),
colour=alpha("grey20", 0.5), fill.alpha=0.5)
— but no success so far. Any hints?
I think drawing rectangles just work fine, I have no idea about better solution, if a simple vertical line or lines are not enough.
And just use alpha=0.5
instead of fill.alpha=0.5
for the transparency issue also specifying inherit.aes = FALSE
in geom_rect()
. E.g. making a plot from the diamonds data:
p <- ggplot(diamonds, aes(x=price, y=carat)) +
geom_line(aes(color=color))
rect <- data.frame(xmin=5000, xmax=10000, ymin=-Inf, ymax=Inf)
p + geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
color="grey20",
alpha=0.5,
inherit.aes = FALSE)
Also note that ymin
and ymax
could be set to -Inf
and Inf
with ease.
这篇关于如何突出显示时间范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!