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