兩幅圖疊加_ggplot2

參考書《R數(shù)據(jù)科學》

ggplot2支持圖層疊加,可以直接添加多個幾何對象函數(shù)

舉例:疊加散點圖和平滑曲線圖

#第一種方法
ggplot(data = mpg)+
      geom_point(mapping = aes(x = displ, y = hwy, color = drv)) +
      geom_smooth(mapping = aes(x = displ, y = hwy, color = drv))
      
#第二種方法      
ggplot(data = mpg,mapping = aes(x = displ, y = hwy, color = drv)) +
      geom_point() +
      geom_smooth()
圖片
#寫在幾何對象函數(shù)里的參數(shù)僅對該幾何對象所在圖層有效

#寫在ggplot()函數(shù)里的參數(shù)會被用做全局映射

ggplot(data = mpg,mapping = aes(x = displ, y = hwy))+
      geom_point(mapping = aes(color = drv))+
      geom_smooth()
圖片

geom_smooth() 函數(shù)中的局部數(shù)據(jù)參數(shù)會覆蓋ggplot() 函數(shù)中的

全局數(shù)據(jù)參數(shù),僅對當前圖層有效

library(dplyr)

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
      geom_point(mapping = aes(color = class)) +
      geom_smooth(
          data = filter(mpg, class == "subcompact"),
          se = FALSE  #這里“se”代表標準誤
      )
圖片

根據(jù)實際需要繪制合適的圖,盡量做到簡潔全面,至少不凌亂

轉(zhuǎn)載來自:https://mp.weixin.qq.com/s/aHKH8DbQ8DqJ6OxytF4h1Q

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容