Re-l
Re-l

Reputation: 301

Limit the color variation in R using scale_color_grey

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")

Upvotes: 1

Views: 637

Answers (1)

Richard Erickson
Richard Erickson

Reputation: 2616

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.

Upvotes: 1

Related Questions