kllrdr
kllrdr

Reputation: 177

Color data points in R when a condition is met

I am plotting the following in R:

ggplot(data=df)+
geom_jitter(mapping=aes(x=as.factor(Phenotype), y=Genic_Enrichment))+
theme_classic()+
geom_vline(xintercept = 0, linetype = 2) +
geom_hline(yintercept = 0, linetype = 2) +
theme(axis.text.x=element_text(angle = 90, vjust = 0.5, hjust=1)) + 
scale_x_discrete(guide = guide_axis(check.overlap = TRUE))

It works perfectly.

However, I would like to add another line which is changing the color of the data points if a certain condition is met. The condition would be another column in the same data frame (all data points have this value, it is a p value). I tried:

col = ifelse(df$col4 < 0.05,'red', 'black')
a = df$col4
col = ifelse(a < 0.05,'red', 'black')

I know that I somehow have to add that I want to do this with the values on the y axis, but I cannot figure out how.

data frame:

Phenotype Genic_enrichment pvalue
Phenotype1 1.2345 0.001
Phenotype2 1.3456 0.123
Phenotype3 1.1113 0.049
Phenotype4 1.6789 0.257

So, if the p value corresponding to genic_enrichment is lower than 0.05, I would like to color my data point to a different color.

Upvotes: 0

Views: 1008

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388797

Add a column in the dataframe and use it in color :

library(ggplot2)

df$col = ifelse(df$pvalue < 0.05,'red', 'black')

ggplot(data=df)+
  geom_jitter(aes(x=as.factor(Phenotype), y=Genic_enrichment, color = col)) +
  theme_classic()+
  geom_vline(xintercept = 0, linetype = 2) +
  geom_hline(yintercept = 0, linetype = 2) +
  theme(axis.text.x=element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE))  +
  scale_color_identity()

enter image description here

Upvotes: 2

Related Questions