how to change the order of legend values in a plot in R?
Jul 23, 2019 1,852 views

## 1 answer to this question.

@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

+1 vote

## 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

+1 vote

## 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

+1 vote

## 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

## What is the difference between [] and [[]] notations to access the elements of a list or dataframe in R?

R provides 3 basic indexing operators. Refer ...READ MORE

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

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

## Aligning title in ggplot2

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

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

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