學(xué)習(xí)了ggplot2的基本繪圖元素ggplot2|詳解八大基本繪圖要素,可以初步繪制出需要展示的圖形,legend可以對圖例進行細(xì)節(jié)的修改ggplot2 |legend參數(shù)設(shè)置,圖形精雕細(xì)琢,那theme有什么用呢? theme是解決圖是否美觀的一個工具,其與scale最大的區(qū)別在于不受數(shù)據(jù)左右。先把scale做好,就是一張合格的圖;再處理theme,則是一張出色的圖。 載入數(shù)據(jù),R包 #載入數(shù)據(jù) 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="學(xué)習(xí)ggplot2可視化",subtitle = "參數(shù)好多學(xué)不會?",caption = "熟能生巧") p 可以看到上圖的標(biāo)題,軸標(biāo)簽和圖例已經(jīng)默認(rèn)設(shè)置好了,是否可以個性化修改呢?當(dāng)然可以?。?! R控制臺輸入
精雕細(xì)琢 1 修改標(biāo)題,坐標(biāo)軸由于繪圖和軸標(biāo)題是文本組件,使用 設(shè)置title的尺寸,顏色,線高,位置p + theme(plot.title=element_text(size=20,face="bold", color="skyblue", #顏色 hjust=0.5, #調(diào)整位置,正中間 lineheight=1.2)) 設(shè)置subtitle和captionp + theme(plot.subtitle=element_text(size=15,face="bold", color = "red", hjust=0.5), # subtitle plot.caption=element_text(size=15)) # caption 修改坐標(biāo)軸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 以上示例涵蓋了一些常用的主題修改,其中
2 修改圖例設(shè)置圖例標(biāo)題,文本和鍵的樣式圖例的關(guān)鍵是像元素一樣的圖形,因此使用 legend.text = element_text(size=10), legend.key=element_rect(fill='green')) 刪除圖例和更改圖例位置圖例是主題的一個方面,因此可以使用 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 修改繪圖背景,主軸和次軸更改繪圖背景# 更改繪圖背景和繪圖區(qū)域p + theme(panel.background = element_rect(fill = 'grey80'), plot.background=element_rect(fill="khaki"), plot.margin = unit(c(3, 2, 1, 1), "cm")) + #設(shè)置繪圖區(qū)域距離邊的據(jù)類,上,右,下,左 labs(title="Modified Background", subtitle="Change Plot Margin") 更改主次網(wǎng)格線以及X,Y坐標(biāo)軸# 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") 刪除主,次網(wǎng)格線,邊框,軸標(biāo)題,文本和刻度p + theme(panel.grid.major = element_blank(), #主網(wǎng)格線panel.grid.minor = element_blank(), #次網(wǎng)格線 panel.border = element_blank(), #邊框 axis.title = element_blank(), #軸標(biāo)題 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") 4 默認(rèn)主題以及自定義主題ggplot2 自帶主題theme_grey()為默認(rèn)主題,theme_bw()為白色背景主題,theme_classic()為經(jīng)典主題。 p + theme_bw() +labs(subtitle="Change theme_bw") ggplot2 擴展包主題library(ggthemes)p + theme_economist() + labs(subtitle="Change theme_economist") #其他可選 #theme_economist theme_economist_white theme_wsj theme_excel theme_few #theme_foundation theme_igray theme_solarized theme_stata theme_tufte自定義主題可根據(jù)常見需要自定義常用主題 p + theme_MJ() + labs(subtitle = "Change theme_MJ") 學(xué)習(xí)ggplot2的八大基本元素,了解legend的基本設(shè)置后,現(xiàn)在也清楚了主題的相關(guān)設(shè)置,就可以畫出一張出色的圖了?? |
|