1

Before I start, allow me to explain my graph: I have two Genotypes (WTB and whd) and each have two conditions (0 and 7), so I have four lines.

Now, I want to make a plot where each variable and its condition is the same color. Anything with whd will be black and anything with WTB will be grey.

I managed to make the graph with the solid/dashed lines correctly, however, I am having trouble with the coloring. I can only make it so that there is a grey gradient. Are there any parameters for scale_color_grey to help me with this?

This is my code (I took out parts of it for easier read):

ggplot(data=df4, aes(x=Day, y=Remain, group=Condition, shape=Condition, colour=Condition) + 
theme_bw() +
geom_line(aes(linetype=Condition), size=1) +
geom_point(size=0, fill="#FFFFFF") +
scale_linetype_manual(name="Genotype",
values=c("solid", "dashed", "solid", "dashed")) +
scale_shape_manual(name="Genotype", values=c(1,19,1,19)) +
scale_color_grey(name="Genotype")

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Re-l
  • 291
  • 1
  • 3
  • 13

1 Answers1

1

I think this code should produce the plot you want. However, without your exact dataset, I had to generate simulated data.

## Generate dummy data and load library
library(ggplot2)
df4 = data.frame(Remain = rep(0:1, times = 4),
      Day = rep(1:4, each = 2),
      Genotype = rep(c("wtb", "whd"), each = 4),
      Condition = rep(c("0", "7"), times = 4 ))

Next, I created a grouping variable:

df4$both = paste0(df4$Genotype, df4$Condition)

And last, I plotted and saved the figure:

example <-  ggplot(data=df4, aes(x=Day, y=Remain, 
                group=both, 
                color = Genotype,
                linetype = Condition)) + 
                geom_line() +
                scale_color_manual(values = c("grey50", "black")) +
                scale_linetype_manual(values = c("solid", "dashed")) +
                theme_bw()

ggsave("example.jpg", example)

enter image description here

From your code, it was not apparent why you included the geom_point. Also, if you only want one sub-legend, rather than two, this SO question provides details on how to do that.

Community
  • 1
  • 1
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
  • 1
    Wow, that's exactly what I was trying to do. Thank you so much! I didn't know about grouping variables, it works so nicely. Thank you! – Re-l Jun 23 '15 at 03:08
  • 1
    You're welcome. `ggplot` can be trick to work with, but can do lot. As a heads up, mixing the `shape`, `color`, `alpha`, `fill`, etc. parameters with `facets` gives you lots of options for plotting, but can be quirky at times as well. – Richard Erickson Jun 23 '15 at 03:20
  • Thanks for the tip! I'll look into those. – Re-l Jun 23 '15 at 03:40