ggplot2 study notes - legend system and its adjustment function

640?wx_fmt=gif&wxfrom=5&wx_lazy=1

About the AuthorIntroduction

Du Yu , member of the EasyCharts team, columnist of the R language Chinese community, interested in: Excel business charts, R language data visualization, geographic information data visualization.

Personal public account: Data Little Rubik's Cube (WeChat ID: datamofang), founder of "Data Little Rubik's Cube". 


Highlights ·

The R language study notes that have been thrown up in those years are all here~

640?wx_fmt=gif&wxfrom=5&wx_lazy=1

640?wx_fmt=png&wxfrom=5&wx_lazy=1

Click on the picture, the limited-time discount is waiting for you!

Friends who have learned ggplot2 probably know that the syntax system of ggplot2 separates the data layer and the beautification layer. This concept gives you more choices for learning. You can only learn the data layer, which can ensure that you make the right decisions. You can also learn the data layer and the beautification layer at the same time (of course you have to spend double the effort, because the ggplot2 concept and several beautified function modules are notoriously complicated).

When I first learned, I was eager for quick success. Later, I slowly settled down to look at the source documents, and gradually systematically summarized the design concepts of its non-data-level systems.

Today I only talk about the legend system, which is something that most learners are very easy to ignore (if you have learned ggplot()+geom_xxx, you will feel amazing and then feel yourself, wow, ggplot2 is so simple, far from the legend. difficult).

It's not right to think so, even if you have a deep grasp of ggplot() + geom_xxx() and its internal parameter inheritance logical relationship, it can only guarantee that you make the right picture. The graph is done well, just like the fact that the work does not make mistakes does not mean that the work is excellent, so only 60 copies can be made, because the quality of the graph is not enough, so using ggplot2 cannot give full play to its power.

If you want to see the whole picture of ggplot2, you need to understand the system other than geom_xxx (stat_xxxx)

Scale adjustment system:

轴标度【scales_x/y_continuous/discrete】、颜色标度【scale_fill/colour_continuous/discrete/manual】、透明度标度【scale_alpha_continuous/discrete/manual】、大小标度【scale_size/radius_area】、形状标度【scale_shape】、线条类型标度【scale_linetype】

Coordinate system:

coord_flip()coord_polar()coord_map()

Legend system:

guides()    guide_colorbar()    guide_legend()

Faceted system:

facet_grid()facet_wrap()

Theme system:

theme()theme_get()theme_set(new)theme_update(...)theme_replace(...)e1 %+replace% e2element_text()element_rect()element_line()element_blank()

Text label system:

geom_text()geom_label()annotations()labs()

It can be said ruthlessly that all the built-in parameters of the above systems are listed, there are hundreds if not thousands, so learning ggplot2 is really a long way to go haha

This article only shares the legend system:

guides()

The legend system functions of ggplot2 are relatively scattered, and there are guide parameters inside all scale adjustment functions (except axis scale). , guide_legend() These two functions are also Big Macs. They are written directly inside the function of scale_xxx, which is itself a Big Mac. The whole code is simply overwhelmed.

So I like to write in the guides function that adjusts the legend separately, which is syntactically parallel to the scale adjustment functions of the scale_xxx class.

According to the type of variables mapped to the aesthetics, the legends are divided into two categories, the legend of the continuous color scale is called colobar, the legend of the discrete color scale and all non-color legends (transparency, size, shape, lines) are called legends.

Then the corresponding legend adjustment function is:

guide_colorbar()
guide_legend()

So the format written in the guides should be like this:

guides(colour/fill = guide_colorbar(), #连续型变量
colour/fill = guide_legend(),   #离散型变量
size        = guide_legend(),shape       = guide_legend(),linetype    = guide_legend(),alpha       = guide_legend())

Assuming that you have so many layers in your chart (in fact, it is impossible, more than two is very difficult to understand), then each scale name corresponds to a scale adjustment function.

Then, let's take a look at the mysterious things inside the specific scale adjustment function~

library("ggplot2")

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,     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 = c("colour",             "color", "fill"), ..., name = "colorbar"), class = c("guide",         "colorbar"))}<environment: namespace:ggplot2>

guide_legend

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 = c("any"), ..., name = "legend"),         class = c("guide", "legend"))}<environment: namespace:ggplot2>

The original function is like this, don’t you look dizzy haha, any function in ggplot2 is like this, don’t be too surprised.

I have roughly filtered the following, and I have screened out a few valuable functions that I think are used for a long time as follows:

640?wx_fmt=jpeg

In fact, there are only three main types of parameters: mainly used for legend title, legend text label, and legend box.

Legend title:

title                         title.vjusttitle.hjust

Legend text label system:

label                       label.positionlabel.vjustlabel.hjustdirection

Legend box function:

arguments  guide_colorbar         guide_legend
size         barwidth/barheight         key.width/key.height/key.size      分箱数       nbin                        nrow/ncol/byrow                                   刻度线       ticks/draw.ulim/draw.llim

barwidth/barheight is used to adjust the width and height properties of the rectangular color block, nbin controls the division interval of the color (the more divisions, the more natural the transition), and the ticks controls whether to display the tick marks.

key.width/key.height/key.size is used to control the width, height and size of the small rectangular block (key) in the legend (except continuous color).

nrow/ncol/byrow is used to control the overall layout of the small holding blocks, arranged in several rows, several columns, and the arrangement basis (by row or by column)

The only difference between the legend of the continuous color scale and other legends is only in appearance. The continuous color legend is a closed rectangular color bar, while the other legends are a set of data bars composed of small squares (the small squares are called is the key). Therefore, in adjusting the legend box, the functions of the two are slightly different.

Next, a simple example is used to implement the above legend adjustment function.

mydata5 <- data.frame(  x = runif(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)

640?wx_fmt=jpeg

640?wx_fmt=jpeg


Then in addition to the guides function, there is another set of legend adjustment functions distributed inside the theme function (the legend system starting with legend).

640?wx_fmt=jpeg


In the above function, focus on the order parameter, which is used to indicate the order in which the legend is displayed on the chart (because if there are multiple aesthetic mappings in the chart, multiple legends will be formed [if these aesthetic mappings are not mapped in the same If there is no limit to the display order of the legend, then the order will be presented according to the logic of the underlying function, which may not meet our actual requirements).

Well, that's all I have shared about the legend. Next time I will share the theme system.

The wall crack recommends that you read the source document to learn ggplot2, the source document is the quality, there is no one~

http://ggplot2.tidyverse.org/reference/


Recommended courses


Comprehensive system, the most tonal! R language visualization & business chart practical course:640?wx_fmt=png

640?wx_fmt=gif

Click "Read the original text" to open a new pose

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326382418&siteId=291194637