Change the order of multiple legends in ggplot

0 votes

HI, how to change the order of multiple legends in ggplot?

Im using below code to build my ggplot.

ggplot(mpg, aes(displ, hwy, col = class)) + geom_point(size = 2,aes(shape = factor(cyl))) + theme(legend.box.background = element_rect(color = "red",fill = "grey"),legend.box.margin = margin(0.4,0.4,0.4,0.4,"cm")) + theme(legend.position = "bottom",legend.box = "vertical")
Jul 23 in Data Analytics by radha
27 views

1 answer to this question.

0 votes

Hi Radha,

You can change the order of legends in any order using the order attribute within the function of the guide.

Suppose my legends are as below - 

ggplot(mpg, aes(displ, hwy, col = cyl)) + geom_point(size = 2,aes(shape = factor(cyl))) + theme(legend.box.margin = margin(0.3,0.3,0.3,0.3,"cm"),legend.background = element_rect(color = "yellow")) + guides(shape = guide_legend(title = "cyl")) + scale_shape_discrete(labels = c("a","b","c","d")) + theme(legend.position = "bottom")

I change the order using code below - 

ggplot(mpg, aes(displ, hwy, col = cyl)) + geom_point(size = 2,aes(shape = factor(cyl))) + theme(legend.box.margin = margin(0.3,0.3,0.3,0.3,"cm"),legend.background = element_rect(color = "yellow")) + guides(shape = guide_legend(title = "cyl")) + scale_shape_discrete(labels = c("a","b","c","d")) + theme(legend.position = "bottom") + guides(shape = guide_legend(order = 2),col = guide_legend(order = 1))

answered Jul 24 by Cherukuri
• 19,510 points

Related Questions In Data Analytics

0 votes
0 answers

Arrange the order of axis elements in ggplot

How to arrange the order of axis ...READ MORE

Jul 17 in Data Analytics by likitha
16 views
0 votes
0 answers

Change the order of legend values in a plot in R

how to change the order of legend ...READ MORE

Jul 23 in Data Analytics by prardhana
20 views
0 votes
1 answer

Changing the order of bars in a bar-plot - ggplot2 - R

You can use the scale_x_discrete() function with ...READ MORE

answered May 28, 2018 in Data Analytics by Bharani
• 4,550 points
2,251 views
0 votes
1 answer

Is it possible to change the column names of the dataset that I'm using in R?

That is definitely possible in R. You ...READ MORE

answered Dec 17, 2018 in Data Analytics by Anisha

edited Dec 17, 2018 38 views
0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,700 points
1,280 views
0 votes
0 answers
0 votes
0 answers
0 votes
0 answers

Create a dual axis column/bar chart using ggplot in R

Hi, i need to create a dual ...READ MORE

Jul 22 in Data Analytics by riya
33 views
0 votes
2 answers

Change the default order of labels of factor in R

You can also use relevel() to change ...READ MORE

answered Jun 27 in Data Analytics by sindhu
28 views
0 votes
2 answers

How to change the value of a variable using R programming in a data frame?

You are getting this error since the ...READ MORE

answered 2 days ago in Data Analytics by anonymous
• 19,510 points
111 views