Reputation: 587
I have this dataset
a <- data.frame(PatientID = c("0002" ,"0004", "0005", "0006" ,"0009" ,"0010" ,"0018", "0019" ,"0020" ,"0027", "0039" ,"0041" ,"0042", "0043" ,"0044" ,"0045", "0046", "0047" ,"0048" ,"0049", "0055"),
volume = c( 200 , 100 , 243 , 99 , 275, 675 ,345 , 234 , 333 ,444, 123 , 274 , 442 , 456 ,666 , 567 , 355 , 623 , 105 , 677 ,876),
Status= c("New" , "Old" , "New" , "New" , "Old", "New" ,"Old" , "New" , "Old" , "New" , "New" ,"New" ,"Old" , "New" ,"New" ,"Old" , "New" , "Old" , "New" , "Old" ,"Old"),
sex = c( 1 , 1 , 1 , 1 , 0, 0 ,0 , 0 , 0 ,1 , 1 , 1 , 0 , 0 ,1 , 1 , 1 , 1 , 1 , 1 ,1), stringsAsFactors = F)
and this code
color <- c("#00B7EB","#EE2A7B")
ggplot(a, aes(y = a$volume, x = a$Status, fill = a$Status)) +
geom_boxplot() +
geom_point(alpha=0.4) +
scale_fill_manual(values=color) +
labs(x='', y='Volume') +
theme_classic() +
theme( text = element_text( size = 15))
This, produces the following plot
THE QUESTION:
What can I do to colour the dots in this ggplot based on the following condition?: If volume is >100 in women (sex==1) red, otherwise black If volume is >200 in men (sex==0) red, otherwise black
Thank you so much!
Upvotes: 0
Views: 1188
Reputation: 319
One way to do this is by setting the colour aesthetic of geom_point to your condition:
geom_point(alpha=0.4, aes(colour = (sex == 1 & volume > 100) | (sex == 0 & volume > 200))) +
Then use scale_colour_manual to set the colours to red and black:
scale_colour_manual(values = c("black", "red")) +
Upvotes: 3