1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > R语言ggplot2 | 如何绘制美观的散点图

R语言ggplot2 | 如何绘制美观的散点图

时间:2023-10-21 00:23:06

相关推荐

R语言ggplot2 | 如何绘制美观的散点图

散点图

加载R包加载数据集绘制基础散点图调整点的大小根据分组类型改变散点图的形状调节散点图的透明度修改x,y轴的刻度范围设定x,y轴的标签及标题和副标等图例的管理(本位只讲位置)字体设置字体大小和类型输出图片完整代码

利用ggplot2包及相关包描绘美观且有用的散点图。散点图是一种常用的图形,可以直观展示回归分析中数据的分布和聚合情况(因变量随自变量而变化的大致趋势,进而找到变量之间的合适函数关系)。我们经常看到用散点图表示线性回归关系,进行预测分析,进而做出科学的决策。变量之间的关系有很多,如线性关系、指数关系、对数关系等等,当然,没有关系也是一种重要的关系。

加载R包

if (!require("pacman")) install.packages ("pacman") # 下载 pacman 程序包 library ("pacman") # library 加载 pacman 程序包p_load (ggplot2, ggthemes, dplyr, readr, showtext, export) # p_load 需要 pacman 包才能运行

加载数据集

# 选择R语言自带的数据集,<- 为赋值符号a <- mpg# 查看数据集前 6 行,tail () 查看尾 6 行 head (a)# 如果想看前 10 行的数据,可以改写成 head (a, 10)# 如果想深入了解该函数用法,可以用 help () 进行访问# 了解数据结构 str (a)

绘制基础散点图

p1 <- ggplot (data = a, # ggplot 函数基础下能够画多种多样的图形aes (x = hwy, y = displ, colour = class))+ # aes 描绘函数,对载入数据中所要表达的x, y, colour, shape等等geom_point ( ) # 画散点图的函数 geom_point ( ) p1 # 或者 print (p1)

在这个基础图上,我们如何进行修改和完善?我们画图的目的是让图片的比例、颜色、形状更让人赏心悦目。接下来,我将逐步对散点图主要的元素进行描绘和讲解,以便大家学习和模仿。

调整点的大小

图中每个散点有点小,不便于大家阅读,将其放大一些。

p2 <- ggplot (data = a, aes (x = hwy, y = displ, colour = class))+geom_point (size = 3.5) # size 函数作用是改变大小p2

根据分组类型改变散点图的形状

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

我们发现既对class进行颜色区分,又对其进行形状区分,这样的视觉效果并不好。于是,我们只保留其颜色区分,并选用18号充实菱形形状来描绘。

p4 <- ggplot (data = a,aes (x = hwy, y = displ, colour = class))+geom_point (size = 5, shape = 18) # 在 geom_point 函数下的 shape 表示所有点都为该形状p4

其实在用我们自己实验得到的数据时,特别是平行样品较多的时候,很多样点会重叠看不清楚,这里我们可以调节散点的透明度,让重叠的点也变得可读。

调节散点图的透明度

这里需要用到 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

随着重叠的散点数量越多,颜色深度也随之增加~

修改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, maxp6

设定x,y轴的标签及标题和副标等

这时候我们可以根据自己的需求修改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

字体设置

可以根据自己喜好调整字体,但是一般投稿的字体类型都用的Arial字体。R语言输出的图片默认的也是Arial字体。

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"), # 印刷体 )

字体大小和类型

仔细观察,现在得到的图x, y轴的标签以及字体大小都偏小,因此需要进行修改。

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

我不太喜欢R语言自带的背景色,通常都选用网格白色底。需要用到theme_bw () 函数。

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

关于背景主题的题材很多,本文就不展开讲了~

输出图片

我们可以选择 ggplot2 包的 ggsave 函数或者 export 包的 graph2ppt 函数导出ppt格式的文件。

# 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, maxp6#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 函数主要是图片内的主题等p8p9 <- 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")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。