首页 > 科研绘图 > ggplot2都有哪些使用不多但是却异常强大的图层函数
2022
12-29

ggplot2都有哪些使用不多但是却异常强大的图层函数

要说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

那么关于这个图层函数,到底有啥用呢,用处大了去了,不信请看:

这个几个图形应该算是平时使用几率不会很高的图形了,但是用的恰到好处的话,能够做出非常惊艳的效果,赶快试一试吧~

《R语言商务图表与数据可视化》的课程已经更新到空间地理信息数据可视化的章节了,感兴趣的童鞋,不了解一下嘛~

https://edu.hellobi.com/course/264

最后编辑:
作者:萌小白
一个热爱网络的青年!

发布评论

表情