【R分享|实战】 跟着科白君学画散点图就对了~
library ("pacman") # library 加载 pacman 程序包
p_load (ggplot2, ggthemes, dplyr, readr, showtext, export) # p_load 需要 pacman 包才能运行
然后,我们利用R语言自带的数据集"mpg"。
a <- mpg
# 查看数据集前 6 行,tail () 查看尾 6 行
head (a)
# 如果想看前 10 行的数据,可以改写成 head (a, 10)
# 如果想深入了解该函数用法,可以用 help () 进行访问
# 了解数据结构
str (a)
结果展示:
最后,利用这份数据,逐步绘制和完善散点图。
3.1 绘制基本的散点图
p1 <- ggplot (data = a, # ggplot 函数基础下能够画多种多样的图形
aes (x = hwy, y = displ, colour = class))+ # aes 描绘函数,对载入数据中所要表达的x, y, colour, shape等等
geom_point ( ) # 画散点图的函数 geom_point ( )
p1 # 或者 print (p1)
输出结果:
3.2 调整点的大小
图中每个散点有点小,不便于大家阅读,将其放大一些。
p2 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 3.5) # size 函数作用是改变大小
p2
输出结果:
3.3 根据不同的分组改变散点图的形状
p3 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class, shape = class))+
geom_point (size = 3.5)+
scale_shape_manual (values = c (16, 17, 18, 19, 20, 21, 22)) # scale_shape_manual 函数用来自定义对应分组散点的形状,数字表示形状类型
p3
输出结果:
p4 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, shape = 18) # 在 geom_point 函数下的 shape 表示所有点都为该形状
p4
结果展示:
其实在用我们自己实验得到的数据时,特别是平行样品较多的时候,很多样点会重叠看不清楚,这里我们可以调节散点的透明度,让重叠的点也变得可读。
3.4 调节散点图的透明度
这里需要用到 geom_point 里的 alpha 函数
p5 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, shape =18, alpha = 0.5) # alpha 函数作用是调节图形中元素的透明度
p5
结果展示:
3.5 修改x,y轴的刻度范围
图基本调节好了,但发现x,y轴有点参差不齐。x和y轴的刻度不是我们所希望表现的,需要重新设定。这里,需要用到 scale_x/y_continuous 函数。
p6 <- p5+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+ # breaks 函数表示 min,max,刻度间隔
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1)) # limits 函数表示 min, max
p6
结果展示:
3.6 设定x,y轴的标签及标题和副标等
p7 <- p6+
labs (title = "Changes of soil organic carbon with soil depth", # labs 函数用来写标签,记得必须添加引号
subtitle = "Soil organic carbon")+
labs (x = "soil depth", y = "Soil organic carbon content (g/kg)")
p7
结果展示:
图例比较小,单独放在外面不够美观。仔细观察,图例右上角还有一处空白地,咱们就把它移动进去,这样整张图就显得更加饱满了。不过图例具体如何管理还需要根据画出来的图片情况来调整,有时可以将图例放图顶或者图底,因人而异。这里,需要强调,当图例放进图中时,x, y为(0,0)是左下角,(1,1)是右上角。根据自己情况进行调整。
p8 <- p7+
theme ( legend.position = c (0.8, 0.6)) # theme 函数主要是图片内的主题等
p8
结果展示:
我们发现得到的图里有一块白色的,不太美观,再用一条图例背景代码。
p9 <- p8+
theme (legend.background = element_blank ())
p9
得到:
关于图例的其他设置,我会专门找一期来细讲。
3.8 字体设置
windowsFonts (
# 中文字体
lishu = windowsFont (family = "LiSu"), # 隶书
yahei = windowsFont (family = "Microsoft YaHei"), # 微软雅黑
xinwei = windowsFont (family = "STXingwei"), # 华文新魏
kaiti = windowsFont (family = "KaiTi"), # 楷体
heiti = windowsFont (family = "SimHei"), # 黑体
# 英文字体
arial = windowsFont (family = "Arial"), # Arial字体
newman = windowsFont (family = "Times New Roman"), #Times New Roman字体
hand = windowsFont (family = "Lucida Calligraphy"), # Lucida手写体
Helvetica = windowsFont (family = "Helvetica"), # 印刷体
)
3.9 字体大小和类型
p9_1 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size =12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
text = element_text (family ="arial"),
plot.title = element_text (family ="arial", size = 16))+ # family 函数表示字体类型,size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
p9_1
结果展示:
p9_1 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+ theme_bw ( )+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size =12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
text = element_text (family ="arial"),
plot.title = element_text (family ="arial", size = 16))+ # family 函数表示字体类型,size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
p9_1
关于背景主题的题材很多,这一期就不展开讲了。后面找一专题再具体详聊。
# export 包下的输出方式
p9_2 <- p9_1+
graph2ppt (file ="outcome.ppt", append = FALSE)
# ggplot2 包下的输出方式
p9_3 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+ theme_bw ( )+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size = 12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
plot.title = element_text (size = 16))+ # size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x = "soil depth", y ="Soil organic carbon content (g/kg)")
ggsave ("p9_3.pdf") # 由于在输出是出现了无效字体的报错,只能把有关字体的代码删除了再进行输出。
附上完整的代码:
#####################################
# ggplot2 包画散点图的详细讲解与绘制
#####################################
#1 加载所需要的R包
if (!require("pacman")) install.packages ("pacman") # 下载 pacman 程序包
library ("pacman") # library 加载 pacman 程序包
p_load (ggplot2, ggthemes, dplyr, readr, showtext, export) # p_load 需要 pacman 包才能运行
#2 加载数据集
# 选择R语言自带的数据集,<- 为赋值符号
a <- mpg
# 查看数据集前 6 行,tail () 查看尾 6 行
head (a)
# 如果想看前 10 行的数据,可以改写成 head (a, 10)
# 如果想深入了解该函数用法,可以用 help () 进行访问
# 了解数据结构
str (a)
#3 逐步分析讲解散点图
#1) 基础的散点图
p1 <- ggplot (data = a, # ggplot 函数基础下能够画多种多样的图形
aes (x = hwy, y = displ, colour = class))+ # aes 描绘函数,对载入数据中所要表达的x, y, colour, shape等等
geom_point ( ) # 画散点图的函数 geom_point ( )
p1 # 或者 print (p1)
#2) 调整点的大小
p2 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 3.5) # size 函数作用是改变大小
p2
#3) 根据不同的分组改变散点图的形状
p3 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class, shape = class))+
geom_point (size = 3.5)+
scale_shape_manual (values = c (16, 17, 18, 19, 20, 21, 22)) # scale_shape_manual 函数用来自定义对应分组散点的形状,数字表示形状类型
p3
#4) 只保留其颜色区分,并选用18号充实菱形形状来描绘
p4 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, shape = 18) # 在 geom_point 函数下的 shape 表示所有点都为该形状
p4
#5) 调节散点图的透明度
p5 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, shape =18, alpha = 0.5) # alpha 函数作用是调节图形中元素的透明度
p5
#6) 修改x,y轴的刻度范围
p6 <- p5+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+ # breaks 函数表示 min,max,刻度间隔
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1)) # limits 函数表示 min, max
p6
#7) 设定x,y轴的标签及标题和副标等
p7 <- p6+
labs (title = "Changes of soil organic carbon with soil depth", # labs 函数用来写标签,记得必须添加引号
subtitle = "Soil organic carbon")+
labs (x = "soil depth", y = "Soil organic carbon content (g/kg)")
p7
#8) 图例的管理(这期主要讲位置)
p8 <- p7+
theme ( legend.position = c (0.8, 0.6)) # theme 函数主要是图片内的主题等
p8
p9 <- p8+
theme (legend.background = element_blank ())
p9
#9) 字体设置
windowsFonts (
# 中文字体
lishu = windowsFont (family = "LiSu"), # 隶书
yahei = windowsFont (family = "Microsoft YaHei"), # 微软雅黑
xinwei = windowsFont (family = "STXingwei"), # 华文新魏
kaiti = windowsFont (family = "KaiTi"), # 楷体
heiti = windowsFont (family = "SimHei"), # 黑体
# 英文字体
arial = windowsFont (family = "Arial"), # Arial字体
newman = windowsFont (family = "Times New Roman"), #Times New Roman字体
hand = windowsFont (family = "Lucida Calligraphy"), # Lucida手写体
Helvetica = windowsFont (family = "Helvetica"), # 印刷体
)
#10) 字体大小和类型
p9_1 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size =12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
text = element_text (family ="arial"),
plot.title = element_text (family ="arial", size = 16))+ # family 函数表示字体类型,size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
p9_1
# 更换白色底背景
p9_1 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+
theme_bw ( )+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size =12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
text = element_text (family ="arial"),
plot.title = element_text (family ="arial", size = 16))+ # family 函数表示字体类型,size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
p9_1
# 输出图片
# export 包
p9_2 <- p9_1+
graph2ppt(file="outcome.ppt",append=FALSE)
# ggplot2 包
p9_3 <- ggplot (data = a,
aes (x = hwy, y = displ, colour = class))+
geom_point (size = 5, alpha = 0.5, shape = 18)+
scale_x_continuous (limits = c (10, 45), breaks = seq (10, 45, 5))+
scale_y_continuous (limits = c (1, 7), breaks = seq (1, 7, 1))+ theme_bw ( )+
theme (axis.text = element_text (size = 12, colour = "black"), # axis.text/title 表示坐标轴标签的信息
axis.title = element_text (size =12, colour = "black"), # 坐标轴的颜色默认是灰黑色并不是黑色,这里需要自己改成黑色。
legend.position = c (0.8, 0.6),
legend.background = element_blank (),
legend.text = element_text (size = 12), # 图例信息的大小
plot.title = element_text (size = 16))+ # size 表示大小
labs (title = "Changes of soil organic carbon with soil depth",
subtitle = "Soil organic carbon")+
labs ( x ="soil depth", y = "Soil organic carbon content (g/kg)")
ggsave("p9_1.pdf")
最后展示: