(轉(zhuǎn)帖)R語(yǔ)言-ggplot2筆記:主題設(shè)置、存儲(chǔ)導(dǎo)出

原文地址: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。

?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 簡(jiǎn)介 文章較長(zhǎng),點(diǎn)擊直達(dá)我的博客,瀏覽效果更好。本文內(nèi)容基本是來(lái)源于STHDA,這是一份十分詳細(xì)的ggplot2使...
    taoyan閱讀 51,691評(píng)論 7 159
  • 變量 自定義變量 PHP變量用$加變量名來(lái)表示,注意變量名區(qū)分大小寫(xiě),這意味著,如下兩個(gè)變量是不一樣的。 有效的變...
    波哥教你學(xué)閱讀 1,885評(píng)論 0 2
  • “今天真是倒霉的一天”——一年前的思維方式, 早起就看到給如意洗的衣服,老公居然沒(méi)晾。今天要帶的。 1、最近如意吃...
    如意媽花小美閱讀 457評(píng)論 2 1
  • 親愛(ài)的軒軒: 今天睡前爸爸給你講故事,所以媽媽沒(méi)有給你讀昨天寫(xiě)的情書(shū),明天補(bǔ)了一起吧! 早上是個(gè)下雨天,媽媽催促你...
    在寫(xiě)日志的眼袋妹閱讀 369評(píng)論 0 1
  • 在近一年的青椒計(jì)劃學(xué)習(xí)里,匆匆而過(guò)的學(xué)習(xí)留給了我滿滿的收獲和深深的思考。這次學(xué)習(xí),我聆聽(tīng)專家的講課,觀摩優(yōu)秀教師的...
    陜縣1643王曉麗閱讀 740評(píng)論 0 0

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