R語(yǔ)言可視化及作圖10--ggplot2的theme函數(shù)


R語(yǔ)言繪圖系列:


theme函數(shù)主要的用途是調(diào)節(jié)圖的主題。如下圖,主題主要分為整幅圖plot,坐標(biāo)軸axis,圖例legend,面板panel和分面元素facet。其中經(jīng)常用到的是坐標(biāo)軸主題的修改,比如常見的坐標(biāo)軸字體大小的修改。

theme函數(shù)其實(shí)很簡(jiǎn)單:
theme(主題.部件=element_類型())
主題:plot, axis, legend, panel, facet
部件:title(名字,坐標(biāo)軸名字), line(線,坐標(biāo)軸的xy軸), text(標(biāo)簽,坐標(biāo)軸刻度的數(shù)字), ticks(坐標(biāo)軸刻度的小線條), background(背景)等
類型:rect(所有矩形區(qū)域?qū)傩?,line(所有線屬性),text(所有文本相關(guān)屬性),title(所有標(biāo)題屬性)
說明:部件要和類型一致。比如,部件為title,text等文字相關(guān)的元素,那么類型處就為text (具體見上圖)。

1. 整幅圖的主題設(shè)置Plot elements
library(tidyverse)
head(mtcars)
p1=ggplot(mtcars,aes(mpg,disp))+geom_point()

p1+labs(title="xyz")+theme(
    plot.background = element_rect(fill = "lightblue", color = "pink", size = 15),
    plot.title = element_text(hjust = 1, color = "black", face = "bold",size=15),
    plot.margin = margin(t = 30, r = 30, b = 30, l = 30, unit = "pt"))
2. 坐標(biāo)軸主題設(shè)置Axis elements
p1+theme(
    axis.line = element_line(color = "lightblue", size = 1),
    axis.title = element_text(color = "black", face = "italic"),
    axis.ticks = element_line(color = "purple", size = 2),
    axis.text = element_text(color = "lightpink"),
    axis.text.x = element_text(angle = 45, hjust = 1)
)
3. 面板元素設(shè)置
p1+ theme(
    panel.background = element_rect(fill = "white", color = "purple"),
    panel.grid = element_line(color = "grey80", size = 0.3)
)
4. 圖例設(shè)置
p2=ggplot(mtcars,aes(mpg,disp,color=cyl))+geom_point()
p2+theme(
    legend.background = element_rect(fill = "lightgrey"),
    legend.title = element_text(color = "black", size = 15),
    legend.key = element_rect(fill = "black"),
    legend.text = element_text(color = "blue"),
    legend.margin = margin(t = 20, r = 10, b = 10, l = 10, unit = "pt"),
    legend.position = "right"
)
5. 參考ggplot2預(yù)設(shè)主題,繪制自己的theme

ggplot2的預(yù)設(shè)主題已經(jīng)寫過了,參考R語(yǔ)言可視化及作圖9--主題函數(shù)。
以theme_linedraw()為例,查看這個(gè)主題設(shè)置了什么參數(shù)

theme_linedraw
## function (base_size = 11, base_family = "", base_line_size = base_size/22, 
##     base_rect_size = base_size/22) 
## {
##     half_line <- base_size/2
##     theme_bw(base_size = base_size, base_family = base_family, 
##         base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace% 
##         theme(axis.text = element_text(colour = "black", size = rel(0.8)), 
##             axis.ticks = element_line(colour = "black", size = rel(0.5)), 
##             panel.border = element_rect(fill = NA, colour = "black", 
##                 size = rel(1)), panel.grid = element_line(colour = "black"), 
##             panel.grid.major = element_line(size = rel(0.1)), 
##             panel.grid.minor = element_line(size = rel(0.05)), 
##             strip.background = element_rect(fill = "black"), 
##             strip.text = element_text(colour = "white", size = rel(0.8), 
##                 margin = margin(0.8 * half_line, 0.8 * half_line, 
##                   0.8 * half_line, 0.8 * half_line)), complete = TRUE)
## }
## <bytecode: 0x7f818ba6d600>
## <environment: namespace:ggplot2>

它無(wú)非是一個(gè)具有兩個(gè)參數(shù)的函數(shù):base_size和base_family。其主題部分直接應(yīng)用的theme函數(shù)就是前面寫的主題設(shè)置函數(shù),這一部分可以直接使用前面介紹的參數(shù)進(jìn)行修改。

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

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

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