• 主页
  • 课程

    关于课程

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

    同等学历教学

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

      关于课程

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

      同等学历教学

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

      未分类

      • 首页
      • 博客
      • 未分类
      • ggplot2包|创建美丽有用的折线图

      ggplot2包|创建美丽有用的折线图

      • 发布者 weinfoadmin
      • 分类 未分类
      • 日期 2021年9月9日
      • 评论 0评论

      专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。

      是新朋友吗?记得先点R语言关注我哦~
      《R语言实践》专栏·第2篇
      文 | RUser
      3293字 | 5分钟阅读
      【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:R群,我会邀请你入群,一起进步和成长。

      我对于R语言做数据可视化,甚是喜欢。我也一直学习和实践着如何用R语言创建一系列美丽而有用的可视化图形。ggplot2包和以其为基础的扩展包,我都喜欢去尝试,同时,也会阅读这方面相关的资料(书籍、博客和代码等),其目的就是为了指导自己创建能够表达信息、带来价值、又具有美学的可视化图形。本文分享利用ggplot2包及相关包创建美丽而有用的折线图。折线图是一种常用的图形,可以直观展示出一种变化趋势(上升、下降、波动和保持不变等),比方说,我们经常看到用折线图表示时间序列关系,反映某个指标或者不同组下某个指标在时间轴上面的变化动态。


      本文逐步地说明,如何用ggplot2包做出美丽而可用的折线图。

      首先,加载所需R包

      if (!require("pacman")) install.packages("pacman")p_load(ggplot2, ggthemes, dplyr, readr, showtext)

      然后,数据准备工作,我们以一份出口数据集为例

      # 准备数据集chilean_exports <- "year,product,export,percentage2006,copper,4335009500,812006,others,1016726518,192007,copper,9005361914,862007,others,1523085299,142008,copper,6907056354,802008,others,1762684216,202009,copper,10529811075,812009,others,2464094241,192010,copper,14828284450,852010,others,2543015596,152011,copper,15291679086,822011,others,3447972354,182012,copper,14630686732,802012,others,3583968218,202013,copper,15244038840,792013,others,4051281128,212014,copper,14703374241,782014,others,4251484600,222015,copper,13155922363,782015,others,3667286912,22"exports_data <- read_csv(chilean_exports)# 数据结构理解str(exports_data)


      第三,根据这份数据,逐步绘制和完善折线图,用于表示不同产品类型,在不同年份里,出口量的变化趋势情况

      3.1 绘制基本的折线图

      p1 <- ggplot(data = exports_data,             aes(x = year, y = export, colour = product)) +  geom_line()p1

      大家可以思考,基于这个基础图,如何做修改和完善,从而让它更美丽和有用,以达成可以公布的目标。

      3.2 调整线条宽度

      图中每条折线图的宽度太细,有必要放大一些。

      p2 <- ggplot(data = exports_data,             aes(x = year, y = export, colour = product)) +  geom_line(size = 1.5)p2

      3.3 修改图例的标签和布局位置

      exports_data <- exports_data %>%  mutate(product = factor(product, levels = c("copper","others"),                          labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))
      p3 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) # 用于控制图例 底部 水平 不显示标题p3

      这样做的目的,让图例更有效地表达要传递的信息。

      3.4 修改x轴的刻度

      观察发现,x轴的刻度不是我们所希望表现的,我们需要它呈现一串年份序列。

      p4 <- p3 + scale_x_continuous(breaks = seq(2006, 2015, 1))p4

      3.5 设置坐标轴的标签和标题

      一幅图形x轴和y轴分别表示什么含义,以及图像所要表示的主题是什么,这可以通过坐标轴的标签和标题来说明。

      p5 <- p4 +   labs(title = "Composition of Exports to China ($)",       subtitle = "Source: The Observatory of Economic Complexity") +  labs(x = "Year", y = "USD million")p5


      3.6 调整直线的配色

      配色是一门艺术,可以参照和学习其它优秀图形的配色。

      colour <- c("#5F9EA0", "#E1B378")p6 <- p5 + scale_color_manual(values = colour)p6


      3.7 字体设置

      font_add("Tahoma","Tahoma.ttf")font_add("Roboto Condensed", "RobotoCondensed-Regular.ttf")showtext_auto()


      3.8 主题设置

      1)白色主题

      p7_1 <- ggplot(data = exports_data,               aes(x = year, y = export, colour = product)) +  geom_line(size = 1.5) +  scale_x_continuous(breaks = seq(2006, 2015, 1)) +  labs(title = "Composition of Exports to China ($)",       subtitle = "Source: The Observatory of Economic Complexity") +  labs(x = "Year", y = "USD million") +  scale_colour_manual(values = colour) +  theme_bw() +  theme(legend.position = "bottom",        legend.direction = "horizontal",        legend.title = element_blank())p7_1


      2)使用经济学杂志的主题

      p7_2 <- ggplot(data = exports_data,               aes(x = year, y = export, colour = product)) +  geom_line(size = 1.5) +  scale_x_continuous(breaks = seq(2006,2015,1)) +  labs(title = "Composition of Exports to China ($)",       subtitle = "Source: The Observatory of Economic Complexity") +  labs(x = "Year", y = "USD million") +  theme_economist() + scale_colour_economist() +  theme(axis.line.x = element_line(size = .5, colour = "black"),        legend.position = "bottom",        legend.direction = "horizontal",        legend.title = element_blank(),        plot.title = element_text(family = "Roboto Condensed"),        text = element_text(family = "Roboto Condensed"))p7_2

      3)使用Five Thirty Eight网站风格的主题

      p7_3 <- ggplot(data = exports_data,             aes(x = year, y = export, colour = product)) +  geom_line(size = 1.5) +  scale_x_continuous(breaks = seq(2006,2015,1)) +  labs(title = "Composition of Exports to China ($)",       subtitle = "Source: The Observatory of Economic Complexity") +  labs(x = "Year", y = "USD million") +  theme_fivethirtyeight() + scale_colour_fivethirtyeight() +  theme(axis.title = element_text(family = "Roboto Condensed"),        legend.position = "bottom", legend.direction = "horizontal",        legend.title = element_blank(),        plot.title = element_text(family = "Roboto Condensed"),        legend.text = element_text(family = "Roboto Condensed"),        text = element_text(family = "Roboto Condensed"))p7_3

      4)自定义主题

      完整代码:

      ##################################ggplot2包画出美丽而有用的点线图#RUser#2021-03-06################################## 加载所需R包if (!require("pacman")) install.packages("pacman")p_load(ggplot2, ggthemes, dplyr, readr, showtext)
      # 准备数据集chilean_exports <- "year,product,export,percentage2006,copper,4335009500,812006,others,1016726518,192007,copper,9005361914,862007,others,1523085299,142008,copper,6907056354,802008,others,1762684216,202009,copper,10529811075,812009,others,2464094241,192010,copper,14828284450,852010,others,2543015596,152011,copper,15291679086,822011,others,3447972354,182012,copper,14630686732,802012,others,3583968218,202013,copper,15244038840,792013,others,4051281128,212014,copper,14703374241,782014,others,4251484600,222015,copper,13155922363,782015,others,3667286912,22"exports_data <- read_csv(chilean_exports)
      # 数据结构理解str(exports_data)
      # 绘制基础的折线图p1 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line()p1
      # 调整线条的宽度p2 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5)p2
      # 修改图例变量的标签和布局的位置exports_data <- exports_data %>% mutate(product = factor(product, levels = c("copper","others"), labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))
      p3 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) # 用于控制图例 底部 水平 不显示标题p3
      # 修改x轴的刻度# 使用scale_x_continuous函数p4 <- p3 + scale_x_continuous(breaks = seq(2006, 2015, 1))p4

      # 设置坐标轴标签和图形标题p5 <- p4 + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million")p5
      # 调整配色colour <- c("#5F9EA0", "#E1B378")p6 <- p5 + scale_color_manual(values = colour)p6
      # 字体设置font_add("Tahoma","Tahoma.ttf")font_add("Roboto Condensed", "RobotoCondensed-Regular.ttf")showtext_auto()
      # 使用主题# 1)白色主题p7_1 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + scale_x_continuous(breaks = seq(2006, 2015, 1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + scale_colour_manual(values = colour) + theme_bw() + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank())p7_1
      # 2) 使用经济学角度的图片可视化p7_2 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + scale_x_continuous(breaks = seq(2006,2015,1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + theme_economist() + scale_colour_economist() + theme(axis.line.x = element_line(size = .5, colour = "black"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(), plot.title = element_text(family = "Roboto Condensed"), text = element_text(family = "Roboto Condensed"))p7_2
      # 3) Five Thirty Eightp7_3 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + scale_x_continuous(breaks = seq(2006,2015,1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + theme_fivethirtyeight() + scale_colour_fivethirtyeight() + theme(axis.title = element_text(family = "Roboto Condensed"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(), plot.title = element_text(family = "Roboto Condensed"), legend.text = element_text(family = "Roboto Condensed"), text = element_text(family = "Roboto Condensed"))p7_3
      # 4) 创建自己的主题colour <- c("#40b8d0", "#b2d183")p7_4 <- ggplot(data = exports_data, aes(x = year, y = export, colour = product)) + geom_line(size = 1.5) + scale_x_continuous(breaks = seq(2006,2015,1)) + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million") + scale_colour_manual(values = colour) + theme(panel.border = element_rect(colour = "black", fill = NA, size = .5), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.key = element_rect(fill = "white", colour = "white"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(), panel.grid.major = element_line(colour = "#d3d3d3"), panel.grid.minor = element_blank(), panel.background = element_blank(), plot.title = element_text(size = 14, family = "Tahoma", face = "bold"), text = element_text(family = "Tahoma"))p7_4


      好书推荐

      1 R语言做商业智能,助你提高商业生产率

      2 用R、tidyverse和mlr做机器学习

      3 推断统计与数据科学,moderndive和tidyverse包


      公众号推荐

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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      R For Machine Learning,从经典机器学习算法入手
      2021年9月9日

      下一篇文章

      ggplot2|主题相关函数,美化图形,总有一款适合你
      2021年9月9日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      8-1678501786
      肿瘤细胞通过改变CD8+ T细胞中的丙酮酸利用和琥珀酸信号来调控抗肿瘤免疫应答。
      7 12月, 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年
      在线支付 激活码

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