突出显示 ggplot2 中感兴趣的区域

发布于 2024-09-16 21:09:08 字数 381 浏览 0 评论 0原文

在普通绘图中,可以在 plotpanel.first 参数中使用 polygon 调用来突出显示背景区域。是否可以在 ggplot2 中执行相同的操作?可以在保留网格线的同时完成吗?

例如:

# plot hp and wt for mtcars data, highlighting region where hp/wt ratio < 35
with(mtcars,plot(hp,wt,
     panel.first=polygon(c(0,0,max(wt)*35),c(0,max(wt),max(wt)),
     col="#d8161688",border=NA)))

In vanilla plotting, it is possible to use a polygon call in the panel.first argument to plot to highlight a background region. Is it possible to do the same in ggplot2? Can it be done while preserving the gridlines?

eg:

# plot hp and wt for mtcars data, highlighting region where hp/wt ratio < 35
with(mtcars,plot(hp,wt,
     panel.first=polygon(c(0,0,max(wt)*35),c(0,max(wt),max(wt)),
     col="#d8161688",border=NA)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-09-23 21:09:08

是的,ggplot2 可以做到这一点。要保持网格线的可见性,您可以使用 Alpha 透明度。请注意,一般来说,几何图形和统计数据的应用顺序很重要。

tmp <- with(mtcars, data.frame(x=c(0, 0, max(wt)*35), y=c(0, max(wt), max(wt))))
ggplot(mtcars, aes(hp, wt)) + 
  geom_polygon(data=tmp, aes(x, y), fill="#d8161688") + 
  geom_point()

ggplot2 输出

Yes, that's possible with ggplot2. To preserve the visibility of grid lines you can use alpha transparency. Note that, in general, the order in which geoms and stats are applied matters.

tmp <- with(mtcars, data.frame(x=c(0, 0, max(wt)*35), y=c(0, max(wt), max(wt))))
ggplot(mtcars, aes(hp, wt)) + 
  geom_polygon(data=tmp, aes(x, y), fill="#d8161688") + 
  geom_point()

ggplot2 output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文