ggplot2包|气泡图
专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。
【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:姓名-R群,我会邀请你入群,一起进步和成长。
本文介绍ggplot2包绘制气泡图,气泡图,又称为权重散点图,除了具有散点图的特性外,还增加了其它变量来控制点的大小和颜色,从而让图所表达的信息更有差异化和显著性。
利用ggplot2包绘制散点图,步骤如下:
第一步:R包管理
if (!require("pacman")) {
install.packages("pacman")
require("pacman")
}
p_load(ggplot2, ggthemes, dplyr, scales, grid, showtext)
第二步:数据加载和理解
data(airquality)
# 1) 数据结构
str(airquality)
# 2)数据概况
summary(airquality)
# 3)数据检视
sample_n(airquality,10)
第三步:逐步绘制气泡图
1)基本的气泡图
在散点图的基础上,使用一个变量控制点的大小
aq_trim <- airquality %>%
filter(Month %in% c(7,8,9)) %>%
mutate(Month = factor(Month,
labels = c("July", "August", "September")))
p1 <- ggplot(data = aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point()
p1
2)修改点的形状
配置参数shape的值
p2 <- ggplot(data = aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point(shape = 21)
p2
3)修改x轴的刻度
使用scale_x_continuous函数
p3 <- p2 + scale_x_continuous(breaks = seq(1, 31, 5))
p3
4)设置轴标签和标题
p4 <- p3 +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)")
p4
5)调整点的配色
使用参数colour和fill来设置
p5 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point(shape = 21, colour = "mediumvioletred", fill = "springgreen") +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
scale_x_continuous(breaks = seq(1, 31, 5))
p5
或者
p6 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
scale_x_continuous(breaks = seq(1, 31, 5))
fill <- c("steelblue", "yellowgreen", "violetred1")
p7 <- p6 + scale_fill_manual(values = fill)
p7
6)点的大小按比例控制
使用scale_size_area()或者scale_size()函数实现成比例控制。
p8 <- p7 + scale_size_area(max_size = 10)
p8
或者
p9 <- p7 + scale_size(range=c(1,10))
p9
7)图例管理1
图例的位置和标题管理
p10 <- p9 +
theme(legend.position = "bottom", legend.direction = "horizontal") +
labs(size = "Wind Speed (mph) ", fill = "Months ")
p10
8)图例管理2
创建水平的图例
p11 <- p10 +
theme(legend.box = "vertical", legend.key.size = unit(0.5, "cm"))
p11
9)字体配置
font_add("Tahoma","Tahoma.ttf")
font_add("heiti", regular = "simhei.ttf")
showtext_auto()
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") # 印刷体
)
10)配置主题
主题1:经济学杂志主题
p12_1 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
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 = "vertical",
legend.key.size = unit(0.5, "cm"),
legend.text = element_text(size = 10),
text = element_text(family = "newman"),
plot.title = element_text(family = "newman"))
p12_1
主题2:Five Thirty Eight主题
p12_2 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
theme_fivethirtyeight() + scale_fill_fivethirtyeight() +
theme(axis.title = element_text(family = "Helvetica"),
legend.position = "bottom",
legend.direction = "horizontal",
legend.box = "vertical",
legend.key.size = unit(0.5, "cm"),
legend.title = element_text(family = "Helvetica", size = 10),
legend.text = element_text(family = "Helvetica", size = 10),
plot.title = element_text(family = "Helvetica"),
text = element_text(family = "Helvetica"))
p12_2
主题3:自定义主题
fill <- c("steelblue", "yellowgreen", "violetred1")
p12_3 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
scale_fill_manual(values = fill) +
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 = "vertical",
legend.key.size = unit(0.5, "cm"),
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 = "Tahoma", face = "bold"),
text = element_text(family = "Tahoma"))
p12_3
关于气泡图的绘制,你有什么问题,请留言。
加入R语言群,一起学习更多R语言知识和技能。
最后跟大家分享一个好消息,我最近在学习李宏毅老师的机器学习课程,真的非常地赞,强烈推荐对机器学习感兴趣或者想用机器学习的朋友也来学习这个课程。这个课程的学习笔记,可以在公众号数据科学与人工智能上面找到,你点击笔记里面的阅读原文,就可以在手机上方便地观看视频,同时,也可以加入机器学习群,大家一起讨论和交流。
你若是要找数据类的工作,或者要招聘数据类人才,可以看下公众号数据人才,它是一个数据人才助手。
附录:完整代码
##################
#气泡图
#################
# 1 R包管理
if (!require("pacman")) {
install.packages("pacman")
require("pacman")
}
p_load(ggplot2, ggthemes, dplyr, scales, grid, showtext)
# 2 数据准备和理解
data(airquality)
# 1) 数据结构
str(airquality)
# 2)数据概况
summary(airquality)
# 3)数据检视
sample_n(airquality,10)
# 3 逐步绘制气泡图
# 1)基本的气泡图
aq_trim <- airquality %>%
filter(Month %in% c(7,8,9)) %>%
mutate(Month = factor(Month,
labels = c("July", "August", "September")))
p1 <- ggplot(data = aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point()
p1
# 2) 修改点的形状
p2 <- ggplot(data = aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point(shape = 21)
p2
# 3) 修改x轴的刻度
p3 <- p2 + scale_x_continuous(breaks = seq(1, 31, 5))
p3
# 4) 设置轴标签和标题
p4 <- p3 +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)")
p4
# 5) 配置点的配色
p5 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind)) +
geom_point(shape = 21, colour = "mediumvioletred", fill = "springgreen") +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
scale_x_continuous(breaks = seq(1, 31, 5))
p5
# 或者
p6 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
scale_x_continuous(breaks = seq(1, 31, 5))
fill <- c("steelblue", "yellowgreen", "violetred1")
p7 <- p6 + scale_fill_manual(values = fill)
p7
# 6)点的大小按比例控制
p8 <- p7 + scale_size_area(max_size = 10)
p8
p9 <- p7 + scale_size(range=c(1,10))
p9
# 7)图例管理1
p10 <- p9 +
theme(legend.position = "bottom", legend.direction = "horizontal") +
labs(size = "Wind Speed (mph) ", fill = "Months ")
p10
# 8) 图例管理2
p11 <- p10 +
theme(legend.box = "vertical", legend.key.size = unit(0.5, "cm"))
p11
# 9) 字体配置
font_add("Tahoma","Tahoma.ttf")
font_add("heiti", regular = "simhei.ttf")
showtext_auto()
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") # 印刷体
)
# 10)配置主题
# 经济学杂志主题
p12_1 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
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 = "vertical",
legend.key.size = unit(0.5, "cm"),
legend.text = element_text(size = 10),
text = element_text(family = "newman"),
plot.title = element_text(family = "newman"))
p12_1
# Five Thirty Eight 主题
p12_2 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
theme_fivethirtyeight() + scale_fill_fivethirtyeight() +
theme(axis.title = element_text(family = "Helvetica"),
legend.position = "bottom",
legend.direction = "horizontal",
legend.box = "vertical",
legend.key.size = unit(0.5, "cm"),
legend.title = element_text(family = "Helvetica", size = 10),
legend.text = element_text(family = "Helvetica", size = 10),
plot.title = element_text(family = "Helvetica"),
text = element_text(family = "Helvetica"))
p12_2
# 自定义主题
fill <- c("steelblue", "yellowgreen", "violetred1")
p12_3 <- ggplot(aq_trim, aes(x = Day, y = Ozone, size = Wind, fill = Month)) +
geom_point(shape = 21) +
labs(title = "Air Quality in New York by Day",
subtitle = "Source: New York State Department of Conservation") +
labs(x = "Day of the month", y = "Ozone (ppb)", size = "Wind Speed (mph) ",
fill = "Months ") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
scale_size(range = c(1, 10)) +
scale_fill_manual(values = fill) +
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 = "vertical",
legend.key.size = unit(0.5, "cm"),
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 = "Tahoma", face = "bold"),
text = element_text(family = "Tahoma"))
p12_3
好书推荐
3 推断统计与数据科学,moderndive和tidyverse包
4 R for machine learning,从经典的机器学习算法入手
5 R for everyone,人人都可学R和用R,以发现数据里的价值
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!