5

我有一个按以下方式创建的数据框。

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")

我想要做的是创建一个看起来大致像这样的多面 ggplot:

在此处输入图像描述

因此,上面的每个方面都是以下的相关图:

# 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..

但我坚持使用以下代码:

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)

正确的方法是什么?

4

3 回答 3

8

您的某些代码不正确。这对我有用:

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)

在此处输入图像描述

编辑:按照 OP 的评论和编辑。这个想法是用所有四种组合重新创建数据,然后再分面。

# 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)

在此处输入图像描述

于 2013-03-18T05:53:51.463 回答
3

由于您的数据格式不正确,因此在绘制之前需要进行一些整形。

首先,将数据重塑为长格式:

library(reshape2)
allM <- melt(all[-1], id.vars = "type")

沿typeval1vs.拆分值val2

allList <- split(allM$value, interaction(allM$type, allM$variable))

创建所有组合的列表:

allComb <- unlist(lapply(c(1, 3), 
                  function(x)
                    lapply(c(2 ,4), 
                           function(y) 
                             do.call(cbind, allList[c(x, y)]))), 
           recursive = FALSE)

创建一个新数据集:

allNew <- do.call(rbind, 
                  lapply(allComb, function(x) {
                                    tmp <- as.data.frame(x)
                                    tmp <- (within(tmp, {xval <- names(tmp)[1]; 
                                                         yval <- names(tmp)[2]}))
                                    names(tmp)[1:2] <- c("x", "y")
                                    tmp}))

阴谋:

library(ggplot2)
p <- ggplot(allNew, aes(x = x, y = y)) + 
       geom_smooth(method = "lm")  + 
       geom_point() +
       facet_grid(yval ~ xval) 
# Calculate correlation for each group
library(plyr)
cors <- ddply(allNew, .(yval, xval), summarise, cor = round(cor(x, y), 2))
p + geom_text(data=cors, aes(label=paste("r=", cor, sep="")), x=0.5, y=0.5)

在此处输入图像描述

于 2013-03-18T07:33:15.573 回答
1

现在有一个额外的软件包ggpubr可以解决这个stat_cor()函数的问题。

library(tidyverse)
library(ggpubr)
ggplot(all, aes(val1, val2))+ 
  geom_smooth(method = "lm")  + 
  geom_point() +  
  facet_grid(~type) +
  stat_cor()

在此处输入图像描述

于 2019-07-05T09:09:01.207 回答