Change the order of legend values in a plot in R

0 votes
how to change the order of legend values in a plot in R?
Jul 23, 2019 in Data Analytics by prardhana
1,934 views

1 answer to this question.

0 votes

@prardhana,

Use scale_fill/color/size_discrete/contin....(labels = c()). compare both to see the difference.

Example -

ggplot(mpg, aes(displ, hwy, fill = factor(cyl))) + geom_bar(stat = "identity") + scale_fill_discrete(labels = 
c("a","b","c","d")) + theme(legend.background = element_rect(colour = "red"),plot.background = element_rect(
  color = "blue"),panel.background = element_rect(color = "green"),legend.text = element_text(color = "hotpink")
  ,legend.title = element_text(color = "pink"))

ggplot(mpg, aes(displ, hwy, fill = factor(cyl))) + geom_bar(stat = "identity") + scale_fill_discrete(labels = 
c("a","c","b","d")) + theme(legend.background = element_rect(colour = "red"),plot.background = element_rect(
  color = "blue"),panel.background = element_rect(color = "green"),legend.text = element_text(color = "hotpink")
  ,legend.title = element_text(color = "pink"))
answered Nov 4, 2019 by payal

Related Questions In Data Analytics

+1 vote
3 answers

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

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,664 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,876 views
+1 vote
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
4,934 views
0 votes
1 answer
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,720 points

edited Jun 9, 2020 by MD 8,755 views
0 votes
1 answer

Aligning title in ggplot2

You can use the theme layer to ...READ MORE

answered Apr 27, 2018 in Data Analytics by Bharani
• 4,660 points

edited Jun 9, 2020 by MD 3,651 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,660 points
9,663 views
0 votes
1 answer

Showing percentage instead of counts in a bar plot - ggplot2 - R

Load the 'scales' package first, which gives ...READ MORE

answered May 29, 2018 in Data Analytics by Bharani
• 4,660 points
3,793 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
4,465 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, 2019 in Data Analytics by sindhu
2,911 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP