1
votes

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

I precise that :

  • I don't need to calculate the correlation coefficients explicitly
  • I don't need a complete correlation matrix between all the variables : I'm not interested in displaying the correlation plots between the input variables.

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")
1
You're talking about displaying correlations, but plot geom_point and geom_smooth. So what do you want to display? There's no code to calculate correlations in your example.pogibas
Still, this is very common long/wide data format problem. All you need is to melt your data: ggplot(reshape2::melt(mydf, "output"), aes(value, output)) + geom_point() + facet_wrap(~ variable)pogibas

1 Answers

1
votes

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)