• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      R语言

      • 首页
      • 博客
      • R语言
      • R语言画折线图?

      R语言画折线图?

      • 发布者 weinfoadmin
      • 分类 R语言
      • 日期 2019年2月27日
      测试开头

      R语言画折线图?


      笔者邀请您,先思考:

      1 折线图有什么作用?如何画折线图


      折线图(Line chart)是将数据表示为一系列称为“标记”的数据点,数据点之间由线段连接而成。它是类似散点图,除了测量点是有序的且用直线段连接。它是许多领域中常见的基本图表类型。


      一 基本折线图

      折线图

       1# 加载R包
      2library(ggplot2)
      3
      4# 构造数据集
      5df <- data.frame(dose=c("D0.5", "D1", "D2"),
      6                 len=c(4.2, 10, 29.5))
      7
      8# 1 基本折线图
      9ggplot(data=df, aes(x=dose, y=len, group=1)) +
      10  geom_line()+
      11  geom_point()
      12
      13# 2 改变线的类型
      14ggplot(data=df, aes(x=dose, y=len, group=1)) +
      15  geom_line(linetype = "dashed")+
      16  geom_point()
      17
      18# 3 改变线的颜色
      19ggplot(data=df, aes(x=dose, y=len, group=1)) +
      20  geom_line(color="red")+
      21  geom_point()
      22

      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      3的图形结果:

      R语言画折线图?


      二 折线图上添加箭头

       1# 1 添加箭头
      2library(grid)
      3ggplot(data=df, aes(x=dose, y=len, group=1)) +
      4  geom_line(arrow = arrow())+
      5  geom_point()
      6
      7# 2 折线图两端添加箭头
      8myarrow <- arrow(angle = 15, ends = "both", type = "closed")
      9ggplot(data=df, aes(x=dose, y=len, group=1)) +
      10  geom_line(arrow=myarrow)+
      11  geom_point()
      12




      1的图形结果:


      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      三 分组折线图

       1df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
      2                  dose=rep(c("D0.5", "D1", "D2"),2),
      3                  len=c(6.8, 15, 33, 4.2, 10, 29.5))
      4#1 多组折线图
      5ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
      6  geom_line()+
      7  geom_point()
      8#2 改变直线类型
      9ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
      10  geom_line(linetype="dashed", color="blue", size=1.2)+
      11  geom_point(color="red", size=3)
      12


      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      四 利用分组改变折线的类型

      1#1 通过分组改变线的类型
      2ggplot(df2, aes(x=dose, y=len, group=supp)) +
      3  geom_line(aes(linetype=supp))+
      4  geom_point()
      5
      6#2 通过分组改变线的类型和点的形状
      7ggplot(df2, aes(x=dose, y=len, group=supp)) +
      8  geom_line(aes(linetype=supp))+
      9  geom_point(aes(shape=supp))


      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      五 使用函数scale_linetype_manual()手动更改线的类型

      1# 手动设置线的类型
      2ggplot(df2, aes(x=dose, y=len, group=supp)) +
      3  geom_line(aes(linetype=supp))+
      4  geom_point()+
      5  scale_linetype_manual(values=c("twodash", "dotted"))


      图形结果:

      R语言画折线图?

      六 分组改变线的颜色

      1# 改变线的颜色
      2
      3p <- ggplot(df2, aes(x=dose, y=len, group=supp)) +
      4  geom_line(aes(color=supp))+
      5  geom_point(aes(color=supp))
      6p
      7


      图形结果:

      R语言画折线图?

      七 利用函数手动改变线的颜色

      1# 1 自定义调色板
      2p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
      3# 2 使用现成的调色板
      4p+scale_color_brewer(palette="Dark2")
      5# 3 使用灰度
      6p + scale_color_grey() + theme_classic()

      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      3的图形结果:

      R语言画折线图?

      八 改变折线图的图例默认位置

      1p <- p + scale_color_brewer(palette="Paired")+
      2  theme_minimal()
      3# 1 图例在上方
      4p + theme(legend.position="top")
      5# 2 图例在下方
      6p + theme(legend.position="bottom")
      7# 3 移除图例
      8p + theme(legend.position="none")

      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      3的图形结果:

      R语言画折线图?


      九 x轴是连续值的折线图

       1df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
      2                  dose=rep(c("0.5", "1", "2"),2),
      3                  len=c(6.8, 15, 33, 4.2, 10, 29.5))
      4
      5# x轴作为连续变量
      6df2$dose <- as.numeric(as.vector(df2$dose))
      7ggplot(data=df2, aes(x=dose, y=len, group=supp, color=supp)) +
      8  geom_line() + 
      9  geom_point()+
      10  scale_color_brewer(palette="Paired")+
      11  theme_minimal()

      图形结果:

      R语言画折线图?

      十 x轴是日期的折线图

      1head(economics)
      2ggplot(data=economics, aes(x=date, y=pop))+
      3  geom_line()

      图形结果:

      R语言画折线图?

      十一 带有误差条的折线图

       1data_summary <- function(data, varname, groupnames){
      2  require(plyr)
      3  summary_func <- function(x, col){
      4    c(mean = mean(x[[col]], na.rm=TRUE),
      5      sd = sd(x[[col]], na.rm=TRUE))
      6  }
      7  data_sum<-ddply(data, groupnames, .fun=summary_func,
      8                  varname)
      9  data_sum <- rename(data_sum, c("mean" = varname))
      10  return(data_sum)
      11}
      12
      13df3 <- data_summary(ToothGrowth, varname="len", 
      14                    groupnames=c("supp", "dose"))
      15
      16# 1 均值的标准差
      17ggplot(df3, aes(x=dose, y=len, group=supp, color=supp)) + 
      18  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1) +
      19  geom_line() + 
      20  geom_point()+
      21  scale_color_brewer(palette="Paired")+
      22  theme_minimal()
      23
      24
      25# 2 使用position_dodge水平移动重叠的误差条
      26ggplot(df3, aes(x=dose, y=len, group=supp, color=supp)) + 
      27  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1, 
      28                position=position_dodge(0.05)) +
      29  geom_line() + 
      30  geom_point()+
      31  scale_color_brewer(palette="Paired")+
      32  theme_minimal()

      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      十二 自定义折线图

       1# 1 简单的折线图
      2# 通过分组改变点的形状和线的类型
      3ggplot(df3, aes(x=dose, y=len, group = supp, shape=supp, linetype=supp))+ 
      4  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1, 
      5                position=position_dodge(0.05)) +
      6  geom_line() +
      7  geom_point()+
      8  labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")+
      9  theme_classic()
      10
      11# 2 分组改变颜色和添加误差条
      12p <- ggplot(df3, aes(x=dose, y=len, group = supp, color=supp))+ 
      13  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1, 
      14                position=position_dodge(0.05)) +
      15  geom_line(aes(linetype=supp)) + 
      16  geom_point(aes(shape=supp))+
      17  labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")+
      18  theme_classic()
      19p + theme_classic() + scale_color_manual(values=c('#999999','#E69F00'))
      20

      1的图形结果:

      R语言画折线图?

      2的图形结果:

      R语言画折线图?

      参考资料:

      1 ggplot2折线图

      R语言画折线图?

      内容推荐


      • 如何阅读论文?

      • 论文管理工具,我用Zotero

      • 一个数据人的2018

      • R机器学习介绍第一部分

      • R机器学习介绍第二部分

      • R中你应该学习的7种可视化

      • R中用线性回归进行预测建模

      • 使用RMarkdown沟通结果

      • 使用LIME探索模型

      • RStudio1.2新功能介绍

      • 使用spotifyr聚类Springsteen专辑

      • 使用R和tidytext对Trustpilot 的评论进行主题建模

      • R语言做深度学习

      数据人网:数据人学习,交流和分享的平台,诚邀您创造和分享数据知识,共建和共享数据智库。

      测试结尾

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      使用R的caret对银行定期存款订阅进行分类
      2019年2月27日

      下一篇文章

      超简单的中文数据画图(R语言)
      2019年2月28日

      你可能也喜欢

      3-1665801675
      R语言学习:重读《R数据科学(中文版)》书籍
      28 9月, 2022
      6-1652833487
      经典铁死亡,再出新思路
      16 5月, 2022
      1-1651501980
      R语言学习:阅读《R For Everyone 》(第二版)
      1 5月, 2022

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?