I have made a loop to create a list of scatter plots of difference (y axis) vs sample (x axis). For these plots, outliers are defined as samples where difference between two counts for the same sample was larger than 10%. I have coloured outliers in red and would like the non-outliers in green but for some reason, they come out as green and grey (specifically I think the points where difference = 0 are grey). I would love to know if there is an error somewhere in my code that I am missing?
Below is my code to create the list of scatter plots
myplots <- list()
for (x in c(1:length(stage2))) {
message(stage2[x])
myplots[[stage2[x]]] <- local({
x <- x
perc_diff <- (abs(df[, paste0("D_", stage2[x])])/
(df[, paste0("M_", stage2[x])] +
df[ , paste0("H_", stage2[x])])/2)*100
sct <- ggplot(df, aes(x=sample, y= df[, paste0("D_", stage2[x])])) +
geom_point(df, size= 2.2,
mapping = aes(colour = ifelse(perc_diff >= 10 , "outlier",
"non-outlier"))) +
labs(y = "", x= "") +
theme(axis.text.x = element_blank(),
axis.ticks.x=element_blank(),
legend.position = "none") +
scale_color_manual(values=c("palegreen3", "tomato3"))
print(sct)
})
}
I then title them and use grid arrange to arrange them:
for (i in c(1:length(D_stage))){
myplots[[i]] <- myplots[[i]] + ggtitle((stage2[i])) +
theme(plot.title = element_text(size = 25))
}
gridExtra::grid.arrange(grobs = myplots, ncol = 2,
left = "count difference", bottom = "sample")
This is what I get - I need the grey points to be green as well: