最近确实更得太少了,也不知道自己在忙啥,反正感觉不到忙碌的收获,要不是好多小伙伴儿在后台催更,感觉都快忘了还有要更新公众号这回事儿, 今天赶紧找空写一篇~ 学过ggplot2的小伙伴儿们大概都了解过,ggplot2的语法系统将数据层和美化层分开,这种理念给了学习更多的选择,你可以只学习数据层,这样大可保证做出正确的图来(虽然质量不敢恭维),也可以同时学习数据层和美化层(当然你要耗费双倍的精力,因为ggplot2理念几个美化的函数模块那是出了名的繁杂)。 最初学习的时候也是各种急功近利,后来才慢慢沉下心来看源文档,逐步系统的总结了其非数据层面的各套系统的设计理念。 今天只讲图例系统,这一块是大部分学习者非常容易忽视的东西(有木有学会ggplot()+geom_xxx就觉得超神了然后自我感慨,哇塞ggplot2原来这么简单,远远没有传说中的那么难耶)。 这么想就不对了,即便你深入掌握了ggplot() + geom_xxx()及其内部参数继承逻辑关系,那也只能保证你正确的做出图来,正确的做出来图并不带表可以把图做好,就像工作不出错并不代表工作出色一样,这样只能打60份,因为图表质量不够,这样使用ggplot2并无法发挥其百分百的功力。 如果你想洞悉ggplot2的全貌,你需要了解除了geom_xxx(stat_xxxx)系统之外的 标度调整系统: 轴标度【scales_x/y_continuous/discrete】、颜色标度【scale_fill/colour_continuous/discrete/manual】、透明度标度【scale_alpha_continuous/discrete/manual】、大小标度【scale_size/radius_area】、形状标度【scale_shape】、线条类型标度【scale_linetype】 坐标系系统: coord_flip()coord_polar()coord_map() 图例系统: guides() guide_colorbar() guide_legend() 分面系统: facet_grid()facet_wrap() 主题系统: theme()theme_get()theme_set(new)theme_update(...)theme_replace(...)e1 %+replace% e2element_text()element_rect()element_line()element_blank() 文本标签系统: geom_text()geom_label()annotations()labs() 可以毫不留情的说,以上这些系统的所有内置参数全部罗列出来,没上千也有好几百了,所以学好ggplot2真的任重而道远呀哈哈 本篇只分享图例系统: guides() ggplot2的图例系统函数比较分散,在所有标度调整函数(轴标度除外)内部留有guide参数,可以通过guide_colorbar()、guide_legend()两个封装函数来进行图例自定义,但是因为guide_colorbar()、guide_legend()这两个函数也是巨无霸,直接写在scale_xxx这种本身就是巨无霸的函数内部,整个代码简直不堪重负。 所以我喜欢写在单独调整图例的函数guides函数中,它在语法层次上是与scale_xxx类的标度调整函数平行的。 按照美学映射给变量的类型来划分,图例一共分两类,连续性颜色标度的图例叫做colobar,离散颜色标度的图例和所有非颜色图例(透明度、大小、形状、线条)叫做legend。 那么对应的图例调整函数就是: guide_colorbar() 所以写在guides中的格式应该是这样的: guides(colour/fill = guide_colorbar(), #连续型变量 假设你的图表中使用了这么多的图层(事实上不可能,多于两个就灰常难以理解了),那么每一个标度名称对应一个标度调整函数。 那么接下来,我们就要看下具体的标度调整函数内部都是些什么神秘兮兮的玩意儿啦~ library('ggplot2')
function (title = waiver(), title.position = NULL, title.theme = NULL, title.hjust = NULL, title.vjust = NULL, label = TRUE, label.position = NULL, label.theme = NULL, label.hjust = NULL, label.vjust = NULL, barwidth = NULL, barheight = NULL, nbin = 20, raster = TRUE, ticks = TRUE, draw.ulim = TRUE, draw.llim = TRUE, direction = NULL, default.unit = 'line', reverse = FALSE, order = 0, ...) { if (!is.null(barwidth) && !is.unit(barwidth)) barwidth <- unit(barwidth,="" default.unit)="" ="">->if (!is.null(barheight) && !is.unit(barheight)) barheight <- unit(barheight,="" default.unit)="" ="" structure(list(title="title," title.position="title.position," ="" ="" ="" title.theme="title.theme," title.hjust="title.hjust," ="" ="" ="" title.vjust="title.vjust," label="label," label.position="label.position," ="" ="" ="" label.theme="label.theme," label.hjust="label.hjust," ="" ="" ="" label.vjust="label.vjust," barwidth="barwidth," barheight="barheight," ="" ="" ="" nbin="nbin," raster="raster," ticks="ticks," draw.ulim="draw.ulim," ="" ="" ="" draw.llim="draw.llim," direction="direction," default.unit="default.unit," ="" ="" ="" reverse="reverse," order="order," available_aes="">->'colour', 'color', 'fill'), ..., name = 'colorbar'), class = c('guide', 'colorbar'))}
function (title = waiver(), title.position = NULL, title.theme = NULL, title.hjust = NULL, title.vjust = NULL, label = TRUE, label.position = NULL, label.theme = NULL, label.hjust = NULL, label.vjust = NULL, keywidth = NULL, keyheight = NULL, direction = NULL, default.unit = 'line', override.aes = list(), nrow = NULL, ncol = NULL, byrow = FALSE, reverse = FALSE, order = 0, ...) { if (!is.null(keywidth) && !is.unit(keywidth)) keywidth <- unit(keywidth,="" default.unit)="" ="">->if (!is.null(keyheight) && !is.unit(keyheight)) keyheight <- unit(keyheight,="" default.unit)="" ="" structure(list(title="title," title.position="title.position," ="" ="" ="" title.theme="title.theme," title.hjust="title.hjust," ="" ="" ="" title.vjust="title.vjust," label="label," label.position="label.position," ="" ="" ="" label.theme="label.theme," label.hjust="label.hjust," ="" ="" ="" label.vjust="label.vjust," keywidth="keywidth," keyheight="keyheight," ="" ="" ="" direction="direction," override.aes="rename_aes(override.aes)," ="" ="" ="" nrow="nrow," ncol="ncol," byrow="byrow," reverse="reverse," ="" ="" ="" order="order," available_aes="">->'any'), ..., name = 'legend'), class = c('guide', 'legend'))} 原函数是这样的,是不是看着很晕呀哈哈,ggplot2里面随便一个函数都是这样的,不用太惊讶。 我大致过滤了以下,筛选出来这么几个觉得长用到的有价值的函数如下: 实际上主要的参数只有三大类:主要作用于图例标题、图例文本标签,以及图例箱体。 图例标题: title title.vjusttitle.hjust 图例文本标签系统: label label.positionlabel.vjustlabel.hjustdirection 图例箱体函数: arguments guide_colorbar guide_legend barwidth/barheight用于调整矩形色块的宽高属性,nbin控制颜色的分割区间(分割越多,过渡越自然),ticks控制是否显示刻度线。 连续型颜色标度的图例和其他图例唯一的去别家仅仅在于外观上,连续型颜色图例是一个封闭的矩形色条,而其他图例都是有小方块 构成的一组数据条(小方格称之为key)。所以在调整图例箱体上,二者的函数略有区别。 接下来通过一个简单的例子来实操以上图例调整函数。 mydata5 <- data.frame(="" x="">->100,0,100), y = runif(100,0,100), z = runif(100,0,100), f = runif(100,0,100), g = rep(LETTERS[1:5],each = 20)) p <- ggplot(mydata5,aes(x,y))+="" geom_point(aes(fill="z,size" =="" f="" ),shape="">->21);p colorbar = guide_colorbar( title = 'Colorbar', title.position = 'left', #left,right,top,bottom title.theme = element_text(size = 15,face = 'italic',colour = 'red',angle = 45), title.hjust = .5, title.vjust = 0, label = TRUE, label.position = 'top', label.theme = element_text(size = 15,face = 'italic',colour = 'red',angle = 45), label.hjust = .5, label.vjust = .5, barwidth = unit(6,'cm'), #Default value is legend.key.width or legend.key.siz barheight = unit(1.2,'cm'), #Default value is legend.key.height or legend.key.size nbin = 20, ticks = TRUE, draw.ulim = TRUE, draw.llim = TRUE, direction = 'horizontal', #'horizontal' or 'vertical.' reverse = TRUE, order = 1) size_legend = guide_legend( title = 'legend', title.position = 'left', title.hjust = .5, title.vjust = .5, title.theme = element_text(size = 15,face = 'italic',colour = 'red',angle = 45), keywidth = 2.5, keyheight = 2, label.position = 'bottom', direction = 'horizontal', label = TRUE, label.hjust = 0.5, label.vjust = 0.5, label.theme = element_text(size = 15,face = 'italic',colour = 'red',angle = 45), nrow = 2, byrow = TRUE, reverse = TRUE, order = 2) p + scale_fill_gradient(breaks = seq(0,100,10)) + scale_size_area(breaks = seq(0,100,10)) + guides(fill = colorbar,size = size_legend) 那么除了guides函数之外,还有另外一套图例调整函数分布在theme函数内部(以legend开头的图例系统)。 以上函数中,着重说一下order参数,order参数用于表明图例在图表上显示的顺序(因为在图表中如果有多个美学映射,那么会形成多个图例【如果这些美学映射不是映射在同一个变量上的话】,如果不对图例显示顺序加以限制,那么顺序就会按照底层函数的逻辑呈现,不一定符合我们实际要求)。 好了,关于图例就分享这么多了,下一次分享一下主题系统。 墙裂推荐大家看源文档学ggplot2,源文档才是精品,没有之一~ http://ggplot2./reference/ 欢迎大家加入QQ群一起探讨学习
|
|