代码之家  ›  专栏  ›  技术社区  ›  James

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

  •  10
  • James  · 技术社区  · 14 年前

    在香草绘图中,可以使用 polygon panel.first 论据 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)))
    
    1 回复  |  直到 14 年前
        1
  •  18
  •   rcs    14 年前

    是的,这在ggplot2中是可能的。要保持栅格线的可见性,可以使用alpha透明度。注意,一般来说,geom和stats的应用顺序很重要。

    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