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

0 votes

I am working with the 'mtcars' dataset and have made this bar-plot with ggplot2:

image

I would want to arrange the bars in ascending order of count. i.e this needs to be the order of the bars: (6,8,3,1,2,4).

Is there a way to modify it?

May 28, 2018 in Data Analytics by DataKing99
• 8,240 points
8,015 views

1 answer to this question.

0 votes

You can use the scale_x_discrete() function with the parameter 'limits' to reorder the bars:

ggplot(data = mtcars,aes(x=factor(carb)))+geom_bar()+scale_x_discrete(limits=c(6,8,3,1,2,4))

The above command will give you the desired result:

image

answered May 28, 2018 by Bharani
• 4,620 points

Related Questions In Data Analytics

0 votes
1 answer

Change the order of legend values in a plot in R

@prardhana, Use scale_fill/color/size_discrete/contin....(labels = c()). compare both to see ...READ MORE

answered Nov 4, 2019 in Data Analytics by payal
730 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,240 points
493 views
0 votes
1 answer
0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 11, 2020 in Data Analytics by Rajiv
• 8,880 points
34,898 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,720 points

edited Jun 9, 2020 by MD 5,748 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,620 points

edited Jun 9, 2020 by MD 2,076 views
0 votes
1 answer

Making a ggplot2 plot without any legends,axes or axis labels

The below command will give you the ...READ MORE

answered May 29, 2018 in Data Analytics by Bharani
• 4,620 points
169 views
0 votes
1 answer

Removing outliers from a box-plot - ggplot2 - R

You just have to add 'outlier.shape=NA' inside ...READ MORE

answered May 31, 2018 in Data Analytics by Bharani
• 4,620 points
17,387 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,620 points
2,620 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
432 views