0
votes

I have the following data.frame of continious values. I would like to plot all combinaison between X et Y with ggplot:

  library(ggplot2)
  df = data.frame(X=1:10, Y1=1:10, Y2=1:10, Y3=1:10)

For this example, I want 3 plots : (X vs Y1) (X vs Y2) (X vs Y3).

2
What type of plot are you expecting? What have you tried so far? - Adam Quek
Assuming that you are asking for dotplot based on your title, what do you want to facet_wrap the plot by? - Adam Quek
do you mean scatterplot? A dotplot doesnt make much sense for your example. - mtoto
yes a scatterplot sorry ! I would like to display my 3 plots inside one using facet feature - DrIDK

2 Answers

3
votes

You need to reshape your data first to "long" format, so you can facet_wrap() by key. Using tidyr:

library(tidyr)
library(ggplot2)

df %>% gather(key, value, -X) %>%
  ggplot(.,aes(x = X, y = value)) + 
  geom_point() +
  facet_wrap(~key)

enter image description here

2
votes

I also succeed by using melt :

 df = data.frame(X=1:10, Y1=1:10, Y2=1:10, Y3=1:10)
 df.m = melt(df, id.vars = X)
 ggplot(df.m, aes(x=X, y=value)) + geom_line() + facet_wrap(~variable)