• 主页
  • 课程

    关于课程

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

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (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语言实践》专栏·第4篇
      文 | RUser
      2515字 |5分钟阅读
      【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:R群,我会邀请你入群,一起进步和成长。

      本文介绍ggplot2包绘制柱形图。柱形图是一种常用的数据可视化图形,使用垂直的柱子显示类别之间数值分布,x轴表示对比的分类维度,y轴表示相应的数值。柱形图的功能对比分类数据的数值大小,适合的数据是一个分类数据字段和一个连续数据字段,柱子的条数建议不要超过12条。若是条数太多,可以采用条形图(水平方向展示)或者把一些类别合并为一种情况来处理。

      利用ggplot2包绘制柱形图,步骤如下:

      首先,R包管理

      if(!require("pacman")) {  install.packages("pacman")  require("pacman")}p_load(ggplot2, ggthemes, dplyr, readr, scales, forcats)

      接下来,数据准备和加载

      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)# 数据概况summary(exports_data)# 数据检视head(exports_data)


      第四、逐步创建柱状图

      1)基础柱形图

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

      2)调整柱状的堆叠顺序

      p2 <- ggplot(data = exports_data,             aes(x = year, y = export, fill = fct_rev(product))) +  geom_col()p2


      3)给柱子添加相应的数值

      exports_data <- exports_data %>%  mutate(export_label = paste(round(export/1000000000,2), "B"))p3 <- ggplot(data = exports_data,             aes(x = year, y = export, fill = fct_rev(product))) +  geom_col() +  geom_text(data = exports_data, aes(x = year, y = export,                                     label = export_label), size = 3)p3

      4)调整数据标签的位置

      # 垂直居中p4 <- ggplot(data = exports_data,             aes(x = year, y = export, fill = fct_rev(product))) +  geom_col() +  geom_text(aes(label = export_label),             position = position_stack(vjust = 0.5),            size = 3)p4

      5) 图例管理

      exports_data <- exports_data %>%  mutate(product = factor(product, levels = c("copper","others"),                          labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))

      p5 <- ggplot(data = exports_data, aes(x = year, y = export, fill = fct_rev(product))) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), size = 3) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) + guides(fill = guide_legend(reverse = TRUE))p5


      6)调整x轴刻度

      p6 <- p5 + scale_x_continuous(breaks = seq(2006,2015,1))p6

      7)添加轴标签和标题

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


      8)柱子的填充色美化

      fill <- c("#E1B378","#5F9EA0")p8 <- p7 + scale_fill_manual(values = fill)p8


      9)配置主题

      使用经济学杂志的主题

      p_load(showtext)font_add("Tahoma","Tahoma.ttf")showtext_auto()fill <- c("#00a3dc","#01526d")p8 <- ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = exports_data) +  geom_col() +  geom_text(aes(label = export_label), position = position_stack(vjust = 0.5),            colour = "white", size = 3, family = "Tahoma",            show.legend = F) +  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_fill_manual(values = fill) +  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 = "Tahoma"),        text = element_text(family = "Tahoma")) +  guides(fill = guide_legend(reverse = TRUE))p8

      附录:完整代码

      ################柱形图#2021-03-16##############
      # 1 R包管理if(!require("pacman")) { install.packages("pacman") require("pacman")}p_load(ggplot2, ggthemes, dplyr, readr, scales, forcats)
      # 2 数据准备和加载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)
      # 3 数据理解# 数据结构str(exports_data)# 数据概况summary(exports_data)# 数据检视head(exports_data)
      # 4 逐步创建柱形图# 1)基础柱形图p1 <- ggplot(data = exports_data, aes(x = year, y = export, fill = product)) + geom_col()p1
      # 2) 调整柱状堆叠顺序p2 <- ggplot(data = exports_data, aes(x = year, y = export, fill = fct_rev(product))) + geom_col()p2
      # 3) 给柱子添加相应的数值# 新增标签变量exports_data <- exports_data %>% mutate(export_label = paste(round(export/1000000000,2), "B"))p3 <- ggplot(data = exports_data, aes(x = year, y = export, fill = fct_rev(product))) + geom_col() + geom_text(data = exports_data, aes(x = year, y = export, label = export_label), size = 3)p3
      # 4)调整数据标签的位置# 垂直居中p4 <- ggplot(data = exports_data, aes(x = year, y = export, fill = fct_rev(product))) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), size = 3)p4
      # 5) 图例管理exports_data <- exports_data %>% mutate(product = factor(product, levels = c("copper","others"), labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))

      p5 <- ggplot(data = exports_data, aes(x = year, y = export, fill = fct_rev(product))) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), size = 3) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) + theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) + guides(fill = guide_legend(reverse = TRUE))p5
      # 6) 调整x轴刻度p6 <- p5 + scale_x_continuous(breaks = seq(2006,2015,1))p6
      # 7) 添加轴标签和标题p7 <- p6 + labs(title = "Composition of Exports to China ($)", subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "USD million")p7
      # 8) 柱子填充色美化fill <- c("#E1B378","#5F9EA0")p8 <- p7 + scale_fill_manual(values = fill)p8
      # 9)配置主题# 使用经济学杂志主题p_load(showtext)font_add("Tahoma","Tahoma.ttf")showtext_auto()fill <- c("#00a3dc","#01526d")p8 <- ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = exports_data) + geom_col() + geom_text(aes(label = export_label), position = position_stack(vjust = 0.5), colour = "white", size = 3, family = "Tahoma", show.legend = F) + 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_fill_manual(values = fill) + 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 = "Tahoma"), text = element_text(family = "Tahoma")) + guides(fill = guide_legend(reverse = TRUE))p8

      好书推荐

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

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

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


      公众号推荐

      。


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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      R 主成分方法实用指南,帮助你用主成分方法做数据处理与分析
      2021年9月9日

      下一篇文章

      我做数据分析工作常用的8个R包
      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年
      在线支付 激活码

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