ggplot2 -两个垂直线条之间的阴影区域[重复]

时间:2023-02-09 16:32:39

This question already has an answer here:

这个问题已经有了答案:

I am using ggplot2 to create some fairly simple scatter plots. I currently have two simple vertical lines using:

我使用ggplot2来创建一些相当简单的散点图。我目前有两条简单的垂直线:

... + 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?

谁能告诉我怎么把这两条线之间的面积从Y轴的顶端投影到X轴上?

2 个解决方案

#1


40  

You can use geom_rect.

您可以使用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.

x的两个值来自geom_vline调用。用ymin=0把它降到0;ymax=Inf会一直到轴的顶端。如果你想让它一直到x轴而不是0,你可以用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).

如果它是在geoms的早期,所以它首先/在其他部分(特别是散点数据)下面绘制,则效果最好。

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.

您可以将填充颜色(填充美学)设置为一个固定值。我还将透明度(alpha)设置为0.5,这样它后面的东西(网格线,如果你把它作为第一个geom,很可能)仍然可以看到。

#2


14  

It might be even easier to use annotate() for this if you know the coordinates for which region you want to shade. I had some strange rendering problems when I tried to use geom_rect().

如果您知道要阴影的区域的坐标,那么使用annotate()可能会更容易。我在尝试使用geom_rect()时遇到了一些奇怪的渲染问题。

library(ggplot2)
data(mtcars)

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
     annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21,
        alpha = .2)

I know it's essentially the same thing; I just happened stumbled on this tidbit from here.

我知道本质上是一样的;我碰巧在这里发现了这个珍闻。

#1


40  

You can use geom_rect.

您可以使用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.

x的两个值来自geom_vline调用。用ymin=0把它降到0;ymax=Inf会一直到轴的顶端。如果你想让它一直到x轴而不是0,你可以用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).

如果它是在geoms的早期,所以它首先/在其他部分(特别是散点数据)下面绘制,则效果最好。

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.

您可以将填充颜色(填充美学)设置为一个固定值。我还将透明度(alpha)设置为0.5,这样它后面的东西(网格线,如果你把它作为第一个geom,很可能)仍然可以看到。

#2


14  

It might be even easier to use annotate() for this if you know the coordinates for which region you want to shade. I had some strange rendering problems when I tried to use geom_rect().

如果您知道要阴影的区域的坐标,那么使用annotate()可能会更容易。我在尝试使用geom_rect()时遇到了一些奇怪的渲染问题。

library(ggplot2)
data(mtcars)

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
     annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21,
        alpha = .2)

I know it's essentially the same thing; I just happened stumbled on this tidbit from here.

我知道本质上是一样的;我碰巧在这里发现了这个珍闻。