order of legend in ggscatter
1
0
Entering edit mode
15 months ago

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 • 920 views
ADD COMMENT
2
Entering edit mode
15 months ago
bk11 ★ 3.0k

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
ADD COMMENT
0
Entering edit mode

Yes, that you so much. Did the trick.

ADD REPLY

Login before adding your answer.

Traffic: 2515 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6