How to create faceted correlation plot using GGPLOT

前端 未结 3 1991
失恋的感觉
失恋的感觉 2021-02-10 06:07

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(le         


        
相关标签:
3条回答
  • 2021-02-10 06:35

    Since your data is not in the appropriate format, some reshaping is necessary before it can be plotted.

    Firstly, reshape the data to the long format:

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

    Split the values along type and val1 vs. val2:

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

    Create a list of all combinations:

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

    Create a new dataset:

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

    Plot:

    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

    0 讨论(0)
  • 2021-02-10 06:40

    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)
    

    enter image description here

    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)
    

    enter image description here

    0 讨论(0)
  • 2021-02-10 06:42

    There is an additional package ggpubr available now addressing exactly this issue with the stat_cor() function.

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

    0 讨论(0)
提交回复
热议问题