0

I'd like to create a series of ggplot charts with different values for x.

This is what I've tried

df %>% 
  ggplot(aes(x = factor_var, y = response_var)) +
    geom_point() +
    stat_smooth() +
    facet_wrap(~factor_var)

Unfortunately this places the factor_var as the x axis value while what I really want is this

df %>% 
  filter(factor_var == 1) %>%
  ggplot(aes(x = independent_var y = response_var)) +
    geom_point() +
    stat_smooth()

And then that same chart for factor_var == 2 and then 3 and so on. I'd like them to show up in the same plot.

Cauder
  • 2,157
  • 4
  • 30
  • 69
  • 1
    Please show a small reproducible example. In the first try, can't you use `x = independent_var` – akrun Jul 14 '20 at 22:17
  • 1
    To echo what akrun suggests, it will be much easier to help if you provide at least a sample of your data with `dput(df)` or if your data is very large `dput(df[1:20,])`. You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jul 14 '20 at 22:18
  • What about adding `facet_wrap()` to last code? – Duck Jul 14 '20 at 22:20

2 Answers2

1

Difficult to tell without a reprex, but it sounds like your data is something like this:

library(ggplot2)
library(dplyr)

set.seed(69)

df <- data.frame(factor_var = factor(rep(1:6, 100)),
           independent_var = rnorm(600),
           response_var = rnorm(600))

And your first plot is wrong because it looks like this:

df %>% 
  ggplot(aes(x = factor_var, y = response_var)) +
  geom_point() +
  stat_smooth() +
  facet_wrap(~factor_var)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

You would be happier with something like this:

df %>% 
  filter(factor_var == 1) %>%
  ggplot(aes(x = independent_var, y = response_var)) +
  geom_point() +
  stat_smooth()
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

But you want a separate version for each of the factor variables, and you want them all on the same page. In that case, you put the independent variable on the x axis, the dependent variable on the y axis, and facet by the factor variable:

df %>% 
  ggplot(aes(x = independent_var, y = response_var)) +
  geom_point() +
  stat_smooth() +
  facet_wrap(~factor_var)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2020-07-14 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

We can replace the x with the 'independent_var' (using a reproducible example, it can be 'gear' from mtcars, and the factor_var as 'cyl')

library(dplyr)
library(ggplot2)
mtcars %>%
    ggplot(aes(x = gear, y = mpg, color = cyl)) + 
        geom_point() + 
        stat_smooth() + 
        facet_wrap(~ cyl)

Or as we commented earlier, just use the x as independent_var

df %>% 
   ggplot(aes(x = independent_var, y = response_var)) +
     geom_point() +
     stat_smooth() +
     facet_wrap(~factor_var)
akrun
  • 874,273
  • 37
  • 540
  • 662