R可视化 庖丁解牛 之 如何设置ggplot2里的轴名称axis name, 标题title, subtitle以及相关设置

这一讲主要讲ggplot2里面对轴名称,标题title, 以及副标题subtitle相关操作。

先加载相关包,并且画出散点图Scatter plot.

rm(list=ls())

if (!require("pacman")) install.packages("pacman")

p_load(datasets, ggplot2, ggthemes, dplyr, RColorBrewer, grid)

data("iris")

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +

geom_point()

截屏2021-03-03 20.20.18.png

1. 增加标题title的方法

我们用labs()函数,然后将标题的名字以字符串参数传进去。

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +

geom_point() +

labs(title = "This is the title about relation between Sepal length and Sepal width",

subtitle = "Source: R pacakage datasets")

截屏2021-03-03 20.25.09.png

2. 增加轴名称的方法

增加轴名称的方法主要有两种

2.1 第一种是通过labs()函数

2.2 第二种是通过scale_x_continous()/scale_y_continous()函数。

2.1 通过labs()函数

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +

geom_point() +

labs(x = "Length of Sepal of iris",

y= "Length of Sepal of iris")

截屏2021-03-03 20.29.56.png

2.1 通过scale_x_continous()/scale_y_continous()函数

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +

geom_point() +

scale_x_continuous(name = "Length of Sepal of iris") +

scale_y_continuous(name = "Width of Sepal of iris")

也能达到相同的目的

截屏2021-03-03 20.33.37.png