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

原文地址:https://mp.weixin.qq.com/s/xg7cggq45tBI0EQUSxPKFw

8.1 Introduction

第八章講的是ggplot2的主題設(shè)置,通過它你可以對數(shù)據(jù)之外的圖形外觀進行控制。第一版的中文版的把這一章的章節(jié)名翻譯為“精雕細琢”。

控制主題設(shè)置主要有以下四個方面:

主題元素,指的是非數(shù)據(jù)元素,plot.title控制標(biāo)題的外觀,axis.ticks.x控制x軸的刻度,legend.key.height控制圖例中按鍵的高度。

元素函數(shù),描述元素的視覺屬性,例如element text()可以設(shè)置字體大小、顏色和文本外觀如plot.title。

theme()函數(shù),用來覆蓋默認的主題元素,如theme(plot.title=element text(colour="red"))

完整主題,如theme_grey(),用來把所有主題元素協(xié)調(diào)一致。

舉例,如下圖表,

(變量cty和hwy:城市和高速公路行駛記錄每加侖行駛的英里數(shù))

base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +

?geom_jitter() +

?geom_abline(colour = "grey50", size = 2)

base

在這個圖的基礎(chǔ)上,我們想改進軸和圖例的標(biāo)簽;添加標(biāo)題;調(diào)整顏色等,

通過第六章標(biāo)度相關(guān)的知識我們可以添加標(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

下一步,如果你想改變整個風(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有多個內(nèi)置主題。其中默認主題是theme_grey(),淡灰色背景和白色網(wǎng)格線。除此之外還有很多其他主題

theme_bw(): 是theme_grey()的變體,白色背景和灰色網(wǎng)格線

theme_linedraw(): 白色背景黑色線條

theme_light(): 和theme_linedraw()很像,區(qū)別是線條為灰色

theme_dark():黑色背景的theme_light(),可以用來畫薄彩色線條

theme_minimal():簡約主題

theme_classic(): 只有x、y軸沒有背景和網(wǎng)格線

theme_void(): 完全空白的主題

創(chuàng)建一個簡單的數(shù)據(jù)集作為例子,分別設(shè)置成以上七個主題:

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()")

除此之外,有一個專門的主題R包叫g(shù)gthemes(Jeffrey Arnold),里面有更多的選擇。

8.3 Modifying Theme Components

這一節(jié)講的是如何修改主題中的個別部分,使用相關(guān)的元素函數(shù)。

基本語法如:

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)題和圖表之間的距離,默認值是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)格線,顏色、粗細、虛實等,如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():清空畫板

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)的細枝末節(jié)的設(shè)置,小白覺得這些設(shè)置相當(dāng)繁瑣,很容易就占用了大量的作圖時間,也不是重點,所以在此就先跳過啦。

8.4 控制主題的各種元素

大概有40多個屬性可以控制主題。大致分為plot,axis,legend,panel和facet這幾大類。

下表總結(jié)了一下控制主題的主要元素:

下面從中選幾個比較重要的來舉幾個例子

當(dāng)橫軸刻度線標(biāo)簽名字過長,我們可以調(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)整圖形的長寬比:

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 儲存和導(dǎo)出(Saving Your Output)

當(dāng)保存圖片時,你有兩種基本選擇:矢量型圖片和柵格型圖片

矢量型:圖形可以無限縮放沒有細節(jié)的損失;但如果包含數(shù)千個對象,矢量渲染過程會很慢

柵格形:以像素陣列形式存儲,有固定的最優(yōu)觀測大小,對于圖形印刷,分辨率(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")

顯然第二種方法更加方便簡潔,不過我們需要設(shè)置以下參數(shù):

path設(shè)定圖形儲存路徑。ggsave()可以生成以下格式:.eps,.pdf,.svg,.wmf,.png,.jpg,.bmp, and.tiff.

width和height設(shè)置絕對尺寸的大小,可以精確控制尺寸

分辨率dpi默認值300,你可以修改為600。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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

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