我的数据可视化学习之旅

ggplot2学习(五)

2022-02-22  本文已影响0人  韧心222

ggplot2基础(5)——配色与图例

参考《R数据可视化手册》、《R绘图:一文了解ggplot2颜色的设置》、《ggplot2图例设置》、《Continuous colour bar guide》、《ggplot2学习笔记——图例系统及其调整函数

在ggplot2中颜色是一个图形属性(aesthetic)。一般而言我们会将数据中的某个属性映射为颜色,以便于在帮助读者来理解图片的内容,而在R中,变量可以分为以下几种:

其中离散变量又可以分为

对于不同类型的变量,我们可以使用不同的方法进行映射

查看R中的颜色名称可以使用colors()函数

1 颜色

1.1连续型变量的映射函数

填充色函数

填充色函数包括

轮廓色函数

轮廓色函数包括:

在使用两色渐变的时候,需要指定参数lowhigh
在使用三色渐变的时候,还需要指定参数mid
使用间隔n中间色渐变的时候,需要指定colous

viridis调色板是对色盲友好的调色板,ggplot2(3.0.0)附带了该调色板

library(ggplot2)
library(scales)
library(cowplot)


p <- ggplot(mtcars, aes(x = wt, y = mpg, colour=disp)) + 
    geom_point(size=3)

p1 <- p + scale_colour_gradient(low="black", high="white")

p2 <- p + scale_colour_gradient2(low=muted("red"), mid="white", high=muted("blue"), midpoint=10)

p3 <- p + scale_colour_gradientn(colours=c("darkred", "orange", "yellow", "white"))

p4 <- p + scale_colour_viridis_c()

plot_grid(p, p1, p2, p3, p4)
image.png

1.2 离散型变量的映射函数

填充色函数

轮廓色函数

先看几个简单的例子:

library(ggplot2)
library(scales)
library(cowplot)


p <- ggplot(mtcars, aes(x = wt, y = mpg, colour=factor(cyl))) + 
    geom_point(size=3)

p1 <- p + scale_colour_viridis_d()

p2 <- p + scale_colour_hue(l=45)

p3 <- p + scale_colour_brewer(palette = "Oranges")

p4 <- p + scale_colour_grey()

plot_grid(p, p1, p2, p3, p4)
image.png

最后重点说一下自定义调色板,在使用自定义调色板的时候,需要指定参数values,需要注意的是values参数在指定过程中,其颜色的数量需要与离散变量的数量相一致,不过不想使用默认的颜色顺序,也可在values中指定。

library(viridis)

p <- ggplot(mtcars, aes(x = wt, y = mpg, colour=factor(cyl))) + 
    geom_point(size=3)

p1 <- p + scale_colour_manual(values=c("red", "blue", "yellow"))

p2 <- p + scale_colour_manual(values=c("6"="red", "8"="blue", "4"="yellow"))

p3 <- p + scale_colour_manual(values=viridis(3))

plot_grid(p, p1, p2, p3)
image.png

2 图例

对于图例的操作,主要涉及三类函数,分别是:

2.1 guide()

guides()函数的定义为:

guides(
    colour/fill = guide_colorbar()/guide_legend(),
    size = guide_legend(),
    shape = guide_legend(),
    linetype = guide_legend(),
    alpha = guide_legend()
)

其中guide_colorbar()主要用于处理连续型变量,guide_legend()用于处理离散型变量。

guide_colorbar()函数的定义为:


guide_colorbar(
    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 = 300,
    raster = TRUE,
    frame.colour = NULL,
    frame.linewidth = 0.5,
    frame.linetype = 1,
    ticks = TRUE,
    ticks.colour = "white",
    ticks.linewidth = 0.5,
    draw.ulim = TRUE,
    draw.llim = TRUE,
    direction = NULL,
    default.unit = "line",
    reverse = FALSE,
    order = 0,
    available_aes = c("colour","color", "fill")
    ...
)

在此只介绍一些比较重要的参数:

guide_legend()函数的定义为:

guide_legend(
    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, 
    ...
) 

其中绝大多数参数与guide_colorbar相一致,差别在于:

library(ggplot2)
library(scales)
library(cowplot)


p <- ggplot(mtcars, aes(x = wt, y = mpg, colour=disp, shape=factor(cyl))) + 
    geom_point(size=3)
    

p1 <- p +
    guides(
        color = guide_colorbar(
            title = "Legend 1",
            ticks = FALSE,
            label.position='left',
            order = 2,
        ),
        shape = guide_legend(
            title = "Legend 2",
            label.position='top',
            order = 1,
        )
    )

p2 <- p +
    guides(
        color = guide_colorbar(
            title = "Legend 1",
            ticks = FALSE,
            label.position='left',
            reverse = TRUE,
            order = 1,
        ),
        shape = guide_legend(
            title = "Legend 2",
            label.position='top',
            order = 2
        )
    )

plot_grid(p, p1, p2)
image.png

2.2 theme()

在主题中,也能够对图例进行一定的设置,其参数为以legend开头的一些参数,包括:

其中需要说明的是:

library(ggplot2)
library(scales)
library(cowplot)


p <- ggplot(mtcars, aes(x = wt, y = mpg, colour=disp, shape=factor(cyl))) + 
    geom_point(size=3)

p1 <- p + theme(
        legend.position = c(1,0), 
        legend.justification=c(1,0),
        
    )

p2 <- p1 + theme(
    legend.background = element_blank(),
    legend.key = element_blank(),
    legend.title = element_text(
        face = "italic",
        color = "red",
        size = 14,
    )
)

plot_grid(p, p1, p2)
image.png

2.3 scale_fill_****()和scale_colour_****()

这两个函数在颜色部分已经做了比较细致的介绍了,在此只介绍一些和图例相关的设置

上一篇 下一篇

猜你喜欢

热点阅读