数据可视化—如何利用”奶牛“,把图组合起来

作者:小橙子

来源:小橙子学数据分析

今天要给大家介绍的是cowplot包(作者:Claus O. Wilke),针对ggplot2的功能扩展包。能把多个ggplot2的图,组合起来


 比如利用cowplot包,把"mpg"的车辆信息,根据"整体"与"drv",按照"总-分"的逻辑,显示在同一张图中:

 
 
1library(ggplot2)2library(cowplot) #奶牛画图包3library(ggthemes) #ggplot中的主题包,囊括了很多经典图表形式,比如经济学人、538、华尔街等等45p <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + theme_economist() #利用了经济学人的主题6q <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + facet_grid(drv ~ .) + theme_economist()78plot_grid(p, q,labels= c('A', 'B'), label_size = 12,label_colour = ("blue"))  
2library(cowplot) #奶牛画图包
3library(ggthemes) #ggplot中的主题包,囊括了很多经典图表形式,比如经济学人、538、华尔街等等
4
5p <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + theme_economist() #利用了经济学人的主题
6q <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + facet_grid(drv ~ .) + theme_economist()
7
8plot_grid(p, q,labels= c('A', 'B'), label_size = 12,label_colour = ("blue"))  


640?wx_fmt=png


组合的形式,同时也支持"纵向"排列

 
 
1plot_grid(p, q,labels= c('A', 'B'), label_size = 12,label_colour = ("blue"),  align = "v",ncol = 1)'A''B'), label_size = 12,label_colour = ("blue"),  align = "v",ncol = 1)


640?wx_fmt=png


同时也支持"3个图"的组合形式

 
 
 1library(ggplot2) 2library(cowplot) 3library(ggthemes) 4 5p <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() +  6      theme_economist(base_family = "myfonts") 7q <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() +  8      facet_grid(drv ~ .) + theme_economist(base_family = "myfonts") 9r <-  ggplot(mpg, aes(x = cty) )+geom_bar(fill = "skyblue")+ theme_economist()1011bottom_row <- plot_grid(p, q,labels= c('A', 'B'), label_size = 12,label_colour = ("blue"),  align = "h")12plot_grid(r, bottom_row, labels = c('A', ''), ncol = 1, rel_heights = c(1, 1.2),label_colour = ("blue"))
2library(cowplot)
3library(ggthemes)
4
5p <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + 
6      theme_economist(base_family = "myfonts")
7q <-  ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))+geom_point() + 
8      facet_grid(drv ~ .) + theme_economist(base_family = "myfonts")
9r <-  ggplot(mpg, aes(x = cty) )+geom_bar(fill = "skyblue")+ theme_economist()
10
11bottom_row <- plot_grid(p, q,labels= c('A''B'), label_size = 12,label_colour = ("blue"),  align = "h")
12plot_grid(r, bottom_row, labels = c('A'''), ncol = 1, rel_heights = c(11.2),label_colour = ("blue"))

640?wx_fmt=png


同时也支持"子图"的组合形式

 
 
1library(viridis) 2ggdraw() +3  draw_plot(p + theme(legend.justification = "bottom"), 0, 0, 1, 1) +4  draw_plot(r + scale_color_viridis(discrete = TRUE) + 5              theme(legend.justification = "top"), 0.5, 0.1, 0.5, 0.4) +6  draw_plot_label(c("A", "B"), c(0, 0.55), c(1, 0.5), size = 15)
2ggdraw() +
3  draw_plot(p + theme(legend.justification = "bottom"), 0011) +
4  draw_plot(r + scale_color_viridis(discrete = TRUE) + 
5              theme(legend.justification = "top"), 0.50.10.50.4) +
6  draw_plot_label(c("A""B"), c(00.55), c(10.5), size = 15)


640?wx_fmt=png


cowplot也支持"水印"功能

 
 
1ggdraw(r) + 2  draw_plot_label("A", size = 14) + 3  draw_label("小橙子学数据分析", angle = 45, size = 60, alpha = .2)
2  draw_plot_label("A", size = 14) + 
3  draw_label("小橙子学数据分析", angle = 45, size = 60, alpha = .2)

640?wx_fmt=png


cowplot也支持"图与背景的重叠"功能

 
 
1library(magick)2p <-   ggplot(mpg, aes(x=hwy, group = cyl , fill = factor(cyl) )) + geom_density(alpha = 0.5)3ggdraw() +4  draw_image("https://jeroen.github.io/images/frink.png") +5  draw_plot(p)
2p <-   ggplot(mpg, aes(x=hwy, group = cyl , fill = factor(cyl) )) + geom_density(alpha = 0.5)
3ggdraw() +
4  draw_image("https://jeroen.github.io/images/frink.png") +
5  draw_plot(p)

640?wx_fmt=png


总结

        当需要组合图片的时候,cowplot是个很好的工具。可以实现各种形式的图形组合。

——————————————

往期精彩:

640?wx_fmt=png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值