原文地址:https://mp.weixin.qq.com/s/xg7cggq45tBI0EQUSxPKFw
8.1 Introduction
第八章講的是ggplot2的主題設(shè)置,通過(guò)它你可以對(duì)數(shù)據(jù)之外的圖形外觀進(jìn)行控制。第一版的中文版的把這一章的章節(jié)名翻譯為“精雕細(xì)琢”。
控制主題設(shè)置主要有以下四個(gè)方面:
主題元素,指的是非數(shù)據(jù)元素,plot.title控制標(biāo)題的外觀,axis.ticks.x控制x軸的刻度,legend.key.height控制圖例中按鍵的高度。
元素函數(shù),描述元素的視覺(jué)屬性,例如element text()可以設(shè)置字體大小、顏色和文本外觀如plot.title。
theme()函數(shù),用來(lái)覆蓋默認(rèn)的主題元素,如theme(plot.title=element text(colour="red"))
完整主題,如theme_grey(),用來(lái)把所有主題元素協(xié)調(diào)一致。
舉例,如下圖表,
(變量cty和hwy:城市和高速公路行駛記錄每加侖行駛的英里數(shù))
base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
?geom_jitter() +
?geom_abline(colour = "grey50", size = 2)
base
在這個(gè)圖的基礎(chǔ)上,我們想改進(jìn)軸和圖例的標(biāo)簽;添加標(biāo)題;調(diào)整顏色等,
通過(guò)第六章標(biāo)度相關(guān)的知識(shí)我們可以添加標(biāo)簽、修改標(biāo)度:
labelled <- base +
labs(
x = "City mileage/gallon",
y = "Highway mileage/gallon",
colour = "Cylinders",
title = "Highway and city mileage are highly correlated"
) +
scale_colour_brewer(type = "seq", palette = "Spectral")
labelled
下一步,如果你想改變整個(gè)風(fēng)格,修改標(biāo)度就不能滿足了,就要用的這一章的內(nèi)容
如修改背景顏色、圖例位置、移除次要網(wǎng)格線、改變字體大小
styled <- labelled +
theme_bw() +
theme(
plot.title = element_text(face = "bold", size = 12),
legend.background = element_rect(fill = "white", size = 4, colour = "white"),
legend.justification = c(0, 1),
legend.position = c(0, 1),
axis.ticks = element_line(colour = "grey70", size = 0.2),
panel.grid.major = element_line(colour = "grey70", size = 0.2),
panel.grid.minor = element_blank()
)
styled
8.2 Complete Themes
ggplot2有多個(gè)內(nèi)置主題。其中默認(rèn)主題是theme_grey(),淡灰色背景和白色網(wǎng)格線。除此之外還有很多其他主題
theme_bw(): 是theme_grey()的變體,白色背景和灰色網(wǎng)格線
theme_linedraw(): 白色背景黑色線條
theme_light(): 和theme_linedraw()很像,區(qū)別是線條為灰色
theme_dark():黑色背景的theme_light(),可以用來(lái)畫(huà)薄彩色線條
theme_minimal():簡(jiǎn)約主題
theme_classic(): 只有x、y軸沒(méi)有背景和網(wǎng)格線
theme_void(): 完全空白的主題
創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)據(jù)集作為例子,分別設(shè)置成以上七個(gè)主題:
df <- data.frame(x = 1:3, y = 1:3)
base <- ggplot(df, aes(x, y)) + geom_point()
base + theme_grey() + ggtitle("theme_grey()")
base + theme_bw() + ggtitle("theme_bw()")
base + theme_linedraw() + ggtitle("theme_linedraw()")
base + theme_light() + ggtitle("theme_light()")
base + theme_dark() + ggtitle("theme_dark()")
base + theme_minimal() + ggtitle("theme_minimal()")
base + theme_classic() + ggtitle("theme_classic()")
base + theme_void() + ggtitle("theme_void()")
除此之外,有一個(gè)專門(mén)的主題R包叫g(shù)gthemes(Jeffrey Arnold),里面有更多的選擇。
8.3 Modifying Theme Components
這一節(jié)講的是如何修改主題中的個(gè)別部分,使用相關(guān)的元素函數(shù)。
基本語(yǔ)法如:
plot + theme(element.name = element function())
元素函數(shù)有四種基本類型:字體(text)、線條(line)、矩形(rectangles)和空白(blank)。
element_text():修改圖標(biāo)題的位置和字體,包括family、face、colour、size、hjust、vjust、angle、lineheight這些參數(shù)
base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
?geom_jitter() +
?geom_abline(colour = "grey50", size = 2)
base_t <- base + labs(title = "This is a ggplot") + xlab(NULL) + ylab(NULL)
base_t + theme(plot.title = element_text(size = 16))
base_t + theme(plot.title = element_text(face = "bold", colour = "red"))
base_t + theme(plot.title = element_text(hjust = 1))
另外,margin()參數(shù)可以設(shè)置標(biāo)題和圖表之間的距離,默認(rèn)值是0,左右上下均可設(shè)置:
# The margins here look asymmetric because there are also plot margins
base_t + theme(plot.title = element_text(margin = margin()))
base_t + theme(plot.title = element_text(margin = margin(t = 10, b = 10)))
base_t + theme(axis.title.y = element_text(margin = margin(r = 10)))
element_line:修改網(wǎng)格線,顏色、粗細(xì)、虛實(shí)等,如colour,size以及l(fā)inetype
base + theme(panel.grid.major = element_line(colour = "black"))
base + theme(panel.grid.major = element_line(size = 2))
base + theme(panel.grid.major = element_line(linetype = "dotted"))
element_rect:添加矩形圖層,如fill(修改背景顏色),colour,size以及l(fā)inetype
base + theme(plot.background = element_rect(fill = "grey80", colour = NA))
base + theme(plot.background = element_rect(colour = "red", size = 2))
base + theme(panel.background = element_rect(fill = "linen"))
element_blank():清空畫(huà)板
base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
?geom_jitter() +
?geom_abline(colour = "grey50", size = 2)
base
last_plot() + theme(panel.grid.minor = element_blank())
last_plot() + theme(panel.grid.major = element_blank())
還有一些相關(guān)的細(xì)枝末節(jié)的設(shè)置,小白覺(jué)得這些設(shè)置相當(dāng)繁瑣,很容易就占用了大量的作圖時(shí)間,也不是重點(diǎn),所以在此就先跳過(guò)啦。
8.4 控制主題的各種元素
大概有40多個(gè)屬性可以控制主題。大致分為plot,axis,legend,panel和facet這幾大類。
下表總結(jié)了一下控制主題的主要元素:
下面從中選幾個(gè)比較重要的來(lái)舉幾個(gè)例子
當(dāng)橫軸刻度線標(biāo)簽名字過(guò)長(zhǎng),我們可以調(diào)整標(biāo)簽的角度和位置:
df <- data.frame(
?x = c("label", "a long label", "an even longer label"),
?y = 1:3
)
base <- ggplot(df, aes(x, y)) + geom_point()
base
## 調(diào)整角度-30°,中心垂直距離下移1
base +
?theme(axis.text.x = element_text(angle = -30, vjust = 1, hjust = 0)) +
?xlab(NULL) +
?ylab(NULL)
假如你需要調(diào)整圖形的長(zhǎng)寬比:
df2 <- data.frame(x = 1:4, y = 1:4, z = rep(c("a", "b"), each = 2))
base <- ggplot(df2, aes(x, y, colour = z)) + geom_point()
base2 <- base + theme(plot.background = element_rect(colour = "grey50"))
# Wide screen
base2 + theme(aspect.ratio = 9 / 16)
# Long and skiny
base2 + theme(aspect.ratio = 2 / 1)
# Square
base2 + theme(aspect.ratio = 1)
8.5 儲(chǔ)存和導(dǎo)出(Saving Your Output)
當(dāng)保存圖片時(shí),你有兩種基本選擇:矢量型圖片和柵格型圖片
矢量型:圖形可以無(wú)限縮放沒(méi)有細(xì)節(jié)的損失;但如果包含數(shù)千個(gè)對(duì)象,矢量渲染過(guò)程會(huì)很慢
柵格形:以像素陣列形式存儲(chǔ),有固定的最優(yōu)觀測(cè)大小,對(duì)于圖形印刷,分辨率(600dpi)是較好的方案。
ggplot2有兩種保存方法:
第一種:
pdf("output.pdf", width = 6, height = 6)
ggplot(mpg, aes(displ, cty)) + geom_point()
dev.off()
第二種使用ggsave:
ggplot(mpg, aes(displ, cty)) + geom_point()
ggsave("output.pdf")
顯然第二種方法更加方便簡(jiǎn)潔,不過(guò)我們需要設(shè)置以下參數(shù):
path設(shè)定圖形儲(chǔ)存路徑。ggsave()可以生成以下格式:.eps,.pdf,.svg,.wmf,.png,.jpg,.bmp, and.tiff.
width和height設(shè)置絕對(duì)尺寸的大小,可以精確控制尺寸
分辨率dpi默認(rèn)值300,你可以修改為600。