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,130 points
3,063 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
0 answers

Change the order of legend values in a plot in R

how to change the order of legend ...READ MORE

Jul 23 in Data Analytics by prardhana
33 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,130 points
174 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
497 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
1,575 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
772 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
54 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
3,423 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,360 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 in Data Analytics by anonymous
85 views