ggplot2都有哪些使用不多但是卻異常強大的圖層函數
來自專欄 R語言中文社區
要說ggplot2中那些使用不多但是卻功能強大的圖層函數,我首先想到的就是geom_rect、geom_linerange、geom_segment、geom_ploygon。
這四個函數分別定義了ggplot2中的矩形圖、垂直線圖(線範圍圖)、線段圖、幾何多邊形圖。這四種圖表類型獨立使用的機會很少,一般都是作為圖表的輔助信息,但是如果能恰到好處的使用,你會挖掘出很多新意。geom_rect()
geom_rect()一般用於製作自定義矩形,僅需在美學映射中指定每一個矩形在坐標系統中的左邊界、右邊界、下邊界、上邊界即可。
分辨對應xmax,xmax,ymin,ymax等四個美學映射參數,當然單個矩形仍然可以執行分組顏色映射操作。
具體示意如下:library("showtext")library("ggplot2")library("magrittr")library("reshape2")library("ggthemes")library(dplyr)mydata <- data.frame( Lebal = c("Point1","Point2","Point3","Point4","Point5"), xstart = c(5.5,15.7,19.5,37.2,36.9), xend = c(9.7,28.1,24.6,44.6,47.1), ystart = c(9.6,23.1,2.3,33.2,9.2), yend = c(16.1,36.2,11.7,38.5,15.3), size = c(12,48,30,11.5,28), class = c("A","A","A","C","C"))ggplot(mydata)+ geom_rect(aes(xmin = xstart,xmax = xend , ymin = ystart , ymax = yend , fill = class)) + scale_fill_wsj()
按照x軸進行圓周化:ggplot(mydata)+ geom_rect(aes(xmin = xstart,xmax = xend , ymin = ystart , ymax = yend , fill = class)) + scale_fill_wsj() + ylim(-10,40) + scale_x_continuous(expand = c(0,0)) + coord_polar(theta = x)#按照y軸進行圓周化ggplot(mydata)+ geom_rect(aes(xmin = xstart,xmax = xend , ymin = ystart , ymax = yend , fill = class)) + scale_fill_wsj() + scale_y_continuous(expand = c(0,0)) + coord_polar(theta = y)分面操作:ggplot(mydata)+ geom_rect(aes(xmin = xstart,xmax = xend , ymin = ystart , ymax = yend , fill = class)) + scale_fill_wsj() + facet_grid(.~class) + scale_y_continuous(expand = c(0,0))
geom_segment()
geom_segment通常用於製作直線段圖,路徑圖、放射線圖等,思路也很簡單,只需要指定每一條線段的起點坐標、終點坐標即可。即分別制定x,y,xend,yend。具體示意如下:mydata <- data.frame( Lebal = c("Segment1","Segment2","Segment3","Segment4","Segment5"), xstart = c(3.5,4.4,8.3,13.2,20), ystart = c(5,2.7,4.6,2.2,4.7), xend = c(7.5,8.7,21,25,23), yend = c(7.9,4.2,7.2,3.8,4.4), class = c("A","A","A","C","C"))ggplot(mydata) + geom_segment( aes( x = xstart, y = ystart, xend = xend, yend = yend, colour = class ), arrow = arrow(length = unit(0.5,"cm")), size = 1.5 ) + scale_colour_wsj()#按照X軸圓周化ggplot(mydata) + geom_segment( aes( x = xstart , y = ystart , xend = xend , yend = yend , colour = class ), arrow = arrow(length = unit(0.5,"cm")), size = 1.5 ) + scale_colour_wsj() + scale_y_continuous(expand = c(0,0)) + coord_polar(theta = x)#按照y軸圓周化ggplot(mydata) + geom_segment( aes( x = xstart , y = ystart , xend = xend , yend = yend , colour = class ), arrow = arrow(length = unit(0.5,"cm")), size = 1.5 ) + scale_colour_wsj() + scale_y_continuous(expand = c(0,0)) + coord_polar(theta = y)#分面操作:ggplot(mydata) + geom_segment( aes( x = xstart , y = ystart , xend = xend , yend = yend , colour = class ), arrow = arrow(length = unit(0.5,"cm")), size = 1.5 ) + facet_grid(.~class) + scale_colour_wsj() + scale_y_continuous(expand = c(0,0))
geom_linerange()
這個圖層函數乍看起來真的沒啥用,貌似很多輔助圖形都可以替代,比如那些誤差線(geom_errorbar)、垂直線(geom_vline)、水平線(geom_hline)等。但是其實不然,這個grom_linerange函數可以實現批量美學映射,包含線條顏色自定義、粗細自定義、線型自定義以及線條上下邊界的自定義,通過coord_flip函數還可以將垂直線的所有應用轉化為水平線應用,非常方便。只需要指定x值,y軸的上下邊界即可。
即x,ymin,ymax。
mydata <- data.frame( Lebal = c("linerange1","linerange2","linerange3","linerange4","linerange5"), xstart = c(3.5,7,12,16,20), ymin = c(2.5,6.5,3,4.5,3.8), ymax = c(7.5,9.5,9,13.5,4.2), class = c("A","A","A","C","C"))ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + scale_colour_wsj()# 橫縱軸互換:ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + coord_flip() + scale_colour_wsj()#按x軸圓周化:ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + scale_colour_wsj() + scale_x_continuous(limits = c(0,25),expand = c(0,0)) + coord_polar(theta = x)#按y軸圓周化:ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + scale_colour_wsj() + scale_y_continuous(expand = c(0,0)) + coord_polar(theta = y)#分面:ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + scale_colour_wsj() + facet_grid(.~class) + scale_x_continuous(limits = c(0,25),expand = c(0,0))ggplot(mydata) + geom_linerange(aes(x = xstart, ymin = ymin , ymax = ymax , colour = class) , size = 1.5) + coord_flip() + scale_colour_wsj() + facet_grid(.~class) + scale_x_continuous(limits = c(0,25),expand = c(0,0))
geom_polygon()
最後一個可以說是很厲害了,幾乎是ggplot2裡面最為複雜、強大、適應性廣的圖層函數了,因為它使用極限思維,什麼東西都是用儘可能多的點來擬合。想像一下我們常見的大部分圖表都是由點線面集合元素來構成,所以理論上說,只要你能從這個圖形對象中發現規律,從而得到儘可能多的圖形邊界點,那麼使用geom_polygon對象來實現目標圖形那都是分分鐘的事兒。它的思路是我們對象看成是若干個閉合多邊形,每一個閉合多邊形通過一組有序點坐標進行首尾連接,之後再按照事物真實的隸屬關係對group(閉合多邊形)進行二次分組,按照有意義的組別進行顏色映射。具體示意如下:
mydata <- data.frame( long = c(15.4,17.2,19.7,15.9,7.4,8.9,8.5,10.4,11.3,9.7,4.8,3.7,22.4,25.6,27.8,25.1,16.7,15.9,29.9,38.7,43.2,40.2,35.6,29.4), lat = c(38.1,36.2,33.1,24.6,29.0,33.6,12.1,11.7,8.9,6.1,5.7,9.1,8.4,7.6,5.7,3.9,4.3,5.9,32.6,31.8,27.6,22.3,24.5,29.6), group= c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4), order =c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6), class = rep(c("A","c"),each = 12))#ggplot(mydata) + geom_polygon(aes(x = long , y = lat , group = group , fill = class),colour = "white") + scale_fill_wsj()#按照X軸圓周化:ggplot(mydata) + geom_polygon(aes(x = long , y = lat , group = group , fill = class),colour = "white") + coord_polar(theta = x) + scale_x_continuous(expand = c(0,0)) + scale_fill_wsj()#按照y軸圓周化:ggplot(mydata) + geom_polygon(aes(x = long , y = lat , group = group , fill = class),colour = "white") + coord_polar(theta = y) + scale_y_continuous(expand = c(0,0)) + scale_fill_wsj()#分面:ggplot(mydata) + geom_polygon(aes(x = long , y = lat , group = group , fill = class),colour = "white") +facet_grid(.~class) + scale_fill_wsj()
https://edu.hellobi.com/course/264
閱讀原文推薦閱讀: