4

I have re-created this plot from the an answered question. But I wanted to know how one can either overlay or replace the bars with a smooth plot, ie linking the points on the X-axis together. If they it is replacing the bars, then it would be nice to fill it in with the colour. I have tried playing around with geom_smooth but I cannot get it to do what I want and help would be most appreciated. The plot and code from the question I am discussing are below.

set.seed(1)
df0 <- data.frame(Age = factor(rep(x = 1:10, times = 2)), 
                  Gender = rep(x = c("Female", "Male"), each = 10),
                  Population = sample(x = 1:100, size = 20))

head(df0)
#   Age Gender Population
# 1   1 Female         27
# 2   2 Female         37
# 3   3 Female         57
# 4   4 Female         89
# 5   5 Female         20
# 6   6 Female         86

library("ggplot2")
ggplot(data = df0, aes(x = Age, y = Population, fill = Gender)) +
  geom_bar(data = subset(df0, Gender=="Female"),
           stat = "identity") +
  geom_bar(data = subset(df0, Gender=="Male"),
           stat = "identity",
           position = "identity",
           mapping = aes(y = -Population)) +
  scale_y_continuous(labels = abs) +
  coord_flip()

Simple pyramid plot

Community
  • 1
  • 1
James Lloyd
  • 127
  • 1
  • 4

1 Answers1

2

Without knowing what problem you faced with geom_smooth, I can only guess at why it didn't work. Perhaps it's related to the fact that you cast Age as a factor in your data frame?

I converted Age to numeric & the following worked for me:

df1 <- df0 %>% mutate(Age = as.numeric(as.character(Age)))
ggplot(df1,
       aes(x = Age, y = Population, fill = Gender, colour = Gender)) +
  geom_bar(data = subset(df1, Gender=="Female"), alpha = 0.5,
           stat = "identity") +
  geom_bar(data = subset(df1, Gender=="Male"), alpha = 0.5,
           stat = "identity",
           position = "identity",
           mapping = aes(y = -Population)) +
  geom_smooth(data = subset(df1, Gender=="Female"), se = F) +
  geom_smooth(data = subset(df1, Gender=="Male"), se = F,
              mapping = aes(y=-Population)) +
  scale_y_continuous(labels = abs) +
  coord_flip()

Resulting chart (smoothed lines overlaid on bars)

Z.Lin
  • 28,055
  • 6
  • 54
  • 94