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 432 views

## 1 answer to this question.

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,
levels=names(sort(table(Position),
decreasing=TRUE))))
## plot
ggplot(theTable,aes(x=Position))+geom_bar(binwidth=1)```

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
• 8,210 points

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

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

+1 vote

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

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

+1 vote

## How to change fill color in each facet using ggplot2?

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

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

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

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

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

## Changing the legend title in ggplot

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

## Split plot into multiple plots corresponding to each value of the field in R

Use facet_grid() and pass the field to ...READ MORE

## Chart/plot to show 3 dimensions in a chart - R

Hi, I want to show chart with ...READ MORE