0

I'm trying to add two trend lines to the data plotted in my faceted plot for two different depths (Road=color). Geom_smooth works to generate the first plot(pred_new2) with trend line, but as soon as I add facet_wrap - the plot is generated, but without any trend lines/smoothing and without any error.

pred_new$Site <- factor(pred_new$Site, 
                        levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))

pred_new2 <- ggplot(pred_new, aes(x = No_cars, y = Site, color = Road)) + 
  geom_point() + 
  geom_smooth(aes(x = No_cars, y = Site, color = Road), method = "lm")

pred_new3 <- pred_new2 + 
  geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror))

pred_new4 <- pred_new3 + 
  facet_wrap(~ Days, scales = "free_x") + 
  ylab("Site") + 
  xlab("No_cars") + 
  theme_classic()
pred_new4

Any help would be greatly appreciated.

pred_new = structure(list(Site = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                             8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 
                                             6L, 7L, 8L, 9L, 1L, 2L, 3L,  4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 
                                             4L, 5L, 6L, 7L, 8L, 9L), 
                                           .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I"), 
                                           class = "factor"), 
                          Days = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                             3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
                                             2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L), 
                                           .Label = c("Thursday", "Tuesday", "Wednesday"), 
                                           class = "factor"),
                          Road = structure(c(1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                             2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                             2L), 
                                           .Label = c("east", "west"), 
                                           class = "factor"), 
                          No_cars = c(15.266427, 8.323348, 8.368608, 9.747807, 7.976356, 8.5684, 6.604537, 
                                      3.812109, 6.719904, 4.799487, 4.996091, 4.796, 4.991479, 4.525789, 
                                      5.115136, 4.939559, 4.783792, 4.185007, 3.857553, 3.095228, 2.890727, 
                                      3.132784, 3.352974, 3.42561, 2.900284, 2.35416, 2.889976, 17.266427, 
                                      10.323348, 10.368608, 11.747807, 9.976356, 10.5684, 8.604537, 5.812109, 
                                      8.719904, 6.799487, 6.996091, 6.796, 6.991479, 6.525789, 7.115136, 
                                      6.939559, 6.783792, 6.185007, 5.857553, 5.095228, 4.890727, 5.132784, 
                                      5.352974, 5.42561, 4.900284, 4.35416, 4.889976), 
                          standerror = c(1.7108483, 0.8175014, 0.6365042, 0.7171749, 0.9978123, 0.9881427, 
                                         0.9215597, 0.6365042, 1.6303975, 0.404129, 0.1934362, 0.1503158, 
                                         0.1694848, 0.2362161, 0.2337497, 0.2180687, 0.1604379, 0.3902528, 
                                         0.3276444, 0.1568268, 0.1218673, 0.1374084, 0.1915103, 0.1895107, 
                                         0.1767974, 0.1300738, 0.3163943, 1.7108483, 0.8175014, 0.6365042, 
                                         0.7171749, 0.9978123, 0.9881427, 0.9215597, 0.6365042, 1.6303975, 
                                         0.404129, 0.1934362, 0.1503158, 0.1694848, 0.2362161, 0.2337497,
                                         0.2180687, 0.1604379, 0.3902528, 0.3276444, 0.1568268, 0.1218673, 
                                         0.1374084, 0.1915103, 0.1895107, 0.1767974, 0.1300738, 0.3163943)), 
                     row.names = c(NA, -54L), class = "data.frame")
Adela
  • 1,757
  • 19
  • 37
Elle Bowd
  • 5
  • 7
  • Welcome to SO Elle Blank. I highly recommend you format your code as code. It makes it easier for people to read and therefore easier for them to help you. I've done it for you in this question. – James Jones Sep 07 '18 at 01:36
  • 1
    Thanks @JamesJones first time asking a q! – Elle Bowd Sep 07 '18 at 01:57
  • Try posting your data, or a minimal toy dataset sufficient for reproducing the issue. One possibility is that adding facets mean there aren't enough points per facet to draw the fitted line, but hard to say without seeing the data. – arvi1000 Sep 07 '18 at 02:08
  • @arvi1000 Ive added the data in please see above – Elle Bowd Sep 07 '18 at 02:39
  • theres 9 points for each facet – Elle Bowd Sep 07 '18 at 02:47
  • @ElleBlank: please use `dput(pred_new)` to share data. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Sep 07 '18 at 03:39
  • For each level of `Days` you have only one observation in each category of `Road` (`east` or `west`) and since `Site` is factor, there cannot be any trend. – Adela Sep 07 '18 at 08:56
  • What is your expected output? – Adela Sep 07 '18 at 09:01
  • Thanks Adela. I wanted to plot the trend of no_cars across the different sites.. is there a way to get around this? – Elle Bowd Sep 07 '18 at 09:35

1 Answers1

1

I guess that you swap the axes. To include trend you need numeric values for Site, so I include as.numeric(Site) in ggplot.

library(ggplot2)

ggplot(pred_new, aes(x = as.numeric(Site), y = No_cars, color = Road)) + 
  geom_point() + 
  geom_smooth(method = "lm") +
  geom_errorbar(aes(ymin = No_cars - standerror, ymax = No_cars + standerror)) +
  xlab("Site") + 
  # this will recode your x-axis
  scale_x_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
  facet_wrap(~ Days, scales = "free_x") + 
  theme_classic() +
  # you can swap x and y axes with coord_flip()
  coord_flip()

Is this desired output?

enter image description here

In case you really want Site on the y-axis, you can simply flip x and y axes:

ggplot(pred_new, aes(y = as.numeric(Site), x = No_cars, color = Road)) + 
  geom_point() + 
  geom_smooth(method = "lm") +
  geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror)) +
  ylab("Site") + 
  # this will recode your y-axis
  scale_y_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
  facet_wrap(~ Days, scales = "free_x") + 
  theme_classic()

enter image description here

Adela
  • 1,757
  • 19
  • 37
  • Thanks so much Adela! This is pretty much exactly what I was after - however I was hoping to have `Site` on the y axis - as the `site`s are shared across the different `day`, whereas the scale of the `no_cars` is different depending what `day`. If I made site a numerical variable - is it possible to change the y axis to read the different sites, rather than numbers. Thanks, Elle – Elle Bowd Sep 07 '18 at 23:57
  • I added axes flip into the answer. Hope this helps. – Adela Sep 08 '18 at 06:57