如何使用GGPLOT创建分面相关图 [英] How to create faceted correlation plot using GGPLOT
本文介绍了如何使用GGPLOT创建分面相关图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(ggplot2)
x< p ; - data.frame(字母[1:10],abs(rnorm(10)),abs(rnorm(10)),type =x)
y< - data.frame 10],abs(rnorm(10)),abs(rnorm(10)),type =y)
#实际上,每个x和y的行数可能大于10
all< - rbind(x,y)
colnames(all)< -C(name,val1,val2,type)
我想要做的是创建一个看起来大致像这样的分面ggplot:
因此,上面的每个方面都是以下关系图:
#左上方
子集(全部,类型== x)$ val1
子集(全部,类型==y)$ val1
#右上角
子集(全部,类型==x) $ val1
subset(all,type ==y)$ val2
#... etc ..
但是我被卡住了以下代码:
p < - ggplot(all,aes(val1,val2))+ geom_smooth(method =lm )+ geom_point()+
facet_grid(type〜)
#计算每个组的相关性
cors < - ddply(all,c(type〜),summarize,cor = round cor(val1,val2),2))
p + geom_text(data = cors,aes(label = paste(r =,cor,sep =)),x = 0.5,y = 0.5)
什么是正确的方法?
解决方案
您的一些代码不正确。这适用于我:
p < - ggplot(all,aes(val1,val2))+ geom_smooth(method = lm)+ geom_point()+
facet_grid(〜type)
#计算每个组的相关性
cors < - ddply(all,。(type),summarize,cor = round cor(val1,val2),2))
p + geom_text(data = cors,aes(label = paste(r =,cor,sep =)),x = 1,y = -0.25)
编辑:关注OP的评论和修改。这个想法是重新创建所有四个组合的数据,然后构面。
#我认为您以前的数据中的类型为xx和yy
dat< - data.frame(val1 = c(rep(all $ val1 [all $ type ==x],2),
rep(all $ val1 [所有$ type ==y],2)),
val2 = rep(all $ val2,2),
grp1 = rep(c(x,x,y ,y),each = 10),
grp2 = rep(c(x,y,x,y),each = 10))
p <-ggplot(dat,aes(val1,val2))+ geom_point()+ geom_smooth(method =lm)+
facet_grid(grp1〜grp2)
cors< - ddply (标签=粘贴(r =,cor,sep =),。(grp1,grp2),summary,cor = round(cor(val1,val2),2))
p + geom_text )),x = 1,y = -0.25)
I have a data frame created the following way.
library(ggplot2)
x <- data.frame(letters[1:10],abs(rnorm(10)),abs(rnorm(10)),type="x")
y <- data.frame(letters[1:10],abs(rnorm(10)),abs(rnorm(10)),type="y")
# in reality the number of row could be larger than 10 for each x and y
all <- rbind(x,y)
colnames(all) <- c("name","val1","val2","type")
What I want to do is to create a faceted ggplot that looks roughly like this:
Hence each facet above is the correlation plot of the following:
# Top left facet
subset(all,type=="x")$val1
subset(all,type=="y")$val1
# Top right facet
subset(all,type=="x")$val1
subset(all,type=="y")$val2
# ...etc..
But I'm stuck with the following code:
p <- ggplot(all, aes(val1, val2))+ geom_smooth(method = "lm") + geom_point() +
facet_grid(type ~ )
# Calculate correlation for each group
cors <- ddply(all, c(type ~ ), summarise, cor = round(cor(val1, val2), 2))
p + geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=0.5, y=0.5)
What's the right way to do it?
解决方案
Some of your code was incorrect. This works for me:
p <- ggplot(all, aes(val1, val2))+ geom_smooth(method = "lm") + geom_point() +
facet_grid(~type)
# Calculate correlation for each group
cors <- ddply(all, .(type), summarise, cor = round(cor(val1, val2), 2))
p + geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=1, y=-0.25)
Edit: Following OP's comment and edit. The idea is to re-create the data with all four combinations and then facet.
# I consider the type in your previous data to be xx and yy
dat <- data.frame(val1 = c(rep(all$val1[all$type == "x"], 2),
rep(all$val1[all$type == "y"], 2)),
val2 = rep(all$val2, 2),
grp1 = rep(c("x", "x", "y", "y"), each=10),
grp2 = rep(c("x", "y", "x", "y"), each=10))
p <- ggplot(dat, aes(val1, val2)) + geom_point() + geom_smooth(method = "lm") +
facet_grid(grp1 ~ grp2)
cors <- ddply(dat, .(grp1, grp2), summarise, cor = round(cor(val1, val2), 2))
p + geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=1, y=-0.25)
这篇关于如何使用GGPLOT创建分面相关图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文