R-ggplot2-如何绘制散点密度图并把图例放图里?
2020-02-08 本文已影响0人
TroyShen
目录
- 0.问题导入
- 1.示例数据
- 2.绘制单幅散点密度图
- 3.将图例放置于图中
- 4.多幅图例独立散点密度图绘制暨bins影响说明
- 5.给图件增加子图标签
- 6.总结
- 7.本篇使用的R-packages(没有的小伙伴需要通过install.packages("包名")进行安装)
- 8.致谢
- 9.号外:技术公众号【TheWhoOPs】上线啦~~
0. 问题导入
在日常绘图过程中,我们可能需要在散点图的基础上通过颜色区别某一区域的密度,即散点密度图,那么应该如何绘制呢?本篇给出解决方案。此外,也将解决多幅散点密度图放到一副图中,并将各图例置于各幅子图中的问题。
1. 示例数据
本篇示例数据采用R中ggplot2包中内置的diamonds数据集。
data(diamonds)
head(diamonds)
# A tibble: 6 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
2. 绘制单幅散点密度图
如图1,格子太大了!细节信息体现不出来?这是为什么呢?原因如下:
geom_bin2d函数中的bins变量设置过小
为了体现bins参数对于图件信息展示的影响,本篇在第四节以多幅散点图绘制为例说明。
p1 = ggplot(data = diamonds,aes(x = carat,y = price))+
geom_bin2d(bins = 10)+
scale_fill_distiller(palette="Spectral", direction=-1)+
theme_bw()+
theme(
axis.text = element_text(size = 12),
axis.title = element_text(face= 'bold',size = 14),
legend.text = element_text(size = 12),
legend.title = element_text(face= 'bold',size = 14)
)+
xlab('Carat')+
ylab('Price')
png('plot1.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p1)
dev.off()

3. 将图例放置于图中
p2 = ggplot(data = diamonds,aes(x = carat,y = price))+
geom_bin2d(bins = 10)+
scale_fill_distiller(palette="Spectral", direction=-1)+
theme_bw()+
theme(
legend.position = c(.95, .02),
legend.justification = c("right", "bottom"),
legend.box.just = "right",
legend.key.height = unit(3,'lines'),
#legend.margin = margin(6, 6, 6, 6),
legend.background = element_rect(fill = 'transparent'))+
theme(
axis.text = element_text(size = 12),
axis.title = element_text(face= 'bold',size = 14),
legend.text = element_text(size = 12),
legend.title = element_text(face= 'bold',size = 14)
)+
xlab('Carat')+
ylab('Price')
png('plot2.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p2)
dev.off()

4. 多幅图例独立散点密度图绘制暨bins影响说明
如图3所示,随着bins 参数的增加,散点密度散点图会变得越精细,并在第二行第1幅图后,密度无再显著变化。第二行第1幅图?是不是觉得很别扭?那接着第5节,我们就将给图件增加子图标签。
p3 = ggplot(data = diamonds,aes(x = carat,y = price))+
scale_fill_distiller(palette="Spectral", direction=-1)+
theme_bw()+
theme(
legend.position = c(.95, .02),
legend.justification = c("right", "bottom"),
legend.box.just = "right",
legend.key.height = unit(1,'lines'),
#legend.margin = margin(6, 6, 6, 6),
legend.background = element_rect(fill = 'transparent'))+
theme(
axis.text = element_text(size = 12),
axis.title = element_text(face= 'bold',size = 14),
legend.text = element_text(size = 12),
legend.title = element_text(face= 'bold',size = 14)
)+
ylab("Price")
# Density Plot
p3_10 = p3+ geom_bin2d(bins = 10)+xlab('Carat Bins = 10')
p3_25 = p3+ geom_bin2d(bins = 25)+xlab('Carat Bins = 25')
p3_50 = p3+ geom_bin2d(bins = 50)+xlab('Carat Bins = 50')
p3_75 = p3+ geom_bin2d(bins = 75)+xlab('Carat Bins = 75')
p3_90 = p3+ geom_bin2d(bins = 90)+xlab('Carat Bins = 90')
p3_100 = p3+ geom_bin2d(bins = 100)+xlab('Carat Bins = 100')
png('plot3.png',
height = 20,
width = 30,
units = 'cm',
res = 800)
grid.arrange(
p3_10,p3_25,p3_50,p3_75,p3_90,p3_100,
ncol = 3,
nrow = 2
)
dev.off()

5. 给图件增加子图标签
这样是不是就好多啦~根据图4d-f,当bins参数大于75后,散点密度图密度增加不再显著。
p4 = ggplot(data = diamonds,aes(x = carat,y = price))+
scale_fill_distiller(palette="Spectral", direction=-1)+
theme_bw()+
theme(
legend.position = c(.95, .02),
legend.justification = c("right", "bottom"),
legend.box.just = "right",
legend.key.height = unit(1,'lines'),
#legend.margin = margin(6, 6, 6, 6),
legend.background = element_rect(fill = 'transparent'))+
theme(
axis.text = element_text(size = 12),
axis.title = element_text(face= 'bold',size = 14),
legend.text = element_text(size = 12),
legend.title = element_text(face= 'bold',size = 14),
title = element_text(face= 'bold',size = 14)
)+
ylab("Price")
# Density Plot
p4_10 = p4+ geom_bin2d(bins = 10)+xlab('Carat')+ggtitle("(a) Bins = 10")
p4_25 = p4+ geom_bin2d(bins = 25)+xlab('Carat')+ggtitle("(b) Bins = 25")
p4_50 = p4+ geom_bin2d(bins = 50)+xlab('Carat')+ggtitle("(c) Bins = 50")
p4_75 = p4+ geom_bin2d(bins = 75)+xlab('Carat')+ggtitle("(d) Bins = 75")
p4_90 = p4+ geom_bin2d(bins = 90)+xlab('Carat')+ggtitle("(e) Bins = 90")
p4_100 = p4+ geom_bin2d(bins = 100)+xlab('Carat')+ggtitle("(f) Bins = 100")
png('plot4.png',
height = 20,
width = 30,
units = 'cm',
res = 800)
grid.arrange(
p4_10,p4_25,p4_50,p4_75,p4_90,p4_100,
ncol = 3,
nrow = 2
)
dev.off()

6. 总结
本篇主要解决了以下技术问题:
- 如何绘制密度散点图?
- 如何将图例绘制于密度散点图中?
- 如何将多幅图例独立密度散点图绘制在一幅图中?
- 如何为多幅图例独立密度散点图增加子图标签?
7. 本篇使用的R-packages(没有的小伙伴需要通过install.packages("包名")进行安装)
library(ggplot2)
library(gridExtra)
library(grid)
8. 致谢
首先,感谢大家的持续关注,小编会继续努力,持续更新下去的!
大家如果觉得有帮助啊,还麻烦大家关注点赞,也可以扩散到朋友圈,多多引导朋友加入咱们这个简书技术平台, 代码共享推动科研进程, 多谢大家啦~
大家如果在使用本代码的过程有遇到问题的,可以留言评论,也可以私信我哈~~
祝大家元宵节快乐,身体健康!!

9. 号外:技术公众号【TheWhoOPs】上线啦~~
为了方便大家的阅读方便,技术公众号【TheWhoOPs】已经上线啦!!欢迎大家关注及转发哈,感谢大家鼎力支持!!
