1
> gender %>% arrange(desc(age))
# A tibble: 18 x 3
   age   sex      pop
   <chr> <chr>  <dbl>
 1 62+   Female    51
 2 62+   Male     167
 3 55-61 Female    98
 4 55-61 Male     283
 5 5-12  Female    93
 6 5-12  Male      87
 7 45-54 Female   160
 8 45-54 Male     346
 9 35-44 Female   257
10 35-44 Male     315
11 25-34 Female   207
12 25-34 Male     285
13 18-24 Female   103
14 18-24 Male      72
15 13-17 Female    37
16 13-17 Male      34
17 0-4   Female    63
18 0-4   Male     105

I cant figure out why "5-12" is not being ordered correctly.

Here is my code

ggplot(data = gender, 
       mapping = aes(x = age, y = ifelse(test = sex == "Male", yes = -pop, no = pop), 
                     fill = sex, )) +
  geom_col(col = "black") +
  coord_flip() +
  scale_y_continuous(labels = abs, limits = max(gender$pop) * c(-1,1)) +
  labs(y = "Population") + 
  labs(title="Age Distribution by Male & Female Genders",
       #subtitle="Point-In-Time Count 2011-2020",
       x="Age Range", 
       y="Individuals Counted",
       fill = "Gender") + 
  scale_fill_brewer(palette = "Set1") 

Currently my x-axis has only 3 labels c(200,0,200).

How can I do c(300,200,100,0,100,200,300)

Thank you

RL_Pug
  • 697
  • 7
  • 30

2 Answers2

0

Because you use coord_flip into your code, your "x axis" is in fact the "y axis". To add more breaks, you can simply pass values into the breaks argument of scale_y_continuous such as:

ggplot(data = df, 
       mapping = aes(x = age, y = ifelse(sex == "Male", yes = -pop, no = pop), 
                     fill = sex)) +
  geom_col(col = "black") +
  coord_flip() +
  scale_y_continuous(labels = abs, limits = max(df$pop) * c(-1,1),
                     breaks = seq(-300,300,by = 100)) +
  labs(y = "Population") + 
  labs(title="Age Distribution by Male & Female Genders",
       #subtitle="Point-In-Time Count 2011-2020",
       x="Age Range", 
       y="Individuals Counted",
       fill = "Gender") + 
  scale_fill_brewer(palette = "Set1")+
  geom_text(aes(x = age, y = ifelse(sex == "Male", yes = -pop, no = pop),label = pop,
                hjust =  ifelse(sex == "Male", yes =0, no = 1)))

enter image description here

Does it answer your question ?


Reproducible example

structure(list(age = c("62+", "62+", "55-61", "55-61", "5-12", 
"5-12", "45-54", "45-54", "35-44", "35-44", "25-34", "25-34", 
"18-24", "18-24", "13-17", "13-17", "0-4", "0-4"), sex = c("Female", 
"Male", "Female", "Male", "Female", "Male", "Female", "Male", 
"Female", "Male", "Female", "Male", "Female", "Male", "Female", 
"Male", "Female", "Male"), pop = c(51L, 167L, 98L, 283L, 93L, 
87L, 160L, 346L, 257L, 315L, 207L, 285L, 103L, 72L, 37L, 34L, 
63L, 105L)), row.names = c(NA, -18L), .internal.selfref = <pointer: 0x55a536b290c0>, class = c("tbl_df", 
"tbl", "data.frame"))
dc37
  • 15,840
  • 4
  • 15
  • 32
0

I like facet_share from the ggpol package. There are various good solutions over here too.

library(ggplot2)
# install.packages("ggpol")
library(ggpol)

df <- read.table(text = 
"   age   sex      pop
1 62+   Female    51
2 62+   Male     167
3 55-61 Female    98
4 55-61 Male     283
5 5-12  Female    93
6 5-12  Male      87
7 45-54 Female   160
8 45-54 Male     346
9 35-44 Female   257
10 35-44 Male     315
11 25-34 Female   207
12 25-34 Male     285
13 18-24 Female   103
14 18-24 Male      72
15 13-17 Female    37
16 13-17 Male      34
17 0-4   Female    63
18 0-4   Male     105",
                 header = TRUE, stringsAsFactors = FALSE)

df$count <- ifelse(df$sex == "Female", df$pop * -1, df$pop)

p <- ggplot(df, aes(x = age, y = count, fill = sex)) + 
  geom_col() +
  facet_share(~sex, dir = "h", scales = 'free_x', reverse_num = TRUE) + 
  coord_flip(clip = 'off') +
  labs(x = '', 
       y = 'Population') +
  scale_fill_brewer(palette = 'Set2') +
  theme_classic(base_size = 16) +
  theme(strip.background = element_blank(),
        strip.text = element_text(face = 'bold')) +
  theme(legend.position = 'none')
p

library(grid)
grid.text("Age", 
          gp = gpar(fontsize = 16, col = "Black"),
          x = unit(0.545, "npc"),
          y = unit(0.965,"npc"))

Created on 2020-04-03 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115