4

My data looks something like this:

# A tibble: 6 x 4
    CFU         strain   diltn order
  <dbl>         <fctr>   <dbl> <dbl>
1   0.0 M12-611025 (0) 5.89279     1
2   1.0 M12-611025 (0) 5.19382     1
3   0.0 M12-611025 (0) 4.49485     1
4   0.5 M12-611025 (0) 3.79588     1
5   1.0 M12-611025 (0) 3.09691     1
6   7.0 M12-611025 (0) 2.39794     1

I have 16 different "strains" and only want to plot a subset onto my graph as well as using geom_smooth to draw a line. I have achieves this as follows:

lines1 <- c("M12-611025 (0)" = "solid",
            "M12-611025 (0) HI" = "dashed",
            "M12-611025 (300)" = "solid",
            "M12-611025 (300) HI" = "dashed",
            "M12-611025 (700)" = "solid",
            "M12-611025 (700) HI" = "dashed",
            "M12-611025 (1100)" = "solid",
            "M12-611025 (1100) HI" = "dashed")

ggplot(data = (subset(data,
  strain %in% c("M12-611025 (0)", "M12-611025 (0) HI",
                "M12-611025 (300)", "M12-611025 (300) HI",
                "M12-611025 (700)", "M12-611025 (700) HI",
                "M12-611025 (1100)", "M12-611025 (1100) HI"))),
  (aes(x = diltn, y = CFU, colour = factor(strain),
       fill = factor(strain), linetype = factor(strain)))) +
  geom_smooth(se = F, span = 1) +
  geom_point(shape = 21, colour = "black", size = 2, stroke = 1) +
  scale_y_continuous(limits = c(-1, 120))+
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4),
                     labels = c(0, 10, 100, 1000, 10000),
                     limits = c(1, 4))+
  annotation_logticks(base = 10, sides = "b", scaled = TRUE) +
  theme(axis.line = element_line(colour = "black",
                                 size = 1,    
                                 linetype = "solid")) +
  ggtitle("RPM Test M12 - 611025") + 
  xlab(expression(paste("Dilution "))) + 
  ylab("CFU") +
  scale_linetype_manual(values = lines1)

The key here is I want to plot the strians with "HI" as dashed lines and the others as solid lines, this gives me:

enter image description here

I then want to define my own colour schemes so the same strains have the same colour points and lines but the lines are dashed for the "HI" versions. I tried this:

cols1 <- c("M12-611025 (0)" = "blue",
           "M12-611025 (0) HI" = "blue",
           "M12-611025 (300)" = "#ff9700",
           "M12-611025 (300) HI" = "#ff9700",
           "M12-611025 (700)" = "#33d100",
           "M12-611025 (700) HI" = "#33d100", 
           "M12-611025 (1100)" = "#fe0000",
           "M12-611025 (1100) HI" = "#fe0000")

M12_611025 + scale_fill_manual(values = cols1)

This changes the points but not the lines, and I can not figure out how to get the lines the same colour as the points?

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
reubenmcg
  • 371
  • 4
  • 18
  • I answered your question but I'd like to point out that you didn't provide a [minimal, reproducible example.](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Please do so in the future. – Claus Wilke Dec 19 '17 at 03:33
  • Thank you, I will use the post you cited next time, I have posted a few times now and have tried to imporove each post in terms of clarity and sticking to the point. I see now I need to include a few more things, thank you for the pointer – reubenmcg Dec 19 '17 at 19:12
  • Yes. In particular, if I cannot easily run your code with my fix, then I can't verify that my answer actually works. I'm only guessing at that point. – Claus Wilke Dec 19 '17 at 19:56

1 Answers1

4

Lines use the color aesthetic and points of shape 21 use the fill aesthetic (for the inside color, the outline is also a color aesthetic). Therefore, in your case, you need to change both the fill and the color scale:

M12_611025 + 
  scale_fill_manual(values = cols1) +
  scale_color_manual(values = cols1)
Claus Wilke
  • 16,992
  • 7
  • 53
  • 104