Home > OS >  How can I change the colour argument from producing gradient colors to distinct colors?
How can I change the colour argument from producing gradient colors to distinct colors?

Time:03-20

Replicating part of my data:

tibble(x=c(3.98, 3.98, 3.98, 3.98, 3.90, 3.90, 3.90, 3.90, 3.85, 3.85, 3.85, 3.85), y = c(-70.1, -63.65, -63.65, -63.65, -61.40, -59.12, -59.12, -59.12, -65.01, -58.90, -58.90, -58.90), type = c("obs", "true", "true", "true", "obs", "true", "true", "true", "obs", "true", "true", "true"), s = c(1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3))

Using the above data, I want to produce a plot that looks like this: Target plot

The data I have provided is only part of the data, so the output may be different.

I have successfully got the way I want it to be using the code below:

d <- read_csv("data/data.csv")

d %>% 
filter(type == 'true') %>% 
ggplot(aes(x,y))   
geom_line()   
geom_point(data = d %>% filter(type == 'obs'), aes(colour = s))  
facet_wrap(~s)

The problem is that I am getting a plot like this: Result

How can I get the 3 groups to have 3 distinct colors (as shown in the Target plot) instead of having a gradient of blue (as shown in Result)?

CodePudding user response:

Change the s value to factor in geom_point.

library(dplyr)
library(ggplot2)

d %>% 
  filter(type == 'true') %>% 
  ggplot(aes(x,y))   
  geom_line()   
  geom_point(data = d %>% filter(type == 'obs'), aes(colour = factor(s)))  
  facet_wrap(~s)
  • Related