ggplot2包|直方图
专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。
【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:姓名-R群,我会邀请你入群,一起进步和成长。
伙伴们,我最近在重温李宏毅老师的机器学课程,并且做了一份学习笔记。你若是想学习和应用机器学习,强烈推荐你进入数据科学与人工智能公众号,查看我的学习笔记,亮点是1)点击阅读原文,就可以在手机上非常方便地观看课程视频;2)加入配套的机器学习群,参与讨论和交流,让我们一起来学习和应用机器学习。
我们先回顾一下,我已经总结和分享了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
好书推荐
3 推断统计与数据科学,moderndive和tidyverse包
4 R for machine learning,从经典的机器学习算法入手
5 R for everyone,人人都可学R和用R,以发现数据里的价值
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!