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


  利用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, max
p6

在这里插入图片描述

设定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, 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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷在前行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值