How to order bars in a bar graph using ggplot2?

0 votes

I am trying to make a bar graph where the largest bar would be nearest to the y axis and the shortest bar would be furthest. 

So I have considered a table like this

    Name   Position
1   James  Goalkeeper
2   Frank  Goalkeeper
3   Jean   Defense
4   Steve  Defense
5   John   Defense
6   Tim    Striker

Here, I am trying to build a bar graph that would show the number of players according to the position

p <- ggplot(theTable, aes(x = Position)) + geom_bar(binwidth = 1)

But, the graph shows the goalkeeper bar first then the defense, and finally the striker one.

 I want the graph to be ordered so that the defense bar is closest to the y-axis, the goalkeeper one, and finally the striker one. 

May 31, 2018 in Data Analytics by darklord
• 6,170 points

1 answer to this question.

0 votes

The key to ordering is to set the levels of the factor in the order you want. 

An ordered factor is not required; the extra information in an ordered factor isn't necessary and if these data are being used in any statistical model, the wrong parametrization might result — polynomial contrasts aren't right for nominal data such as this.

## set the levels in order we want
theTable <- within(theTable, 
                   Position <- factor(Position, 
## plot

We simply need to set the factor levels to be in the desired order. There are multiple ways of doing this depending on the situation. For instance, we could do:

levels(theTable$Position) <- c(...)

and simply list the levels in the desired order on the right-hand side. You can also specify the level order within the call factor as above:

theTable$Position <- factor(theTable$Position, levels = c(...))
answered Jun 1, 2018 by DataKing99
• 8,130 points

Related Questions In Data Analytics

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,550 points
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,170 points
0 votes
1 answer

How to change fill color in each facet using ggplot2?

You can map the facetting variable to ...READ MORE

answered May 8, 2018 in Data Analytics by kappa3010
• 2,020 points
0 votes
1 answer

How to create a box-plot using “plotly” in R?

You can use this command to create ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
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
0 votes
11 answers

Changing the legend title in ggplot

Hi, you can also try guides() to ...READ MORE

answered Jul 30 in Data Analytics by Cherukuri
• 28,040 points
0 votes
0 answers
0 votes
0 answers
0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 16, 2018 in Data Analytics by DataKing99
• 8,130 points
0 votes
1 answer

How to forecast season and trend of data using STL and ARIMA in R?

You can use the forecast.stl function for the ...READ MORE

answered May 18, 2018 in Data Analytics by DataKing99
• 8,130 points