【问题标题】:Highlighting regions of interest in ggplot2在 ggplot2 中突出显示感兴趣的区域
【发布时间】:2011-04-06 08:01:32
【问题描述】:

在原版绘图中,可以在panel.first 参数中使用polygon 调用plot 来突出显示背景区域。是否可以在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)))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    是的,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()
    

    【讨论】:

    • rcs,这很酷,但你能解释一下为什么这种颜色是透明的吗?谢谢!
    • 因为 RGB 字符串中的最后 2 位数字(0=完全透明,FF=不透明,参见 ?rgb);也可以在geom_polygon 中使用alpha 美学
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2012-02-22
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多