I have a plot with overlapping points and would like to change which group will be on top.
Please check this example, I manage to invert a and b in terms of colour and order in the legend, with rev()
but "b" is still on top of "a". Any solution?
Thanks in advance
D.
library(ggplot2)
mytable <- data.frame(x = rep(1:10, 2),
y = c(1:10, (1.1:10.1)),
var = factor(c(rep("a",10), rep("b",10)))
)
ggplot(mytable) +
geom_point(aes(x=x,y=y, colour = var), cex=2)
mytable$var_rev <- with(mytable, factor(var, levels=rev(levels(var))))
ggplot(mytable) +
geom_point(aes(x=x,y=y, colour = var_rev), cex=2)
Try this:
library(tidyverse)
mytable %>%
arrange(desc(var)) %>%
ggplot() +
geom_point(aes(x=x,y=y, colour = var), cex=2)
Just:
ggplot(mytable) +
geom_point(aes(x=x,y=y, colour = var), cex=2)
The points are plotted in the same order as in the data frame