-4

I use the code dput(head(Refugees_demographics, 2))

and here is the sample dataset below:

structure(list(Year = c(2006, 2007), `Female 0-4` = c(0, 3), 
    `Female 5-11` = c(0, 0), `Female 12-17` = c(140, 36), `Female 18-59` = c(1118, 
    519), `Female 60+` = c(121, 85), `F: Total` = c(1379, 643
    ), `Male 0-4` = c(0, 7), `Male 5-11` = c(6, 1), `Male 12-17` = c(152, 
    56), `Male 18-59` = c(1323, 870), `Male 60+` = c(595, 88), 
    `M: Total` = c(2076, 1022)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

I would like to get the following stacked bar chart. (like the photo)

enter image description here

The x-axis would be the year, and the y-axis would be the number of number of refugees. Each year would have two stacked bars. One for male and one for female. Within each bar, it's stacked by their age, i.e. female0-4, female 5-11...etc.

I tried to use the following code but it didn't work and I also have no idea how to group female and male by year.

ggplot() +
  geom_bar(aes(y = Refugees_demographics$`Female 0-4`,
               x = Refugees_demographics$Year, fill = product),
           data = Refugees_demographics, stat="identity")

If someone could help this, I will be greatly appreciated!

Thank you in advance.

r2evans
  • 141,215
  • 6
  • 77
  • 149
sodasky
  • 9
  • 2
  • 1
    Please do not link to data: links go stale, and when they do this question becomes completely [unreproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It's generally better to (a) include a representative *sample* of your actual data; (b) randomly generate data and provide the code (with `set.seed`) to make it ourselves; or (c) use base-R datasets (e.g., `mtcars`, `iris`, `ggplot2::diamonds`, etc). – r2evans May 14 '18 at 16:12
  • 1
    However, a search of StackOverflow for [`[r] [ggplot2] stack bar`](https://stackoverflow.com/search?q=%5Br%5D+%5Bggplot2%5D+stack+bar) should provide what you need. – r2evans May 14 '18 at 16:15
  • Possible duplicate of https://stackoverflow.com/q/6644997/3358272 (holding out on closing this in case I'm wrong) – r2evans May 14 '18 at 16:16
  • @r2evans, sorry about that. It's my first time to ask the question here. I changed the dataset to a sample dataset! – sodasky May 14 '18 at 17:08
  • No worries, sodasky. Another thing that would be helpful is to read about [StackOverflow markdown formatting](https://stackoverflow.com/editing-help), which can make it much easier to differentiate (visually) "conversation" from "`data/code`". In this case, just highlighting the `structure(...)` code and hitting Ctrl-K worked. (Unfortunately, the `kbd` tags don't work on SO ... :-( – r2evans May 14 '18 at 17:10

1 Answers1

0

You will need to transform your data prior to to creating the visual. See the code below. There are a number of things going on here that you will have to work through. For example, I removed the total columns from your raw data, updated the year column, and updated the column names.

require(tidyverse)
require(lubridate)

df$Year <- year(as.Date(as.character(df$Year), "%Y"))

colnames(df) <- sub("\\.", "_", colnames(df))

df2 <- df %>%
  gather("Variable", "Total", 2:13) %>%
  separate(variable, c("Gender", "Age"), "_" ) %>%
  filter(age != ".Total")

df2$Age <- gsub("\\.", "-", df2$Age)

ggplot(df2, aes(Gender, Total, fill = Age)) + geom_col() + facet_grid(~Year)
LMunyan
  • 116
  • 6