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")
geom_point
andgeom_smooth
. So what do you want to display? There's no code to calculate correlations in your example. – pogibasmelt
your data:ggplot(reshape2::melt(mydf, "output"), aes(value, output)) + geom_point() + facet_wrap(~ variable)
– pogibas