0

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

4
  • What type of plot are you expecting? What have you tried so far? Commented Apr 19, 2017 at 9:30
  • Assuming that you are asking for dotplot based on your title, what do you want to facet_wrap the plot by? Commented Apr 19, 2017 at 9:31
  • do you mean scatterplot? A dotplot doesnt make much sense for your example. Commented Apr 19, 2017 at 9:34
  • yes a scatterplot sorry ! I would like to display my 3 plots inside one using facet feature Commented Apr 19, 2017 at 9:37

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

And maybe facet_wrap(~ key, scales = "free") in case that Ys are on different scales.
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.