Jitter lines in ggplot - code works without "position =" argument

I have tried many ways to jitter my lines (vertically) in ggplot and have had no success. Different error messages for different attempts, but here's one of them:

CODE:

ggplot(cont_surv, 
       aes(
          x=Day,
          y=percent_surv,
          group = treatment,
          xlab = "Days since drought treatment start",
          ylab = "Percent surviving"
        )
 )+
   geom_line(aes(linetype=treatment,
                 color = treatment,
                 position=position_dodge(width=0.2), #removing this line => code works
   )) +
   
   scale_linetype_manual(values= c(5,5,1,1,4,4,3,3)
 )+
   scale_color_manual(values=c(
     "chartreuse3", 
     "brown", 
     "chartreuse3",
     "brown",
     "chartreuse3",
     "brown",
     "chartreuse3",
     "brown"
   ))+
   scale_size_manual(values=c(1,1.5))

### ERROR MESSAGE:

#Warning: Ignoring unknown aesthetics: position
#Don't know how to automatically pick scale for object of type PositionDodge/Position/ggproto/gg. #Defaulting to continuous.
#Error: Aesthetics must be valid data columns. Problematic aesthetic(s): position = position_dodge(width #= 0.2). 
#Did you mistype the name of a data column or forget to add stat()?

Hi @erinaiello, if you could post your data along with your code, that would help folks help you sort out what might be going on. The simplest way to do this would be to apply the dput() function to your table cont_surv, and paste the output from the console here, between triple backticks (```), like this:

```
<-- paste output of dput(cont_surv)
```
> dput(cont_surv)
structure(list(Day = c(1L, 13L, 14L, 15L, 17L, 20L, 24L, 27L, 
29L, 31L, 34L, 38L, 41L, 45L, 48L, 69L, 73L, 77L, 80L, 83L, 1L, 
13L, 14L, 15L, 17L, 20L, 24L, 27L, 29L, 31L, 34L, 38L, 41L, 45L, 
48L, 69L, 73L, 77L, 80L, 83L, 1L, 13L, 14L, 15L, 17L, 20L, 24L, 
27L, 29L, 31L, 34L, 38L, 41L, 45L, 48L, 69L, 73L, 77L, 80L, 83L, 
1L, 13L, 14L, 15L, 17L, 20L, 24L, 27L, 29L, 31L, 34L, 38L, 41L, 
45L, 48L, 69L, 73L, 77L, 80L, 83L, 1L, 13L, 14L, 15L, 17L, 20L, 
24L, 27L, 29L, 31L, 34L, 38L, 41L, 45L, 48L, 69L, 73L, 77L, 80L, 
83L, 1L, 13L, 14L, 15L, 17L, 20L, 24L, 27L, 29L, 31L, 34L, 38L, 
41L, 45L, 48L, 69L, 73L, 77L, 80L, 83L, 1L, 13L, 14L, 15L, 17L, 
20L, 24L, 27L, 29L, 31L, 34L, 38L, 41L, 45L, 48L, 69L, 73L, 77L, 
80L, 83L, 1L, 13L, 14L, 15L, 17L, 20L, 24L, 27L, 29L, 31L, 34L, 
38L, 41L, 45L, 48L, 69L, 73L, 77L, 80L, 83L), percent_surv = c(100L, 
97L, 95L, 94L, 91L, 86L, 81L, 71L, 62L, 60L, 48L, 47L, 36L, 36L, 
34L, 21L, 10L, 9L, 5L, 5L, 100L, 100L, 100L, 99L, 97L, 94L, 92L, 
84L, 76L, 76L, 70L, 67L, 63L, 58L, 58L, 47L, 29L, 22L, 12L, 12L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 99L, 99L, 99L, 98L, 98L, 98L, 98L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 99L, 99L, 99L, 99L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 98L, 
94L, 92L, 62L, 62L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 98L, 88L, 85L, 84L, 84L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 99L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 100L, 100L, 100L), treatment = structure(c(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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("Broom_drought_live", 
"Broom_drought_sterile", "Broom_wet_live", "Broom_wet_sterile", 
"uninvaded_drought_live", "uninvaded_drought_sterile", "uninvaded_wet_live", 
"uninvaded_wet_sterile"), class = "factor")), class = "data.frame", row.names = c(NA, 
-160L))

Thanks dromano! Did so.

Perfect! Could you also edit your original post so that the code is also enclosed in triple backticks? (With comment in R style, with #.)

Just take the position argument out of the aes() function

library(ggplot2)

ggplot(cont_surv, 
       aes(
           x=Day,
           y=percent_surv,
           group = treatment,
           xlab = "Days since drought treatment start",
           ylab = "Percent surviving"
       )
) +
    geom_line(aes(linetype=treatment,
                  color = treatment),
              position = position_jitter(w=2, h=2)) +
    
    scale_linetype_manual(values= c(5,5,1,1,4,4,3,3)
    )+
    scale_color_manual(values=c(
        "chartreuse3", 
        "brown", 
        "chartreuse3",
        "brown",
        "chartreuse3",
        "brown",
        "chartreuse3",
        "brown"
    ))+
    scale_size_manual(values=c(1,1.5))

You got a funky looking graph. Maybe I didn't post my code correctly?

This is what it looks like when I run it without the position argument:

It was the quoted variable

Thank you, but when I try that, I get this error message, and no graph:

Warning: Ignoring unknown aesthetics: position
Don't know how to automatically pick scale for object of type PositionJitter/Position/ggproto/gg. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): position = position_jitter(w = 2, h = 2).
Did you mistype the name of a data column or forget to add stat()?

...... is there another library I should load, perhaps?

Also, god that looks ugly (my fault, not yours). I've seen in other examples where the lines kept their shape, they just moved up a couple of pixels or so, to stack on top of one another. Perhaps position_jitter is not what I want to use...

It seems like you still have position inside the aes() function, you have to take it out, position is not an aesthetic.

2 Likes

Thank you for your continued help. That worked!

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.