Kumpelka
Kumpelka

Reputation: 987

Facet correlation plots between an output variable and multiple input variables with ggplot2

I want to display all the correlations between an output variable and multiple input variables.

I precise that :

I can use a loop or an apply call but I wonder if there is a better and more elegant solution with the facet function of ggplot2.

Here is a simplified example. I want to facet the three possible correlation plots.

library(ggplot2)

# data
output <- c(3, 5, 8, 9, 12, 13)
input_1 <- c(1, 3, 4, 6, 8, 11)
input_2 <- c(3, 8, 2, 5, 11, 1)
input_3 <- c(14, 8, 6, 4, 2, 1)

mydf <- data.frame(output, input_1, input_2, input_3)

# First Correlation plot
ggplot(data = mydf, aes(x = input_3, y = output)) +
  geom_point() +
  geom_smooth(method = "lm")

# Second correlation plot
ggplot(data = mydf, aes(x = input_2, y = output)) +
  geom_point() +
  geom_smooth(method = "lm")

# Third correlation plot
ggplot(data = mydf, aes(x = input_3, y = output)) +
  geom_point() +
  geom_smooth(method = "lm")

Upvotes: 2

Views: 960

Answers (1)

Kumpelka
Kumpelka

Reputation: 987

With the comment above (thanks to @PoGibas), I solve using the following code.

library(ggplot2)
library(tidyr)

# Data
output <- c(3, 5, 8, 9, 12, 13)
input_1 <- c(1, 3, 4, 6, 8, 11)
input_2 <- c(3, 8, 2, 5, 11, 1)
input_3 <- c(14, 8, 6, 4, 2, 1)

mydf <- data.frame(output, input_1, input_2, input_3)

# Change data format
mydf2 <- gather(mydf, key = "key", value = "input", -output)

# Correlation plots between the output and the input variables
ggplot(mydf2, aes(input, output)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~ key)

Upvotes: 1

Related Questions