43Tesseracts
43Tesseracts

Reputation: 4907

How to rearrange legend order in a ggplot

I have a ggplot with two legends, how can I rearrange the order of the legends of this plot, so that Sample Size appears below Party?

The docs show how to do this for a qplot, but I can't make the leap from that to the code I have.

Also didn't find here.

Upvotes: 3

Views: 3116

Answers (1)

Thomas K
Thomas K

Reputation: 3311

Just add guides(size = guide_legend(order = 1)) to your plot. It works by specifying the position size should take.

Further information you will find when you read the documentation for guides and guide_legend.

Slightly adapted example from ?guide_legend:

df <- data.frame(x = 1:20, y = 1:20, color = letters[1:5], size = LETTERS[1:2])
p <- ggplot(df, aes(x, y)) +
  geom_point(aes(colour = color, size = size))
p

p + guides(size = guide_legend(order = 1))

Upvotes: 6

Related Questions