ITPub博客

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

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

作者:浪花小雨滴 时间:2020-06-16 15:15:12 0 删除 编辑

之前在学习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)

联系方式:

wechat:ljty1991
Mail:578708965@qq.com
个人公众号:数据小魔方(datamofang)
团队公众号:EasyCharts
qq交流群:[魔方学院]553270834

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3159313/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    40005