寫在前面。
在本文中將討論如何控制 ggplot2 圖形的整體外觀,例如字體、背景顏色等問題。
和數(shù)據(jù)所映射的圖形元素不同,主題系統(tǒng)為控制非數(shù)據(jù)元素的外觀提供了可能。
設(shè)置圖形的標(biāo)題
如何設(shè)置一幅圖形的標(biāo)題?
示例數(shù)據(jù)需要加載 gcookbook 包,其中的 heightweight 數(shù)據(jù)集:
> library(gcookbook)
> str(heightweight)
'data.frame': 236 obs. of 5 variables:
$ sex : Factor w/ 2 levels "f","m": 1 1 1 1 1 1 1 1 1 1 ...
$ ageYear : num 11.9 12.9 12.8 13.4 15.9 ...
$ ageMonth: int 143 155 153 161 191 171 185 142 160 140 ...
$ heightIn: num 56.3 62.3 63.3 59 62.5 62.5 59 56.5 62 53.8 ...
$ weightLb: num 85 105 108 92 112 ...
使用 ggtitle 設(shè)置標(biāo)題或者 labs進(jìn)行設(shè)置。
p <- ggplot(data = heightweight, aes(x = ageYear, y = heightIn)) + geom_point()
p + ggtitle("Age and Height \nof Schoolchildren")
[圖片上傳失敗...(image-2559fe-1699922015360)]
使用labs:
p <- ggplot(data = heightweight, aes(x = ageYear, y = heightIn)) + geom_point()
p + labs(title = "Age and Height of Schoolchildren")
[圖片上傳失敗...(image-a636b5-1699922015360)]
以上。