• 如何用R语言ggplot2画折线图



    前言

    在这里插入图片描述


    一、数据集

    数据下载链接见文章顶部

    数据:

    在这里插入图片描述

    library(readxl)
    
    data <- read_excel("fig3_line.xlsx", sheet = "Sheet1")
    

    二、ggplot2画图

    1、全部代码

    library(ggplot2)
    
    pic =
        ggplot(data, aes(x = x, y = y, group = Species, color = Species)) +
        geom_line(linewidth = 1.5) +
        # 主题设置
        theme_classic() +
        # 轴设置
        theme(axis.text = element_text(size = 20)) +
        theme(axis.title = element_text(size = 22)) +
        coord_cartesian(xlim = c(0, 10)) +
        ylab("Percent") +
        xlab("Production") +
        scale_y_continuous(labels = scales::percent) +
        scale_x_continuous(breaks = seq(0, 10, by = 2)) +
        # 图例设置
        theme(legend.title = element_blank()) +
        theme(legend.text = element_text(size = 18, face = "italic")) + 
        theme(legend.position = c(.68, .78)) +
        # 颜色
        scale_color_manual(values = c('#73c0de', '#5470C6', '#3ba272', '#fac858', '#ee6666', '#fc8452', '#9a60b4', '#ea7ccc'))
    
    jpeg(filename = "test3.png", width = 3300, height = 3000, res = 600, quality = 100)
    pic
    dev.off()
    

    2、细节拆分

    1)导包

    library(ggplot2)
    

    2)创建图形对象

    pic =
        ggplot(data, aes(x = x, y = y, group = Species, color = Species)) +
        geom_line(linewidth = 1.5)
    
    • 设置 x 轴为列 x,y 轴为列 y,按物种类型为线条着色。
    • geom_line 用于绘制线图,并通过 linewidth = 1.5 参数设置线条的宽度为1.5个单位(毫米)。在老版本的 ggplot2 中是通过 size 参数调整线宽的,即 size = 1.5,而在新版本中,推荐用 linewidth 替换 size。

    3)主题设置

    theme_classic()
    
    • theme_classic 指定经典主题。

    4)轴设置

    theme(axis.text = element_text(size = 20)) +
    theme(axis.title = element_text(size = 22)) +
    coord_cartesian(xlim = c(0, 10)) +
    ylab("Percent") +
    xlab("Production") +
    scale_y_continuous(labels = scales::percent) +
    scale_x_continuous(breaks = seq(0, 10, by = 2)) +
    
    • 设置轴刻度字号20,轴标题字号22。
    • coord_cartesian(xlim = c(0, 10)) 用于限制 x 轴坐标轴的范围,只显示从0到10的区间。这意味着即使数据中存在超出这个范围的值,图形也只会显示在这个范围内。该函数与xlim(0, 10)的区别是:xlim 函数会先删除超出范围的数据点再绘制;而 coord_cartesian 函数会用所有的点绘制,但只展示 y∈[0,10] 区间的图像。在本图中用这两个函数绘制效果没有区别,但在绘制箱线图时,这两个函数绘制的图中中位数和四分位点可能截然不同。
    • xlab 设置 x 轴标题,ylab 设置 x 轴标题。
    • scale_y_continuous() 函数用于设置y轴的连续型变量的比例尺。
      labels = scales::percent 表示将y轴的标签格式化为百分比形式,即将数值乘以100并加上百分号符号。
    • scale_x_continuous() 函数用于设置x轴的连续型变量的比例尺。
      breaks = seq(0, 10, by = 2) 表示设置x轴刻度的位置。在这个例子中,刻度位置从0开始,每隔2个单位设置一个刻度,一直到10。

    5)图例设置

    theme(legend.title = element_blank()) +
    theme(legend.text = element_text(size = 18, face = "italic")) + 
    theme(legend.position = c(.68, .78))
    
    • 设置图例标题为空。
    • 设置图例字体为18号斜体。
    • .68 和 .78 即 0.68 和 0.78,表示图例中心点位置相对于整个图表的坐标系的百分比。

    6)颜色

    scale_color_manual(values = c('#73c0de', '#5470C6', '#3ba272', '#fac858', '#ee6666', '#fc8452', '#9a60b4', '#ea7ccc'))
    
    • 分别为每个物种类别的线条指定颜色。

    7)保存图片

    jpeg(filename = "test3.png", width = 3300, height = 3000, res = 600, quality = 100)
    pic
    dev.off()
    
    • jpeg 函数打开了一个JPEG设备,设定了图片的保存路径为 “test3.png”,图片的宽度为3300像素,高度为3000像素,分辨率为600 dpi,图片质量为100%。
    • pic 是之前生成的图形对象。
    • dev.off() 关闭了之前打开的图形设备,保存了图片到指定路径。这是在完成图片保存后必须执行的步骤,以确保保存的图片被正确地输出。
  • 相关阅读:
    如何做到安全上网
    chatgpt相关问题解答
    CANoe-如何基于CAN协议在诊断控制台诊断通信(5000字保姆级教程)
    2022国赛D题气象报文信息卫星通信传输参考代码及思路
    Mysql 以字符分割一行变多行(substring_index函数)
    Ubuntu环境下基于libxl库文件使用C++实现对表格的操作
    js基本包装类型
    二阶系统时域响应
    博客的评论与回复功能的实现
    day01(Flume)
  • 原文地址:https://blog.csdn.net/BingJH2018212666/article/details/139573914