[英]How can have different ggplot2 backgrounds for different regions in a plot
I have this empty plot defined by two lineal equations defining three regions in a plot.我有一个由两个线性方程定义的空图,这些方程定义了图中的三个区域。
lin.func <- function(int, slope, x) {
y = int + slope*x
}
ggplot(data.frame(x = c(32, 40)), aes(x)) +
stat_function(fun = lin.func, args = list(int = 78.8, slope = -1.76),
colour="black", size = 1) +
stat_function(fun = lin.func, args = list(int = 105.3, slope = -2.38),
colour="black", size = 1)+
theme_bw() +
scale_x_continuous(limits=c(28,44), breaks = c(seq(min(28),max(44), length.out = 5))) +
scale_y_continuous(limits=c(10,25), breaks = c(seq(min(10),max(25), length.out = 4)))
The plot looks like this:情节是这样的:
What I need is top have a green background in the central diagonal strip, a red in the leftmost region, and blue on the rightmost region.我需要的是顶部的中央对角线带绿色背景,最左侧区域为红色,最右侧区域为蓝色。
Is that possible to do?那有可能吗? I was looking around and could find any solution.
我环顾四周,可以找到任何解决方案。
Another question would be how to make the axis start right at the numbers I set in scale_x_continuous
and not a little bit less and far than that..另一个问题是如何使轴从我在
scale_x_continuous
设置的数字开始,而不是比那个少一点点。
Thanks so much in advance非常感谢提前
I have to do this from time to time and it can be somewhat tedious, but the best way I've found is to define polygons.我必须不时这样做,这可能有点乏味,但我发现的最好方法是定义多边形。 In This case, you would need to define three polygons, each identified by a unique id.
在这种情况下,您需要定义三个多边形,每个多边形都由一个唯一的 id 标识。 See my example of the first polygon below.
请参阅我下面第一个多边形的示例。 If you need to do this multiple times, it may be easier to define a function that creates polygons based on their vertices.
如果您需要多次执行此操作,则定义一个基于顶点创建多边形的函数可能会更容易。 I also switched to
coord_cartesian
because it allows you to define the polygons based on points that are not visible on the plot.我还切换到
coord_cartesian
因为它允许您根据绘图上不可见的点定义多边形。 For the middle background, I would suggest defining it by three points: the intersection of the two lines and two other points, each far off your graph on one line.对于中间背景,我建议用三个点来定义它:两条线和另外两个点的交点,每个点都在一条线上远离图形。 I hope this gives you some kind of start.
我希望这能给你一些开始。 Good luck.
祝你好运。
polygons <- data.frame(x= c(10, 10, 20, 40), y = c(5, 25, 78.8-1.76*c(20,40)), group = c(rep(1,4)))
ggplot(data.frame(x = c(32, 40)), aes(x)) +
geom_polygon(data = polygons, aes(x = x, y=y, group = group, fill= factor(group))) +
stat_function(fun = lin.func, args = list(int = 78.8, slope = -1.76),
colour="black", size = 1) +
stat_function(fun = lin.func, args = list(int = 105.3, slope = -2.38),
colour="black", size = 1)+
theme_bw() +
coord_cartesian(x = c(28,44), y = c(10,25))
If you want to colour the area to the left, between and to the right of two curves (or two lines, as in your case), you can use geom_ribbon with properly set xmin, xmax, ymin and ymax columns.如果您想为两条曲线(或两条线,如您的情况)的左侧、之间和右侧的区域着色,您可以使用 geom_ribbon 并正确设置 xmin、xmax、ymin 和 ymax 列。 Add to that a column with a factor of three levels (left, between, right) and set the aesthetics to colour by that column.
添加一个因子为三个级别(左、中间、右)的列,并将美学设置为该列的颜色。
There are several ways to define the limits, some more efficient than others, but I think drawing rectangles in the background is probably easiest?
有几种方法可以定义限制,有些方法比其他方法更有效,但我认为在后台绘制矩形可能最简单?
To remove the padding around the edges, use expand = c(0,0)
要删除边缘周围的填充,请使用
expand = c(0,0)
lin.func <- function(int, slope, x) {
y = int + slope*x
}
miny = 10
maxy = 25
x1 = c(28,31)
x2 = c(31,40)
x3 = c(40,44)
ggplot(data.frame(x = c(32, 40)), aes(x)) +
geom_rect(aes(xmin=x1[1], xmax=x1[2], ymin=miny, ymax=maxy), fill='#BB0000', color=NA, alpha=0.2) +
geom_rect(aes(xmin=x2[1], xmax=x2[2], ymin=miny, ymax=maxy), fill='#00BB00', color=NA, alpha=0.2) +
geom_rect(aes(xmin=x3[1], xmax=x3[2], ymin=miny, ymax=maxy), fill='#0000BB', color=NA, alpha=0.2) +
stat_function(fun = lin.func, args = list(int = 78.8, slope = -1.76),
colour="black", size = 1) +
stat_function(fun = lin.func, args = list(int = 105.3, slope = -2.38),
colour="black", size = 1)+
theme_bw() +
scale_x_continuous(expand = c(0, 0),limits=c(28,44), breaks = c(seq(min(28),max(44), length.out = 5))) +
scale_y_continuous(expand = c(0, 0),limits=c(miny,maxy), breaks = c(seq(min(miny),max(maxy), length.out = 4)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.