• 主页
  • 课程

    关于课程

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

    同等学历教学

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

      伙伴们,我最近在重温李宏毅老师的机器学课程,并且做了一份学习笔记。你若是想学习和应用机器学习,强烈推荐你进入数据科学与人工智能公众号,查看我的学习笔记,亮点是1)点击阅读原文,就可以在手机上非常方便地观看课程视频;2)加入配套的机器学习群,参与讨论和交流,让我们一起来学习和应用机器学习。


      我们先回顾一下,我已经总结和分享了ggplot2包绘制折线图、面积图、柱形图、散点图和气泡图,相关文章如下:

      1 ggplot2包|折线图

      2 ggplot2包|面积图

      3 ggplot2包|柱形图

      4 ggplot2包|散点图

      5 ggplot2包|气泡图

      我们在做可视化的时候,根据变量的类型、变量的个数、变量的关系,选择合适可视化图形,以达成有效、简洁、美丽地表达信息和结果。

      本文介绍ggplot2包绘制直方图。直方图,大家不陌生,它是一种常用图形,用于观察和表示单个连续变量的分布。我们绘制直方图的时候,首先要对数据分组或者分箱,然后统计每个组的频数,最后绘制成图形。直方图的作用1)能够显示各组频数或数量分布的情况;2)易于显示各组频数或数量的差异。直方图还可以发现那些数据比较集中,异常和孤立的数据分布在何处。直方图主要应用场景1)观察单一连续变量的分布;2)观察异常和孤立数据。

      利用ggplot2包绘制直方图,步骤如下:

      第一步:R包管理

      if (!require("pacman")) {  install.packages("pacman")  require("pacman")}p_load(datasets, ggplot2, ggthemes, dplyr, grid, RColorBrewer)

      第二步:数据加载和理解

      使用data()函数导R语言自带数据集airquality。

      使用glimpse()函数观察数据结构,summary()函数了解数据概况,sample_frac()函数做随机按比例检视数据。

      data(airquality)glimpse(airquality)summary(airquality)sample_frac(airquality,0.1)

      第三步:逐步绘制直方图

      1)基本直方图

      p1 <- ggplot(airquality, aes(x = Ozone)) +   geom_histogram()p1

      2)添加正态密度曲线

      p2 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..density..)) +  stat_function(fun = dnorm, colour = "red",                args = list(mean = mean(airquality$Ozone, na.rm = TRUE),                            sd = sd(airquality$Ozone, na.rm = TRUE)))p2


      3)把密度分布改成频数,效果和基本直方图一样

      p3 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..))p3

      4)调整分组宽度

      p4 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5)p4


      5)调整轴标签

      5.1)一行展示

      p5 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone in parts per billion") +  scale_y_continuous(name = "Count")p5


      5.2)换行展示

      p5_1 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone innparts per billion") +  scale_y_continuous(name = "Count")p5_1

      6)修改x轴的刻度和范围

      p6 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25),                      limits = c(0, 175)) +  scale_y_continuous(name = "Count")p6

      7)添加标题

      p7 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25),                      limits = c(0, 175)) +  scale_y_continuous(name = "Count")  +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation")p7

      8)调整柱子的颜色

      barfill = "gold1" #填充的颜色barlines = "black" #柱子四周的颜色p8 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5,                 colour = barlines, fill = barfill) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation")p8

      或者

      通过HEX编码

      barfill <- "#4271AE"barlines <- "#1F3552"p8_1 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5,                 colour = barlines, fill = barfill) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation")p8_1

      9)颜色渐变

      通过把aes(y = ..count..)改成aes(fill = ..count..)即可

      p9 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(fill = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation")p9

      或者

      使用scale_fill_gradient()美化渐变颜色

      p9_1 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(fill = ..count..), binwidth = 5) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  scale_fill_gradient("Count", low = "blue", high = "red")p9_1

      10)字体配置

      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")      # 印刷体)

      11)主题风格装饰

      主题一:经济学主题(我很喜欢的一种风格)

      p11_1 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 5,                 colour = barlines, fill = barfill) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  theme_economist() + scale_fill_economist() +  theme(axis.line.x = element_line(size = .5, colour = "black"),        axis.title = element_text(size = 12),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.text = element_text(size = 10),        text = element_text(family = "arial"),        plot.title = element_text(family = "arial"))p11_1

      主题二:自定义风格

      p11_2 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 10,                 colour = "black", fill = "#56B4E9") +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  theme(axis.line.x = element_line(size = 1, colour = "black"),        axis.line.y = element_line(size = 1, colour = "black"),        axis.text.x = element_text(colour = "black", size = 10),        axis.text.y = element_text(colour = "black", size = 10),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.key = element_blank(),        panel.grid.major = element_blank(),        panel.grid.minor = element_blank(),        panel.background = element_blank(),        plot.title = element_text(family = "Helvetica"),        text = element_text(family = "Helvetica"))p11_2


      12)添加一个cut-off的直线

      p12 <- ggplot(airquality, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 10,                 colour = "black", fill = "#56B4E9") +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  geom_vline(xintercept = 75, size = 1, colour = "#FF3721",             linetype = "dashed") +  theme(axis.line.x = element_line(size = 1, colour = "black"),        axis.line.y = element_line(size = 1, colour = "black"),        axis.text.x = element_text(colour = "black", size = 10),        axis.text.y = element_text(colour = "black", size = 10),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.key = element_blank(),        panel.grid.major = element_blank(),        panel.grid.minor = element_blank(),        panel.background = element_blank(),        plot.title = element_text(family = "Helvetica"),        text = element_text(family = "Helvetica"))p12

      13)多个直方图对比观察

      13.1)分面板观察

      p13_1 <- ggplot(airquality_trimmed, aes(x = Ozone)) +  geom_histogram(aes(y = ..count..), binwidth = 10,                 colour = barlines, fill = barfill) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  facet_grid(. ~ Month.f, scales = "free") +  theme(panel.border = element_rect(colour = "black", fill = NA, size = .5),        axis.text.x = element_text(colour = "black", size = 9),        axis.text.y = element_text(colour = "black", size = 9),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.key = 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 = "yahei", face = "bold"),        text = element_text(family = "yahei"))p13_1

      13.2)在同一个图形里面观察

      p13_2 <- ggplot(airquality_trimmed, aes(x = Ozone, fill = Month.f)) +  geom_histogram(aes(y = ..count..), binwidth = 10,                 position = "identity", alpha = 0.75) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  scale_fill_brewer(palette = "Accent") +  theme(panel.border = element_rect(colour = "black", fill = NA, size = .5),        axis.text.x = element_text(colour = "black", size = 9),        axis.text.y = element_text(colour = "black", size = 9),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.key = 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 = "yahei", face = "bold"),        text = element_text(family = "yahei"))p13_2

      14)图例管理

      p14 <- ggplot(airquality_trimmed, aes(x = Ozone, fill = Month.f)) +  geom_histogram(aes(y = ..count..), binwidth = 10,                 position = "identity", alpha = 0.75) +  scale_x_continuous(name = "Mean ozone innparts per billion",                     breaks = seq(0, 175, 25), limits = c(0, 175)) +  scale_y_continuous(name = "Count") +  labs(title = "Frequency histogram of mean ozone",       subtitle = "Source: New York State Department of Conservation") +  scale_fill_brewer(palette = "Accent") +  theme(panel.border = element_rect(colour = "black", fill = NA, size = .5),        axis.text.x = element_text(colour = "black", size = 9),        axis.text.y = element_text(colour = "black", size = 9),        legend.position = "bottom",        legend.direction = "horizontal",        legend.box = "horizontal",        legend.key = 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 = "yahei", face = "bold"),        text = element_text(family = "yahei")) +  labs(fill='Month')p14



      关于直方图的绘制,你有什么问题,请留言。

      加入R语言群,一起学习更多R语言知识和技能。

      你若是要找数据类的工作,或者要招聘数据类人才,可以看下公众号数据人才,它是一个数据人才助手。


      附录:完整代码

      ##############直方图#############
      # 1 R包管理if (!require("pacman")) { install.packages("pacman") require("pacman")}p_load(datasets, ggplot2, ggthemes, dplyr, grid, RColorBrewer)
      # 2 数据加载和理解data(airquality)glimpse(airquality)summary(airquality)sample_frac(airquality,0.1)
      # 3 逐步绘制直方图# 1)基本直方图p1 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram()p1
      # 2) 添加正态密度曲线p2 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..density..)) + stat_function(fun = dnorm, colour = "red", args = list(mean = mean(airquality$Ozone, na.rm = TRUE), sd = sd(airquality$Ozone, na.rm = TRUE)))p2
      # 3)把分布密度改成频数p3 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..))p3
      # 4)调整分组宽度p4 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5)p4
      # 5)调整轴标签p5 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone in parts per billion") + scale_y_continuous(name = "Count")p5# 或者p5_1 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone innparts per billion") + scale_y_continuous(name = "Count")p5_1
      # 6) 修改x轴的刻度p6 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count")p6
      # 7)添加标题p7 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation")p7
      # 8)改变柱子的颜色barfill = "gold1" #填充的颜色barlines = "black" #柱子四周的颜色p8 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5, colour = barlines, fill = barfill) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation")p8# 或者barfill <- "#4271AE"barlines <- "#1F3552"p8_1 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5, colour = barlines, fill = barfill) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation")p8_1
      # 9) 颜色渐变p9 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(fill = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation")p9# 或者p9_1 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(fill = ..count..), binwidth = 5) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + scale_fill_gradient("Count", low = "blue", high = "red")p9_1
      # 10) 字体配置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") # 印刷体)

      # 11)主题风格装饰p11_1 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 5, colour = barlines, fill = barfill) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + theme_economist() + scale_fill_economist() + theme(axis.line.x = element_line(size = .5, colour = "black"), axis.title = element_text(size = 12), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.text = element_text(size = 10), text = element_text(family = "arial"), plot.title = element_text(family = "arial"))p11_1
      # 自定义风格p11_2 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 10, colour = "black", fill = "#56B4E9") + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + theme(axis.line.x = element_line(size = 1, colour = "black"), axis.line.y = element_line(size = 1, colour = "black"), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.key = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), plot.title = element_text(family = "Helvetica"), text = element_text(family = "Helvetica"))p11_2
      # 12) 添加一个cut off的直线p12 <- ggplot(airquality, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 10, colour = "black", fill = "#56B4E9") + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + geom_vline(xintercept = 75, size = 1, colour = "#FF3721", linetype = "dashed") + theme(axis.line.x = element_line(size = 1, colour = "black"), axis.line.y = element_line(size = 1, colour = "black"), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.key = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), plot.title = element_text(family = "Helvetica"), text = element_text(family = "Helvetica"))p12
      # 13) 多个直方图对比观察# 13.1)分面板观察# 数据处理airquality_trimmed <- airquality %>% filter(Month %in% c(5,7)) %>% mutate(Month.f = factor(Month, labels = c("May", "July")))p13_1 <- ggplot(airquality_trimmed, aes(x = Ozone)) + geom_histogram(aes(y = ..count..), binwidth = 10, colour = barlines, fill = barfill) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + facet_grid(. ~ Month.f, scales = "free") + theme(panel.border = element_rect(colour = "black", fill = NA, size = .5), axis.text.x = element_text(colour = "black", size = 9), axis.text.y = element_text(colour = "black", size = 9), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.key = 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 = "yahei", face = "bold"), text = element_text(family = "yahei"))p13_1# 同一个图形观察p13_2 <- ggplot(airquality_trimmed, aes(x = Ozone, fill = Month.f)) + geom_histogram(aes(y = ..count..), binwidth = 10, position = "identity", alpha = 0.75) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + scale_fill_brewer(palette = "Accent") + theme(panel.border = element_rect(colour = "black", fill = NA, size = .5), axis.text.x = element_text(colour = "black", size = 9), axis.text.y = element_text(colour = "black", size = 9), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.key = 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 = "yahei", face = "bold"), text = element_text(family = "yahei"))p13_2
      # 14)图例管理p14 <- ggplot(airquality_trimmed, aes(x = Ozone, fill = Month.f)) + geom_histogram(aes(y = ..count..), binwidth = 10, position = "identity", alpha = 0.75) + scale_x_continuous(name = "Mean ozone innparts per billion", breaks = seq(0, 175, 25), limits = c(0, 175)) + scale_y_continuous(name = "Count") + labs(title = "Frequency histogram of mean ozone", subtitle = "Source: New York State Department of Conservation") + scale_fill_brewer(palette = "Accent") + theme(panel.border = element_rect(colour = "black", fill = NA, size = .5), axis.text.x = element_text(colour = "black", size = 9), axis.text.y = element_text(colour = "black", size = 9), legend.position = "bottom", legend.direction = "horizontal", legend.box = "horizontal", legend.key = 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 = "yahei", face = "bold"), text = element_text(family = "yahei")) + labs(fill='Month')p14

      好书推荐

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

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

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

      4 R for machine learning,从经典的机器学习算法入手

      5 R for everyone,人人都可学R和用R,以发现数据里的价值

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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      你喜欢用R和ggplot2做数据可视化吗?
      2021年9月9日

      下一篇文章

      ceshi
      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年
      在线支付 激活码

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