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 in Data Analytics by prardhana
64 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 by payal

Related Questions In Data Analytics

0 votes
2 answers
+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 in Data Analytics by anonymous
106 views
0 votes
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 in Data Analytics by anonymous
• 32,260 points
136 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
1,870 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,560 points
869 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,560 points
3,779 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 28, 2018 in Data Analytics by Bharani
• 4,560 points
1,520 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,690 points
216 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
46 views