这一讲主要讲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()
![](https://img.haomeiwen.com/i441912/15224d4be908f14a.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")
![](https://img.haomeiwen.com/i441912/394d24db6e9c131e.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")
![](https://img.haomeiwen.com/i441912/ac5333df3cc49b84.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")
也能达到相同的目的
![](https://img.haomeiwen.com/i441912/47a3bd2c5f0d183b.png)
网友评论