R绘图笔记 | 二维散点图与统计直方图组合
前面介绍了散点图、柱状图、直方图和核密度估计图,有时候散点图不能很直观的看的出数据的分布情况,这里介绍散点图与统计直方图组合绘制。
一.方法1
利用ggpubr包的ggscatterhist()函数进行绘制。
ggscatterhist(data, x, y, group = NULL, color = "black", fill = NA, palette = NULL, shape = 19, size = 2, linetype = "solid", bins = 30, margin.plot = c("density", "histogram", "boxplot"), margin.params = list(), margin.ggtheme = theme_void(), margin.space = FALSE, main.plot.size = 2, margin.plot.size = 1, title = NULL, xlab = NULL, ylab = NULL, legend = "top", ggtheme = theme_pubr(), ...)部分参数解释:
data是用于绘图的数据,x和y分别指定数据中的x轴和y轴,group指定一个分组变量,shape指定点的形状【参考:散点图】。
library(ggpubr)
N<-300x1 <- rnorm(mean=1.5, N)y1 <- rnorm(mean=1.6, N)x2 <- rnorm(mean=2.5, N)y2 <- rnorm(mean=2.2, N)
data1 <- data.frame(x=c(x1,x2),y=c(y1,y2))head(data1)> head(data1) x y1 1.9237124 0.10884822 3.1930833 1.84346233 3.4372797 1.93962514 -0.1662552 1.93206015 1.4886753 0.78044156 1.7652103 0.4776553
margin.plot = "histogram"指定边缘的图是直方图,margin.params用来指定该图形的参数。看下面代码,比较一下就知道各参数什么意思。
ggscatterhist( data1, x ='x', y = 'y', shape=21,fill="#7FFFD4",color = "black",size = 3, alpha = 1, #palette = c("#00AFBB", "#E7B800", "#FC4E07"), margin.params = list( fill="red",color = "blue", size = 0.3,alpha=1), margin.plot = "histogram", legend = c(0.8,0.8), ggtheme = theme_minimal())

如果是散点图结合核密度估计图,将margin.plot 设置为 "density",多组数据,fill= "class",参数palette指定填充颜色,看一个案例。
N<-200x1 <- rnorm(mean=1.5, sd=0.5,N)y1 <- rnorm(mean=2,sd=0.2, N)x2 <- rnorm(mean=2.5,sd=0.5, N)y2 <- rnorm(mean=2.5,sd=0.5, N)x3 <- rnorm(mean=1, sd=0.3,N)y3 <- rnorm(mean=1.5,sd=0.2, N)data2 <- data.frame(x=c(x1,x2,x3),y=c(y1,y2,y3),class=rep(c("A","B","C"),each=200))> head(data2) x y class1 1.9221129 2.139207 A2 2.1656947 1.778408 A3 1.6277478 2.221711 A4 1.1816189 2.006987 A5 1.6467425 1.833635 A6 0.4997666 2.033704 Aggscatterhist( data2, x ='x', y = 'y', #iris shape=21,color ="black",fill= "class", size =3, alpha = 0.8, palette = c("#00AFBB", "#E7B800", "#FC4E07"), margin.plot = "density", margin.params = list(fill = "class", color = "black", size = 0.2), legend = c(0.9,0.15), ggtheme = theme_minimal())
二.方法2
利用ggExtra包的ggMarginal()函数
ggMarginal(p, data, x, y, type = c("density", "histogram", "boxplot", "violin", "densigram"), margins = c("both", "x", "y"), size = 5, ..., xparams = list(), yparams = list(), groupColour = FALSE, groupFill = FALSE)p:添加边缘地块的ggplot2散点图。如果p不提供,则必须提供所有数据,x和y。
data:用于创建边缘地块的数据。框架。如果p被提供并且边缘图反映相同的数据是可选的。
type:要显示什么类型的边缘图。其中之一是[密度,直方图,箱线图,小提琴,密度图(density, histogram, boxplot, violin, densigram)](“密度图”是指密度图覆盖在直方图上)。
scatter <- ggplot(data=data1,aes(x=x,y=y)) + geom_point(shape=21,fill="#00AFBB",color="black",size=3)+ theme_minimal()+ theme( #text=element_text(size=15,face="plain",color="black"), axis.title=element_text(size=15,face="plain",color="black"), axis.text = element_text(size=13,face="plain",color="black"), legend.text= element_text(size=13,face="plain",color="black"), legend.title=element_text(size=12,face="plain",color="black"), legend.background=element_blank() #legend.position = c(0.12,0.88) )
ggMarginal(scatter,type="histogram",color="black",fill="#00AFBB")
scatter <- ggplot(data=data2,aes(x=x,y=y,colour=class,fill=class)) + geom_point(aes(fill=class),shape=21,size=3)+#,colour="black")+ scale_fill_manual(values= c("#00AFBB", "#E7B800", "#FC4E07"))+ scale_colour_manual(values=c("#00AFBB", "#E7B800", "#FC4E07"))+ theme_minimal()+ theme( #text=element_text(size=15,face="plain",color="black"), axis.title=element_text(size=15,face="plain",color="black"), axis.text = element_text(size=13,face="plain",color="black"), legend.text= element_text(size=13,face="plain",color="black"), legend.title=element_text(size=12,face="plain",color="black"), legend.background=element_blank(), legend.position = c(0.9,0.15) )ggMarginal(scatter,type="density",color="black",groupColour = FALSE,groupFill = TRUE)
三.方法3
利用grid.arrange()函数。
library(gridExtra)#(a) 二维散点与统计直方图
# 绘制主图散点图,并将图例去除,这里point层和path层使用了不同的数据集scatter <- ggplot() + geom_point(data=data1,aes(x=x,y=y),shape=21,color="black",size=3)+ theme_minimal()# 绘制上边的直方图,并将各种标注去除hist_top <- ggplot()+ geom_histogram(aes(data1$x),colour='black',fill='#00AFBB',binwidth = 0.3)+ theme_minimal()+ theme(panel.background=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())# 同样绘制右边的直方图hist_right <- ggplot()+ geom_histogram(aes(data1$y),colour='black',fill='#00AFBB',binwidth = 0.3)+ theme_minimal()+ theme(panel.background=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), #axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())+ coord_flip()
empty <- ggplot() + theme(panel.background=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())# 要由四个图形组合而成,可以用空白图作为右上角的图形也可以,但为了好玩加上了R的logo,这是一种在ggplot中增加jpeg位图的方法# logo <- read.jpeg("d:\\Rlogo.jpg")# empty <- ggplot(data.frame(x=1:10,y=1:10),aes(x,y))+# annotation_raster(logo,-Inf, Inf, -Inf, Inf)+# opts(axis.title.x=theme_blank(), # axis.title.y=theme_blank(),# axis.text.x=theme_blank(),# axis.text.y=theme_blank(),# axis.ticks=theme_blank())# 最终的组合grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4,1), heights=c(1,4))
# 绘制主图散点图,并将图例去除,这里point层和path层使用了不同的数据集scatter <- ggplot() + geom_point(data=data2,aes(x=x,y=y,fill=class),shape=21,color="black",size=3)+ scale_fill_manual(values= c("#00AFBB", "#E7B800", "#FC4E07"))+ theme_minimal()+ theme(legend.position=c(0.9,0.2))# 绘制上边的直方图,并将各种标注去除hist_top <- ggplot()+ geom_density(data=data2,aes(x,fill=class),colour='black',alpha=0.7)+ scale_fill_manual(values= c("#00AFBB", "#E7B800", "#FC4E07"))+ theme_void()+ theme(legend.position="none")# 同样绘制右边的直方图hist_right <- ggplot()+ geom_density(data=data2,aes(y,fill=class),colour='black',alpha=0.7)+ scale_fill_manual(values= c("#00AFBB", "#E7B800", "#FC4E07"))+ theme_void()+ coord_flip()+ theme(legend.position="none")
empty <- ggplot() + theme(panel.background=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())# 要由四个图形组合而成,可以用空白图作为右上角的图形也可以,但为了好玩加上了R的logo,这是一种在ggplot中增加jpeg位图的方法# logo <- read.jpeg("d:\\Rlogo.jpg")# empty <- ggplot(data.frame(x=1:10,y=1:10),aes(x,y))+# annotation_raster(logo,-Inf, Inf, -Inf, Inf)+# opts(axis.title.x=theme_blank(), # axis.title.y=theme_blank(),# axis.text.x=theme_blank(),# axis.text.y=theme_blank(),# axis.ticks=theme_blank())# 最终的组合grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4,1), heights=c(1,4))

参考资料:
1.R语言数据可视化之美,张杰/著
2.grid.arrange()函数帮助文档
3.ggMarginal()函数帮助文档
4.ggscatterhist()函数帮助文档
赞 (0)
