Tan
Tan

Reputation: 25

ggplot (geom_bar) not sorting y-axis according to numeric values

I am trying to sort y-axis numerically according to population values. Have tried other stackoverflow answers that suggested reorder/ converting columns to numeric data type (as.numeric), but those solutions does not seem to work for me.

Without using reorder, the plot is sorted alphabetically: enter image description here

Using reorder, the plot is sorted as such: enter image description here

The code i am using:

library(ggplot2)
library(ggpubr)
library(readr)
library(tidyverse)
library(lemon)
library(dplyr)

pop_data <- read_csv("respopagesextod2011to2020.csv")
temp2 <- pop_data %>% filter(`Time` == '2019')
ggplot(data=temp2,aes(x=reorder(PA, Pop),y=Pop)) +  geom_bar(stat='identity') + coord_flip()

How should I go about sorting my y-axis? Any help will be much appreciated. Thanks!

I am using data filtered from: https://www.singstat.gov.sg/-/media/files/find_data/population/statistical_tables/singapore-residents-by-planning-areasubzone-age-group-sex-and-type-of-dwelling-june-20112020.zip

Upvotes: 1

Views: 1256

Answers (1)

chemdork123
chemdork123

Reputation: 13883

The functions are all working as intended - the reason you don't see the result as expected is because the reorder() function is specifying the ordering of the pop_data$PA based on each observation in the set, whereas the bars you are plotting are a result of summary statistics on pop_data.

The easiest solution is to probably perform the summarizing first, then plot and reorder the summarized dataset. This way, the reordering reflects an ordering of the summarized data, which is what you want.

temp3 <- pop_data %>% filter(`Time` == '2019') %>%
  group_by(PA) %>%
  summarize(Pop = sum(Pop))

ggplot(data=temp3, aes(x=reorder(PA, Pop),y=Pop)) +
  geom_bar(stat='identity') + coord_flip()

enter image description here

Upvotes: 3

Related Questions