0

I have a bargraph that is generated using the following code.

ggplot(data = df, aes(y = y_variable, fill = fact_variable)) + geom_bar(stat='count') + ggtitle("Graph")

This generates the following plot: (Titles and axes have been cropped due to sensitivity of information) enter image description here

I want the plot however to be ordered in descending order of count. How do i do that? Ideally it should look like this but with the fill variable in the previous plot:

enter image description here

NOTE: y_variable and fact_variable are factor variables.

All examples on stackoverflow currently show the reorder() method as the solution, however that is for a bargraph that has a x variable plotted on the x-axis. I have a bargraph that has count in the x-axis.

How do i order it on this basis?

Adhishwar
  • 41
  • 10
  • 1
    I don't think the axis of a histogram should be reordered. Otherwise it would no longer be a histogram (which shows the distribution, or shape, of the data). – neilfws Apr 20 '21 at 05:37
  • @neilfws My bad. The y-variable is a factor variable and hence its a bar graph. But even then all solutions on stackoverflow give me examples when there's another variable on the x-axis. Whereas i dont have another variable plotted on the x-axis. Its just the count. – Adhishwar Apr 20 '21 at 05:50
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 20 '21 at 06:14
  • @RonakShah Done. An example plot has been added in my question. – Adhishwar Apr 20 '21 at 07:53
  • 3
    Does this answer your question? [How to reorder x-axis based on y-axis values in R ggplot2](https://stackoverflow.com/questions/63165943/how-to-reorder-x-axis-based-on-y-axis-values-in-r-ggplot2) – Sinh Nguyen Apr 20 '21 at 11:15

2 Answers2

0

Try this one

ggplot(df, aes(y = reorder(y_variable, -perc), fill  = fact_variable)) + geom_bar(stat='count') + ggtitle("Graph")
Limey
  • 10,234
  • 2
  • 12
  • 32
  • I already did. The following error is returned: ```Error in tapply(X = X, INDEX = x, FUN = FUN, ...) : object 'perc' not found``` – Adhishwar Apr 20 '21 at 07:48
  • you need to have the variable perc in your dataframe df - either create it or assign the right variable – Ray Apr 20 '21 at 12:20
0

@Adishwar it is generally recommended to use geom_col() if you work with totals. This avoids you having to set stat in geom_bar().

Since there is no reproducible example in terms of data, I have grossly simplified your data frame emulating your variable names. (The error you get is because perc is not known in your case, aka "object not found"). I then use geom_col() and reorder for the ordered barchart.

Your example does not specify where you got the fill colours from. You can set them manually dependent on your needs. I use the out-of-the-box colours ...

If you do not want a label to show, set it to NULL (which removes it).

# setting up the test data with 2 categories and counts
df <- tribble(
  ~y_variable, ~fact_variable, ~count
  ,"Government" , "fact1"     , 100
  ,"Government" , "fact2"     , 200
  ,"Government" , "fact3"     , 500
  ,"Nonprofit"  , "fact1"     , 75
  ,"Nonprofit"  , "fact2"     , 200
  ,"Nonprofit"  , "fact3"     , 100
  ,"Other"      , "fact1"     , 300
  ,"Other"      , "fact2"     , 50
  ,"Other"      , "fact3"     , 80
)

# "standard barchart on counts (totals) using geom_col()
ggplot(data = df, aes(x = count, y = y_variable, fill = fact_variable)) + 
  geom_col() +

# this is to set the title, labels, etc ---------------------------
  labs(title = "your title here", fill = "possible fill title"
       ,x = "my counts or NULL", y = NULL) +
  theme_minimal()

For the ordered version, use reorder(variable, ordervariable). Make sure to assign the right variable names.

ggplot(data = df, aes(x = count, y = reorder(y_variable, count), fill = fact_variable)) + 
  geom_col() +
  labs(title = "your title here", fill = "possible fill title"
       ,x = "my counts or NULL", y = NULL) +
  theme_minimal()

enter image description here

Ray
  • 2,008
  • 14
  • 21