2013-03-18 34 views
5

我有以下方式創建的數據框。如何使用GGPLOT創建分面關聯圖

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看起來大致是這樣的:

enter image description here

因此上述每個方面是下面的相關情節:

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

什麼是正確的做法?

+0

是什麼類型都與你所希望的描繪形象呢?有ggpairs函數的ggAlly包可能有用。就目前而言,我正在努力查看您的示例數據與所需圖表之間的關係。 – mnel 2013-03-18 06:17:22

+2

這是特別令人困惑的是,你參考mpg和wt,這不是你的數據 – alexwhan 2013-03-18 06:31:53

+0

抱歉。我糾正了它。感謝您指出。 – neversaint 2013-03-18 06:36:11

回答

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) 

enter image description here

編輯:繼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) 

enter image description here

+0

不完全。它應該創建2x2網格。請參閱組合圖中的* blue *字體。 – neversaint 2013-03-18 05:56:02

+3

那麼你想在對角線上做什麼?它說'不需要繪製' - 但它被繪製在你的圖表中? – alexwhan 2013-03-18 05:58:24

+0

我在圖表中只是爲了在每個網格中顯示哪些值組合用於相關。 – neversaint 2013-03-18 05:59:57

3

因爲你的數據不是以適當的格式,一些整形是必要的,可以繪製之前。

首先,數據重塑以長格式:

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

斯普利特一起type值和val1val2

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) 

enter image description here