ggplot2學習筆記系列之主題(theme)設置

taoyan

上次提了下theme(),本文將專門講解一下。凡是與數據無關的圖形設置可以歸為主題類,ggplot2中主題設置十分多,根本不可能講解完,只能稍微講點皮毛,靈活運用才是關鍵,本文只是總體上略作介紹。正如R語言大神Hadley Wickham所講的,ggplot2只是提供了一個平臺,可以根據自己的需要無限創造。理論上來講,只要能想到的圖形,ggplot2都能實現。

library(ggplot2)
#我們先來看看ggplot2默認的主題設置函數theme_gray()的源代碼
theme_gray#函數名不加括號可獲得函數源代碼

## function (base_size = 11, base_family = "") 
## {
## half_line <- base_size/2
## theme(line = element_line(colour = "black", size = 0.5, linetype = 1, 
## lineend = "butt"), rect = element_rect(fill = "white", 
## colour = "black", size = 0.5, linetype = 1), text = element_text(family = base_family, 
## face = "plain", colour = "black", size = base_size, lineheight = 0.9, 
## hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), 
## debug = FALSE), axis.line = element_blank(), axis.line.x = NULL, 
## axis.line.y = NULL, axis.text = element_text(size = rel(0.8),
## colour = "grey30"), axis.text.x = element_text(margin = margin(t = 0.8 * 
## half_line/2), vjust = 1), axis.text.x.top = element_text(margin = margin(b = 0.8 * ## half_line/2), vjust = 0), axis.text.y = element_text(margin = margin(r = 0.8 * 
## half_line/2), hjust = 1), axis.text.y.right = element_text(margin = margin(l = 0.8 * ## half_line/2), hjust = 0), axis.ticks = element_line(colour = "grey20"), 
## axis.ticks.length = unit(half_line/2, "pt"), axis.title.x = element_text(margin = margin(t = half_line), 
## vjust = 1), axis.title.x.top = element_text(margin = margin(b = half_line), 
## vjust = 0), axis.title.y = element_text(angle = 90, 
## margin = margin(r = half_line), vjust = 1), axis.title.y.right = element_text(angle = -90, 
## margin = margin(l = half_line), vjust = 0), legend.background = element_rect(colour = NA), 
## legend.spacing = unit(0.4, "cm"), legend.spacing.x = NULL, 
## legend.spacing.y = NULL, legend.margin = margin(0.2, 
## 0.2, 0.2, 0.2, "cm"), legend.key = element_rect(fill = "grey95", 
## colour = "white"), legend.key.size = unit(1.2, "lines"), 
## legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(0.8)), 
## legend.text.align = NULL, legend.title = element_text(hjust = 0), 
## legend.title.align = NULL, legend.position = "right", 
## legend.direction = NULL, legend.justification = "center", 
## legend.box = NULL, legend.box.margin = margin(0, 0, 0, 
## 0, "cm"), legend.box.background = element_blank(),
## legend.box.spacing = unit(0.4, "cm"), panel.background = element_rect(fill = "grey92", 
## colour = NA), panel.border = element_blank(), panel.grid.major = element_line(colour = "white"), 
## panel.grid.minor = element_line(colour = "white", size = 0.25), 
## panel.spacing = unit(half_line, "pt"), panel.spacing.x = NULL, 
## panel.spacing.y = NULL, panel.ontop = FALSE, strip.background = element_rect(fill = "grey85", 
## colour = NA), strip.text = element_text(colour = "grey10", 
## size = rel(0.8)), strip.text.x = element_text(margin = margin(t = half_line, 
## b = half_line)), strip.text.y = element_text(angle = -90, 
## margin = margin(l = half_line, r = half_line)), strip.placement = "inside", 
## strip.placement.x = NULL, strip.placement.y = NULL, strip.switch.pad.grid = unit(0.1, 
## "cm"), strip.switch.pad.wrap = unit(0.1, "cm"), plot.background = element_rect(colour = "white"), 
## plot.title = element_text(size = rel(1.2), hjust = 0, 
## vjust = 1, margin = margin(b = half_line * 1.2)), 
## plot.subtitle = element_text(size = rel(0.9), hjust = 0, 
## vjust = 1, margin = margin(b = half_line * 0.9)),
## plot.caption = element_text(size = rel(0.9), hjust = 1, 
## vjust = 1, margin = margin(t = half_line * 0.9)),
## plot.margin = margin(half_line, half_line, half_line, 
## half_line), complete = TRUE)
## }
## <environment: namespace:ggplot2>

可以看出,源代碼主要是theme()函數,設置也很簡單:theme(..., complete = FALSE),但是其內含的參數則十分多。

幾乎所有元素在theme()里都使用element_lineelement_rectelement_textelement_blank函數設置. 下面就舉例稍微講解一下

