ggplot2 : the grammar of graphics
哲学 :点到图像的映射
title 标题 subtitle 副标题 panel 面板绘图区 background 背景板 geoms 几何对象 legend title 图例标题 legend labels 图例标签 legend symbols 图例符号 grid lines 网格线 axis text 坐标轴文本 axis.ticks 坐标轴刻度 axis.title 坐标轴标题
1 .1要安装的包
- tidyverse
- ggplot2
- dplyr 数据预处理
- tidyr 清理数据
- readr 读取数据
## 安装CRAN上的R包
install.packages(c("tidyverse", "colorspace", "corrr", "cowplot",
"ggdark", "ggforce", "ggrepel", "ggridges", "ggsci",
"ggtext", "ggthemes", "grid", "gridExtra", "patchwork",
"rcartocolor", "scico", "showtext", "shiny",
"plotly", "highcharter", "echarts4r"))
# 安装github上的R包
devtools::install_github("JohnCoene/charter")
chic <- readr::read_csv('C:\Users\JD\Downloads\chicago-nmmaps.csv')
library(tidyverse)
names(chic)
g <- ggplot(data = chic,aes(x = date,y = temp))
# aes() 为相应的映射,也就是将变量date映射到x轴,temp变量映射到y轴,当然也可以映射其他的变量到各种各样的美学属性,如颜色大小和形状
# ggplot2 允许将绘图对象存储为变量,之后可以通过添加图层来扩展这个ggplotobject,
图形中那么会是这样的呢?
- 因为,ggplot并不知道我们希望如何绘制该数据,我们应该改添加几何图形
1 .2添加集合对象
不同图形的集合对象通常都是以geom_开头的,示例
g + geom_point() # 用来创建一个散点图
# or
ggplot(data = chic,aes(x = date,y = temp)) + geom_point()
g + geom_line()
我们也可以将多种几何图形组合起来,
g + geom_line() + geom_point()
1.3 更改几何图形的属性
在geom_* 中,我们可以设置一些属性,例如形状和颜色大小
g + geom_point(color = "firebrick",
shape = 'diamond',
size = 2)
g + geom_line() + geom_point(color = 'firebrick',
shape = 'diamond',
size = 2)
note:R 中可以将col识别为color
三原色:RGB 红绿蓝 ,也可以是RGBA,多出来的A表示透明度 表示方法rgb(255,0,0)
16禁止颜色 255对应的16进制就是FF,把三个数值一次并列起来,并且以# 开头
例如#FF0000
就是红色,#FFFF00
就是表示黄色
g + geom_point(color ='#FFFF00',
shape = 'diamond',
size = 2)
g + geom_point(color = "#FF0000",
shape = 'diamond',
size = 2)
g + geom_point(col = rgb(255,0,0,maxColorValue = 255),
shape = 'diamond',
size = 5)
每个geom对象带有参数,不同的参数变换,会得到不同的图形
g + geom_point(col = 'firebrick',shape = 'diamond',size = 3) + geom_line(col = '#FFFFF1',size = 1)
1.4改变默认的ggplot2主题
默认的ggplot2是灰色白色的主题,我们通过调用theme_set(),将后面的plot将拥有相同的主题
g + geom_point(col = 'firebrick') + theme_set(theme_bw())
g + geom_point(col = 'firebrick') + theme_bw()
想转换为之前的主题
theme_set(theme_gray())
1.5 添加坐标轴
坐标轴的使用需要是labs() 函数
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick') + labs(x = 'Year',y = 'Temperature(F)')
# 上面的等价于
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick') + xlab('Year') + ylab('Temperature(F)')
也可以采用更复杂的方式,用expression()函数建立表达式,用来添加一些数学符号
ggplot(data = chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = expression(paste('Temperature(',degree~F,")"^"12312")))
1.6 增加坐标轴与坐标轴之间的空间
theme()函数是用来修改特定主题元素的基本命令例如文本标题符号北京,我们将会大量使用他
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick') + labs(x = 'Year',y = 'Temperature') + theme(axis.title.x = element_text(vjust = 0,size =15),axis.title.y = element_text(vjust = 2,size = 15))
vjust :表示的是垂直对齐,他的范围通常是在0-1之间,或者通过指定两个文本的边距来改变距离
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = 'Temperature') + theme(axis.title.x = element_text(margin = margin(t = 10),size = 15),axis.title.y = element_text(margin=margin(r = 10),size = 15))
margin(t,r,b,l) 中的top blow,right,left,这几个参数可以调整
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = 'Temperature') + theme(axis.title = element_text(margin = margin(t = 10),size = 15))
而且还可以改变坐标轴的样式,theme()函数,修改axis.title.x,axis.title.y元素
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = 'tempature') + theme(axis.title = element_text(size = 15,face = 'italic',color = 'red'))
axis.title = element_text()中的face参数是用来调节字体的,bold粗体,italic斜体,而且axis.title 是对x轴和y轴一起修改,axis.title.x,axis.title.y是对x轴和y轴的标题进行修改
ggplot(data = chic,aes(x = date,y = temp)) + geom_point(col = 'blue') + labs(x = 'year')+theme(axis.title = element_text(face = 'bold',color = 'green',size = 20))
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'red') + labs(x = '123',y = '这个是y的标签') + theme(axis.title = element_text(color = 'blue',size = 25,face = 'bold.italic'))
g <- ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'red') + labs(x = '123',y = '这个是y的标签') + theme(axis.title = element_text(color = 'blue',size = 25,face = 'bold.italic'))
g + theme_set(theme_bw())
分别对坐标轴的x轴标签和y轴标签进行修改
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'red') + labs(x = 'Year',y = 'tempature') + theme(axis.title.x = element_text(color = 'green',face = 'bold',size = 10),axis.title.y = element_text(color = 'yellow',size = 20))
axis.tittle.x 会继承axis.title 的部分参数
1.7改变坐标轴文本的属性
使用axis.text 和axis.text.x axis.text.y来修改坐标轴刻度的外观,note 与刚才的标题不一样了,十分的不同
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = 'Temperature') + theme(axis.text = element_text(color = "dodgerblue",size = 12),axis.text.x = element_text(face = 'italic'))
1.8 旋转坐标轴文本的刻度
theme() 中的函数中具有参数angle允许你旋转文本,
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = "firebrick") + labs(x = 'Year' ,y = 'Tempterature') + theme(axis.text.x = element_text(angle = 50,vjust = 1,hjust = 1,size = 12))
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = "firebrick") + labs(x = 'Year' ,y = 'Tempterature') + theme(axis.text.x = element_text(angle = 120,vjust = 1,hjust = 1,size = 12))
不显示坐标轴
ggplot(data = chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick') + labs(x = NULL,y = "")
不显示坐标轴的刻度
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick') + labs(x = 'year',y = 'Temperature') + theme(axis.ticks.y = element_blank(),axis.text.y = element_blank())
限制坐标轴的取值范围
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'dodgerblue')
ggplot(data = chic,aes(x = date,y = temp)) + geom_point(col = 'dodgerblue') + labs(x = 'year',y = 'temeperature') + ylim(c(0,110))
chic <- readr::read_csv('C:\Users\JD\Downloads\chicago-nmmaps.csv')
ggplot(data = chic,aes(x = date,y = temp)) +
geom_point(col = 'dodgerblue') +
labs(x = 'Year',y = 'Temperature') +
ylim(c(0,60))
ggplot(chic, aes(x = date, y = temp)) +
geom_point(color = "firebrick") +
labs(x = "Year", y = "Temperature (°F)") +
ylim(c(0, 50))
将绘制的图形强制从0点开始
ggplot(data = chic,aes(x = date,y = o3)) + geom_point(col = 'dodgerblue') + labs(x = 'Year',y = 'Temperature') + coord_cartesian(xlim = c(0,NA),ylim = c(0,NA))
# date_trans works with objects of class Date only 这个是不可以的,因为其横坐标是日期型数据,所以不可能将其远点缩减为0,0
ggplot(data = chic,aes(x = temp,y = o3)) + geom_point(col = 'dodgerblue') + labs(x = 'Year',y = 'Temperature') + coord_cartesian(xlim = c(0,NA),ylim = c(0,NA))
# or
ggplot(data = chic,aes(x = temp,y = o3)) + geom_point(col = 'dodgerblue') + labs(x = 'Year',y = 'Temperature') + expand_limits(x = 0, y = 0)
将两个坐标轴保持尺度上的一致性
用到coord_equal()
ggplot(chic,aes(x = temp ,y = temp + rnorm(nrow(chic),sd = 20))) + geom_point(color = 'sienna') + labs(x = 'Temperature',y = 'Temperature + random.randn noise') + xlim(c(0,150)) + ylim(c(0,150)) + coord_fixed()
ggplot(chic,aes(x = temp ,y = temp + rnorm(nrow(chic),sd = 20))) + geom_point(color = 'sienna') + labs(x = 'Temperature',y = 'Temperature + random.randn noise') + xlim(c(0,200)) + ylim(c(0,200)) + coord_fixed()
ggplot(chic,aes(x = temp ,y = temp + rnorm(nrow(chic),sd = 20))) + geom_point(color = 'sienna') + labs(x = 'Temperature',y = 'Temperature + random.randn noise') + xlim(c(0,100)) + ylim(c(0,100)) + coord_fixed()
ggplot(chic,aes(x = temp ,y = temp + rnorm(nrow(chic),sd = 20))) + geom_point(color = 'sienna') + labs(x = 'Temperature',y = 'Temperature + random.randn noise') + xlim(c(0,100)) + ylim(c(0,100)) + coord_fixed(2)
添加标题
ggtitle() 函数可以为图形添加一个标题
ggplot(chic,aes(x = date,y = temp)) + geom_point(color = 'firebrick')+ labs(x = 'Year',y = 'Temperature') + ggtitle('Temperature in Chicage')
我觉得最好还是直接使用labs()函数直接一起带走就行
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'year',y = 'Temperature',
title = 'Temperature in Chicago',
suntitle = 'Seasonal pattern of daily temperatures from 1997 to 2001',
caption = "Data:MMAPS",
tag = 'Fig.1'
)
caption: 说明
tag: 标签
更改标题的样式
对于这些文本的样式我们都可以使用theme() 函数,来进行修改,可使用的函数有,plot.subtitle plot.caption plot.tag legend.title, legend. text axix.title axis.text后面都是跟着element_text()
ggplot(chic,aes(x = date,y = temp)) + geom_point(col = 'firebrick') + labs(x = 'Year',y = 'Temperature',title = 'Temperature in Chicago') + theme(plot.title = element_text(face = 'bold',margin = margin(10,10,0,0),size = 14))
ggplot(chic,aes(x = date,y =temp)) + geom_point(col = 'firebrick') + labs(x = 'Year' ,y = 'Temperature',title = 'Thie is a title') + theme(axis.title = element_text(size = 20),plot.title = element_text(color = 'blue', face = 'bold',size = 20))
margin 中的参数为trouble,也即top right bottom left 也就是你眼前的顺时针
ggplot(chic, aes(x = date, y = temp)) +
geom_point(color = "firebrick") +
labs(x = "Year", y = "Temperature (°F)") +
ggtitle("Temperatures in Chicago
from 1997 to 2001")
调整标题位置
ggplot(chic, aes(x = date, y = temp)) +
geom_point(color = "firebrick") +
labs(x = "Year", y = NULL,
title = "Temperatures in Chicago",
caption = "Data: NMMAPS") +
theme(plot.title = element_text(hjust = 1, size = 16, face = "bold.italic"))
ggplot(chic, aes(x = date, y = temp)) +
geom_point(color = "firebrick") +
labs(x = "Year", y = NULL,
title = "Temperatures in Chicago",
caption = "Data: NMMAPS") +
theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold.italic"))
vjust 来进行垂直控制