This is a test version of Biostars. For the public version, visit https://www.biostars.org.
order of legend in ggscatter

I am stuck with what I thought was rather simple: all I want is changing the order of the 2 items in my legend in a scatterplot. Maybe my brain is in Sunday mode??? I've got 2 items in "Group" and R puts them in alphabetical order into the legend. I want them the other way round but can't work it out. Anyone got an idea?

ggscatter(df, x = "T1", y = "T0",
          color="Group",palette = c("red", "blue"), 
          add = "reg.line", conf.int = TRUE, 
          add.params = list(color = "brown"), 
          cor.coef = TRUE, cor.method = "spearman",
          cor.coef.coord = c(2.5,5),  
          cor.coef.size = 5, 
          ylim = c(0, 5),
          xlab = "T1", ylab = "T0")+
 scale_x_continuous(n.breaks=7) +
  theme(legend.position = "right", legend.justification = "top" )+
  theme(axis.line=element_line(size=1))+
  theme(text = element_text(size = 13))
ggscatter r

1 answer

You can reorder the levels of your Group variable as follows-

df$Group <- factor(df$Group, levels = c("item1", "item2")) # if you want item1 first
df$Group <- factor(df$Group, levels = c("item2", "item1")) # if you want item2 first

Log in to answer this question.