#利用數據集mtcars演示head(mtcars)
#先創建p圖層
p<- ggplot(data=mtcars, aes(x=wt, y=mpg))+
geom_point(aes(color=factor(cyl)))#先試試圖例修改
p+theme(legend.position = "none")#無圖例
p+theme(legend.position = "bottom")#圖例在底部
#也可以自定義
p+theme(legend.position = c(0.5, 0.7))
#為圖例加邊界
p+theme(legend.background = element_rect(color="red"))
#或者為圖例中的每個元素進行設置,如加邊界
p+theme(legend.key =element_rect(color="red"))
#進行填充
p+theme(legend.key = element_rect(fill="yellow"))
#圖例內容字體大小、顏色、角度等設置
p+theme(legend.text = element_text(size=25, color="darkred", angle=45))
#為圖例標題設置字體、顏色、大小等
p+theme(legend.title = element_text(face="italic", size=25, color="red"))

接下來是坐標以及網格等的自定義

#修改背景顏色
p+theme(panel.background = element_rect(fill="black"))
#修改邊界線類型、顏色
p+theme(panel.border = element_rect(linetype = "dashed", color="red"))
#修改網格線p+theme(panel.grid.major = element_line(color="blue", size= 3))
p+theme(panel.grid.minor = element_line(linetype = "dotted", color="red"))

還可以修改x、y軸等,這里懶得講了,有興趣的可以自己搗鼓搗鼓

了解theme之后就可以自己定義自己的主題,以后作圖就直接像格式刷一樣就行

#下面是我比較常用的主題,畫圖時刷一下就行了
windowsFonts(CA=windowsFont("Calibri"))
mytheme <- theme_bw()+
theme(legend.position = 'top', panel.border = element_blank(), 
panel.grid.major = element_line(linetype = 'dashed'), panel.grid.minor = 
element_blank(), legend.text = element_text(size=9,color='#003087',family = "CA"), 
plot.title = element_text(size=15,color="#003087",family = "CA"), legend.key = 
element_blank(), axis.text = element_text(size=10,color='#003087',family = "CA"), 
strip.text = element_text(size=12,color="#EF0808",family = "CA"), 
strip.background = element_blank())
pie_theme <- mytheme+
theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = 
element_blank(), panel.grid.major = element_blank())
myline_blue <- geom_line(color="#085A9C", size=2)
myline_red <- geom_line(color="#EF0808",size=2)
myarea <- geom_area(color=NA,fill="#003087",alpha=0.2)
mypoint <- geom_point(size=3,shape=21,color="#003087",fill="white")
mybar <- geom_bar(fill="#0C8DC4",stat = "identity")
mycolor_3 <- scale_fill_manual(values = c("#085A9C","#EF0808","#526373"))
mycolor_7 <- scale_fill_manual(values=c ("#085A9C","#EF0808","#526373","#FFFFE7","#FF9418","#219431","#9C52AD"))
mycolor_line_7 <- scale_color_manual(values=c ("#085A9C","#EF0808","#526373","#FFFFE7","#FF9418","#219431","#9C52AD"))
#可以來刷一刷#隨便建個數據集
x <-rep(1:5, each = 3)
y <-rep(c('A','B','C'),times = 5)
set.seed(1111)
z <-round(runif(min = 10, max = 30, n = 15))
df <-data.frame(x = x, y = y, z = z)
head(df)
##   x   y   z
## 1 1   A   19
## 2 1   B   18
## 3 1   C   28
## 4 2   A   13
## 5 2   B   25
## 6 2   C   30

#柱形圖
(p1 <- ggplot(data=df, aes(x=factor(x), y=z, fill=y))+
geom_bar(stat = "identity", position = "dodge")+
ggtitle("自定義主題theme并應用實踐"))+
xlab("隨便定義了個x")+ylab("隨機數")
p1+mytheme
p1+mytheme+mycolor_7


還有線圖、餅圖等有興趣的也可以自己刷一刷,你會發現ggplot2的魅力所在就是它擁有無窮的可能性。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,302評論 25 708
  • 簡介 文章較長,點擊直達我的博客,瀏覽效果更好。本文內容基本是來源于STHDA,這是一份十分詳細的ggplot2使...
    taoyan閱讀 51,429評論 7 159
  • 最近朋友圈已經開始在倒數了,今年的電量僅剩20%了。國慶圣誕元旦新年…,一年就快要過完了!再回顧下這一年,列下的書...
    吳在天閱讀 891評論 12 14
  • 苦是一種味道苦是一種經歷苦是一種快樂苦是一種得到苦是一種營養苦是一種境界當你品嘗得了食物的苦,當你承受得了生活的苦...
    Becky的能量加油站閱讀 752評論 0 1