參考書《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