Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading