ggplot2 - 线上方的阴影区域
我有一些数据限制在 1:1 线以下。我将通过轻轻地对线上方的区域进行阴影来在绘图上演示这一点,以吸引观众的注意力到线下方的区域。
我正在使用 qplot 来生成图表。很快,我就有了;
qplot(x,y)+geom_abline(slope=1)
但对于我来说,无法弄清楚如何在不绘制单独对象的情况下轻松地对上述区域进行着色。有一个简单的解决办法吗?
编辑
好的,Joran,这是一个示例数据集:
df=data.frame(x=runif(6,-2,2),y=runif(6,-2,2),
var1=rep(c("A","B"),3),var2=rep(c("C","D"),3))
df_poly=data.frame(x=c(-Inf, Inf, -Inf),y=c(-Inf, Inf, Inf))
这是我用来绘制它的代码(我听取了你的建议并一直在查找ggplot()):
ggplot(df,aes(x,y,color=var1))+
facet_wrap(~var2)+
geom_abline(slope=1,intercept=0,lwd=0.5)+
geom_point(size=3)+
scale_color_manual(values=c("red","blue"))+
geom_polygon(data=df_poly,aes(x,y),fill="blue",alpha=0.2)
被踢回的错误是:“未找到对象'var1'”有些东西告诉我,我错误地实现了参数......
I have some data that is constrained below a 1:1 line. I would to demonstrate this on a plot by lightly shading the area ABOVE the line, to draw the attention of the viewer to the area beneath the line.
I'm using qplot
to generate the graphs. Quickly, I have;
qplot(x,y)+geom_abline(slope=1)
but for the life of me, can't figure out how to easily shade the above area without plotting a separate object. Is there an easy fix for this?
EDIT
Ok, Joran, here is an example data set:
df=data.frame(x=runif(6,-2,2),y=runif(6,-2,2),
var1=rep(c("A","B"),3),var2=rep(c("C","D"),3))
df_poly=data.frame(x=c(-Inf, Inf, -Inf),y=c(-Inf, Inf, Inf))
and here is the code that I'm using to plot it (I took your advice and have been looking up ggplot()
):
ggplot(df,aes(x,y,color=var1))+
facet_wrap(~var2)+
geom_abline(slope=1,intercept=0,lwd=0.5)+
geom_point(size=3)+
scale_color_manual(values=c("red","blue"))+
geom_polygon(data=df_poly,aes(x,y),fill="blue",alpha=0.2)
The error kicked back is: "object 'var1' not found" Something tells me that I'm implementing the argument incorrectly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以@Andrie的答案为基础,这是一个更通用(但不完全)的解决方案,在大多数情况下处理给定线上方或下方的阴影。
我没有使用@Andrie此处引用的方法 因为我遇到了 ggplot 的问题,当您在边缘附近添加点时,ggplot 会自动扩展绘图范围。相反,这会根据需要使用
Inf
和-Inf
手动构建多边形点。一些注意事项:数据框中的点必须处于“正确”的顺序,因为 ggplot 按照点出现的顺序绘制多边形。因此,获取多边形的顶点是不够的,它们还必须按顺序排列(顺时针或逆时针)。
此解决方案假设您正在绘制的线本身不会导致 ggplot 扩展绘图范围。您将在我的示例中看到,我通过随机选择数据中的两个点并通过它们绘制线条来选择要绘制的线条。如果您尝试绘制一条距离其他点太远的线,ggplot 将自动更改绘图范围,并且很难预测它们会是什么。
首先,这是构建多边形数据框的函数:
它需要数据的 x 和 y 范围(如
range()
中所示)、要绘制的线的斜率和截距,以及是否要在线上方或下方进行阴影处理。这是我用来生成以下四个示例的代码:以下是结果的一些示例。当然,如果您发现任何错误,请告诉我,以便我更新此答案...
编辑
更新以使用OP的示例数据说明解决方案
:产生以下输出:
Building on @Andrie's answer here is a more (but not completely) general solution that handles shading above or below a given line in most cases.
I did not use the method that @Andrie referenced here since I ran into issues with
ggplot
's tendency to automatically extend the plot extents when you add points near the edges. Instead, this builds the polygon points manually usingInf
and-Inf
as needed. A few notes:The points have to be in the 'correct' order in the data frame, since
ggplot
plots the polygon in the order that the points appear. So it's not enough to get the vertices of the polygon, they must be ordered (either clockwise or counterclockwise) as well.This solution assumes that the line you are plotting does not itself cause
ggplot
to extend the plot range. You'll see in my example that I pick a line to draw by randomly choosing two points in the data and drawing the line through them. If you try to draw a line too far away from the rest of you points,ggplot
will automatically alter the plot ranges, and it becomes hard to predict what they will be.First, here's the function that builds the polygon data frame:
It expects the x and y ranges of your data (as in
range()
), the slope and intercept of the line you are going to plot, and whether you want to shade above or below the line. Here's the code I used to generate the following four examples:And here are some examples of the results. If you find any bugs, of course, let me know so that I can update this answer...
EDIT
Updated to illustrate solution using OP's example data:
and this produces the following output:
据我所知,除了创建具有 alpha 混合填充的多边形之外,没有其他方法。例如:
As far as I know there is no other way other than creating a polygon with alpha-blended fill. For example:
实现此目的的一种简单方法是使用
geom_ribbon
,并将ymax
值设置为Inf
,并将ymin
值设置为Inf
通过stat_function
计算:由 reprex 包 (v2.0.1) 创建于 2022 年 5 月 26 日< /sup>
One easy way to do this is to use
geom_ribbon
with theymax
value set toInf
, and theymin
value calculated bystat_function
:Created on 2022-05-26 by the reprex package (v2.0.1)
基于@joran的答案的最小修改版本:
您还可以扩展 ggplot 像这样:
能够像使用它一样轻松地使用它geom_abline:
此变体还有一个额外的优点,即它还可以处理多个斜率和非默认极限扩展。
Based on a minimally modified version of @joran's answer:
you can also extend ggplot like this:
To be able to use it as easily as a geom_abline:
This variant has the additional advantage that it also works with multiple slopes and non-default limit expansions.