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,100 points
1,867 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,550 points

Related Questions In Data Analytics

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,100 points
106 views
0 votes
1 answer
0 votes
1 answer

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

Yes, we can find the index of ...READ MORE

answered Apr 13, 2018 in Data Analytics by zombie
• 3,690 points
158 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,680 points
93 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,170 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,550 points
594 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,550 points
37 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,550 points
2,027 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,550 points
1,038 views
+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
47 views