ggplot2 - 两条垂直线之间的阴影区域 [英] ggplot2 - shade area between two vertical lines
问题描述
我使用 ggplot2
来创建一些相当简单的散点图。我现在有两条简单的垂直线:
... + geom_vline(xintercept = 159684.186,linetype =dotted,size = 0.6)+
geom_vline(xintercept = 159683.438,linetype =dotted,size = 0.6)+ ...
任何人都可以告诉我如何遮蔽从Y轴顶部到X轴这两条线之间的区域吗?
您可以使用 geom_rect
。
... + geom_rect(aes(xmin = 159683.438,xmax = 159684.186,ymin = 0,ymax = Inf))
x的两个值来自您的 geom_vline
调用。使用 ymin = 0
将其降为0; ymax = Inf
会一直走到轴的顶端。如果你希望它一直到X轴而不是0,你可以使用 ymin = -Inf
。
一些注释:
如果按照geoms的先后顺序,这样可以最好地将其绘制在其他部分的首位/下面(特别是散点图数据)。
您可以在 aes 之外设置填充颜色(
fill
/ code>调用一个固定值。我还会将透明度( alpha
)设置为0.5之类的东西,以便它后面的东西(很可能是网格线,如果你把它作为第一个几何图形)仍然可以被看到。
I am using ggplot2
to create some fairly simple scatter plots. I currently have two simple vertical lines using:
... + geom_vline(xintercept=159684.186,linetype="dotted",size=0.6)+
geom_vline(xintercept=159683.438,linetype="dotted",size=0.6)+ ...
Can anyone tell me how to shade the area between these two lines from the top of the Y axis to the X axis?
You can use geom_rect
.
... + geom_rect(aes(xmin=159683.438, xmax=159684.186, ymin=0, ymax=Inf))
The two values for x come from your geom_vline
calls. using ymin=0
takes it down to 0; ymax=Inf
will take it all the way to the top of the axis. If you want it to go all the way down to the x axis rather than 0, you can use ymin=-Inf
.
Some notes:
This works best if it is early in the order of geoms so that it gets drawn first/below the other parts (especially the scatterplot data).
You can set the fill color (fill
aesthetic) outside the aes
call to a fixed value. I would also set the transparency (alpha
) to something like 0.5 so that the stuff behind it (grid lines, most likely, if you put it as the first geom) can still be seen.
这篇关于ggplot2 - 两条垂直线之间的阴影区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!