前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言可视化——图表嵌套(母子图)

R语言可视化——图表嵌套(母子图)

作者头像
数据小磨坊
发布2018-04-11 14:04:25
1.4K0
发布2018-04-11 14:04:25
举报
文章被收录于专栏:数据小魔方数据小魔方

之前在学习ggplot的时候,一直存在着一个困惑。

就是这个函数是否允许两个做出来的两个相关图表重叠嵌套(也就是在一个大图(主图)的边缘位置,放置另一个缩小版的小图)。

这个想法很奇葩,本来想着没啥希望,鉴于该包的开发者那犀利的审美观,估计也不能允许这种情况的发生。

不过最近浏览一位大神的博客,真的有这种情况的解决措施,喜出望外,赶紧在这里分享给大家。

不过他的处理方式不是通过ggplot的内置函数,而是通过grid包中的viewport函数来实现的:

以下是具体的实现步骤:

加载包:

library(ggplot2) #用于画图,主图和附图都使用ggplot的内置数据集

library(grid) #用于设定附图的长宽及叠放在主图的精确位置

加载并预览数据集:

这里我们还是使用关于钻石的那个数据集(之前的图表案例很多都是使用该数据集)

data(diamonds)

head(diamonds)

#制作复合图的主图:

chart1<-ggplot(diamonds,aes(carat,price,colour=cut))+geom_point()+theme(legend.position=c(0.9,0.72),legend.background=element_rect(I(0)))

以上函数可以制作出以carat和price为主要对应关系的散点图,同时分类变量cut通过颜色映射进行区分。最后调整了图例位置和图表背景。

#设定附图长宽及其最终落在主图上的精确位置:

vie<-viewport(width=0.669,height=0.4,x=0.7,y=0.306)

#制作附图

chart2 <-ggplot(diamonds,aes(depth,fill=cut,alpha=.2))+geom_density()+xlim(54,70) +

theme(axis.text.y=element_text(face="bold",colour="black"),

axis.title.y=element_blank(),

axis.text.x=element_text(face="bold",colour="black"),

plot.background=element_rect(I(0),linetype=0),

panel.background=element_rect(I(0)),

panel.grid.major=element_line(colour=NA),

panel.grid.minor=element_line(colour=NA),

legend.background=element_rect(I(0),linetype=1),

legend.position=c(0.85,0.72))

chart2 #预览附图

因为附图要放置在主图边缘并且缩放很大比例,为了防止其背景和网格线系统遮挡主图的重要信息,对其主题元素进行了大量的简化。

将主图与附图合成一并显示:

print(chart2,vp=vie)

将以上代码打包组合:

chart1<-ggplot(diamonds,aes(carat,price,colour=cut))+geom_point()+theme(legend.position=c(0.9,0.72),legend.background=element_rect(I(0)))

chart1

vie<-viewport(width=0.669,height=0.4,x=0.7,y=0.306)

chart2 <-ggplot(diamonds,aes(depth,fill=cut,alpha=.2))+geom_density()+xlim(54,70) +

theme(axis.text.y=element_text(face="bold",colour="black"),

axis.title.y=element_blank(),

axis.text.x=element_text(face="bold",colour="black"),

plot.background=element_rect(I(0),linetype=0),

panel.background=element_rect(I(0)),

panel.grid.major=element_line(colour=NA),

panel.grid.minor=element_line(colour=NA),

legend.background=element_rect(I(0),linetype=1),

legend.position=c(0.85,0.72))

print(chart2,vp=vie)

其实仔细看这种做法,里面也不外乎图层叠加,先做出主图,然后通过viewport函数将附图缩小并叠加到主图上,不过这种方式用来展示一些需要多角度透视的数据分布问题还是很合适的,而且因为是依赖于不同的包,所有主图与附图之间没有严格的类型限制,你所需要做的只是调整好两个图表的位置与大小,别让彼此相互遮挡掩盖重要信息就OK了。

下面我将附图的类型更换为堆积直方图大家看下效果:

chart1<-ggplot(diamonds,aes(carat,price,colour=cut))+geom_point()+theme(legend.position=c(0.9,0.72),legend.background=element_rect(I(0)))

chart1

vie<-viewport(width=0.669,height=0.4,x=0.7,y=0.306)

chart2 <-ggplot(diamonds,aes(depth,fill=color))+geom_histogram()+xlim(54,70) +

theme(axis.text.y=element_text(face="bold",colour="black"),

axis.title.y=element_blank(),

axis.text.x=element_text(face="bold",colour="black"),

plot.background=element_rect(I(0),linetype=0),

panel.background=element_rect(I(0)),

panel.grid.major=element_line(colour=NA),

panel.grid.minor=element_line(colour=NA),

legend.background=element_rect(I(0),linetype=1),

legend.position=c(0.85,0.72))

print(chart2,vp=vie)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-10-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据小魔方 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档