兩幅圖疊加_ggplot2

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

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

舉例:疊加散點(diǎn)圖和平滑曲線圖

#第一種方法
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()
圖片
#寫在幾何對(duì)象函數(shù)里的參數(shù)僅對(duì)該幾何對(duì)象所在圖層有效

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

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

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

全局?jǐn)?shù)據(jù)參數(shù),僅對(duì)當(dāng)前圖層有效

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”代表標(biāo)準(zhǔn)誤
      )
圖片

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

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

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

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

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