學習了ggplot2的基本繪圖元素ggplot2|詳解八大基本繪圖要素,可以初步繪制出需要展示的圖形,legend可以對圖例進行細節的修改ggplot2 |legend參數設置,圖形精雕細琢,那theme有什么用呢?
theme是解決圖是否美觀的一個工具,其與scale最大的區別在於不受數據左右。先把scale做好,就是一張合格的圖;再處理theme,則是一張出色的圖。
一 載入數據,R包
library(ggplot2)
#載入數據
data(diamonds)
set.seed(1234)
diamond <- diamonds[sample(nrow(diamonds), 2000), ]
# 繪制初始圖形
p <- ggplot(data = diamond) +geom_point(aes(x=carat, y=price, colour=color,shape=cut)) + labs(title="學習ggplot2可視化",subtitle = "參數好多學不會?",caption = "熟能生巧")
p
可以上圖的標題,軸標簽和圖例已經默認設置好了,是否可以個性化修改呢? 當然可以!!!
輸入?theme
可以看到theme()
功能的大量參數,可以實現與外觀相關的大多數要求。其中有四種主要類型:
-
element_text()
:使用element_text()
函數設置基於文本的組件,如title,subtitle 和caption等。 -
element_line()
:使用element_line()
設置基於線的組件,如軸線,主網格線和次網格線等。 -
element_rect()
:使用element_rect()
修改基於矩形的組件,如繪圖區域和面板區域的背景。 -
element_blank()
:使用element_blank()
關閉顯示的主題內容。
二 精雕細琢
1 修改標題,坐標軸
由於繪圖和軸標題是文本組件,使用element_text()
參數修改。
設置title的尺寸,顏色,線高,位置
p + theme(plot.title=element_text(size=20,
face="bold",
color="skyblue", #顏色
hjust=0.5, #調整位置,正中間
lineheight=1.2))
設置subtitle和caption
p + theme(plot.subtitle=element_text(size=15,
face="bold",
color = "red",
hjust=0.5), # subtitle
plot.caption=element_text(size=15)) # caption
修改坐標軸
p + theme(axis.title.x=element_text(vjust=1,
size=20), # X axis title
axis.title.y=element_text(size=10,
color = "blue"), # Y axis title
axis.text.x=element_text(size=10,
angle = 45,
color = "red",
vjust=.5), # X axis text
axis.text.y=element_text(size=10)) # Y axis text
以上示例涵蓋了一些常用的主題修改,其中
-
vjust
,控制標題(或標簽)和繪圖之間的垂直間距。 -
hjust
,控制水平間距。將其設置為0.5將標題居中。 -
face
,設置字體(“plain”,“italic”,“bold”,“bold.italic”)
2 修改圖例
設置圖例標題,文本和鍵的樣式
圖例的關鍵是像元素一樣的圖形,因此使用element_rect()
函數進行設置。
p + theme(legend.title = element_text(size=15, color = "firebrick"),
legend.text = element_text(size=10),
legend.key=element_rect(fill='green'))
刪除圖例和更改圖例位置
圖例是主題的一個方面,因此可以使用theme()
功能進行修改。其中legend.justification
參數可以將圖例設置在圖中,legend.position
參數用來將圖例設置在圖表區域,其中x和y軸的位置(0,0)
是在圖表的左下和(1,1)
是右上角。
# No legend --------------------------------------------------
p + theme(legend.position="None") + labs(subtitle="No Legend")
# legend at the bottom and horizontal ------------------------
p + theme(legend.position="bottom", legend.box = "horizontal") + labs(subtitle="Legend bottom")
# legend at bottom-right, inside the plot --------------------
p + theme(legend.title = element_text(size=12, color = "salmon", face="bold"),
legend.justification=c(1,0),
legend.position=c(0.95, 0.05),
legend.background = element_blank(),
legend.key = element_blank()) +
labs(subtitle="Legend: Bottom-Right Inside the Plot")
3 修改繪圖背景,主軸和次軸
更改繪圖背景
# 更改繪圖背景和繪圖區域
p + theme(panel.background = element_rect(fill = 'grey80'),
plot.background=element_rect(fill="khaki"),
plot.margin = unit(c(3, 2, 1, 1), "cm")) + #設置繪圖區域距離邊的據類,上,右,下,左
labs(title="Modified Background", subtitle="Change Plot Margin")
更改主次網格線以及X,Y坐標軸
# Change Plot Background elements -----------------------------------
p + theme(
panel.grid.major = element_line(colour = "burlywood", size=1.5),
panel.grid.minor = element_line(colour = "tomato",
size=0.25,
linetype = "dashed"),
panel.border = element_blank(),
axis.line.x = element_line(colour = "darkorange",
size=1.5,
lineend = "butt"),
axis.line.y = element_line(colour = "skyblue",
size=1.5)) +
labs(
subtitle="Change Major and Minor grid, Axis Lines")
刪除主,次網格線,邊框,軸標題,文本和刻度
p + theme(panel.grid.major = element_blank(), #主網格線
panel.grid.minor = element_blank(), #次網格線
panel.border = element_blank(), #邊框
axis.title = element_blank(), #軸標題
axis.text = element_blank(), # 文本
axis.ticks = element_blank()) +
labs(title="Modified Background", subtitle="Remove major and minor axis grid, border, axis title, text and ticks")
默認主題以及自定義主題
ggplot2 自帶主題
theme_grey()為默認主題,theme_bw()為白色背景主題,theme_classic()為經典主題。
p + theme_bw() +
labs(subtitle="Change theme_bw")
ggplot2 擴展包主題
library(ggthemes)
p + theme_economist() +
labs(subtitle="Change theme_economist")
#其他可選theme_economist theme_economist_whitetheme_wsj theme_exceltheme_few
#theme_foundationtheme_igray theme_solarizedtheme_stata theme_tufte
自定義主題
可根據常見需要自定義常用主題
p + theme_MJ() + labs(subtitle = "Change theme_MJ")
學習ggplot2的八大基本元素,legend的基本設置后,現在也清楚了主題的相關設置,先把scale做好,就是一張合格的圖;再處理theme,則是一張出色的圖。
【關注“生信補給站”公眾號,對話框回復 R-theme 即可獲得上述R代碼】
更多關於生信,R,Python的內容請掃碼關注小號,謝謝。