0

I am trying to plot the two dataframes I have, yet do not get the bars.

Here it is an example of the dataframes I have:

    ae_attendances_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male", "Not Specified"
), class = "factor"), AgeBand = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("0 yrs", 
"1-4 yrs", "10-14 yrs", "15-19 yrs", "20-24 yrs", "25-29 yrs", 
"30-34 yrs", "35-39 yrs", "40-44 yrs", "45-49 yrs", "5-9 yrs", 
"50-54 yrs", "55-59 yrs", "60-64 yrs", "65-69 yrs", "70-74 yrs", 
"75-79 yrs", "80-84 yrs", "85+ yrs"), class = "factor"), Attend = c(4708L, 
7065L, 1914L, 2292L, 4612L, 4968L, 5620L, 4007L, 2802L, 2429L, 
2652L, 1908L, 1474L, 1414L, 1462L, 1388L, 1468L, 1586L, 4149L, 
6720L, 10781L, 2911L, 1492L, 2666L, 3565L, 3163L, 2880L, 3007L, 
4000L, 5167L), Attendances_Gender = c("Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Female_not_admitted", "Female_not_admitted", "Female_not_admitted", 
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted", 
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted", 
"Male_not_admitted", "Male_not_admitted", "Male_not_admitted", 
"Male_not_admitted", "Male_not_admitted")), row.names = c(NA, 
-30L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Gender", drop = TRUE, indices = list(
    0:18, 19:29), group_sizes = c(19L, 11L), biggest_group_size = 19L, labels = structure(list(
    Gender = structure(1:2, .Label = c("Female", "Male", "Not Specified"
    ), class = "factor")), row.names = c(NA, -2L), class = "data.frame", vars = "Gender", drop = TRUE))

The second data frame:

ae_admissions_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male", "Not Specified"
    ), class = "factor"), AgeBand = structure(c(1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("0 yrs", 
    "1-4 yrs", "10-14 yrs", "15-19 yrs", "20-24 yrs", "25-29 yrs", 
    "30-34 yrs", "35-39 yrs", "40-44 yrs", "45-49 yrs", "5-9 yrs", 
    "50-54 yrs", "55-59 yrs", "60-64 yrs", "65-69 yrs", "70-74 yrs", 
    "75-79 yrs", "80-84 yrs", "85+ yrs"), class = "factor"), Admitted = c(4352L, 
    5229L, 1145L, 1328L, 2963L, 3769L, 3475L, 2342L, 1544L, 1141L, 
    1856L, 1022L, 1072L, 1005L, 1062L, 1000L, 801L, 736L, 1030L, 
    4727L, 8072L, 1947L, 1002L, 1615L, 2231L, 1883L, 1653L, 1602L, 
    1572L, 3054L), Admissions_Gender = c("Female_admitted", "Female_admitted", 
    "Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted", 
    "Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted", 
    "Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted", 
    "Female_admitted", "Female_admitted", "Female_admitted", "Female_admitted", 
    "Female_admitted", "Male_admitted", "Male_admitted", "Male_admitted", 
    "Male_admitted", "Male_admitted", "Male_admitted", "Male_admitted", 
    "Male_admitted", "Male_admitted", "Male_admitted", "Male_admitted"
    )), row.names = c(NA, -30L), class = c("grouped_df", "tbl_df", 
    "tbl", "data.frame"), vars = "Gender", drop = TRUE, indices = list(
        0:18, 19:29), group_sizes = c(19L, 11L), biggest_group_size = 19L, labels = structure(list(
        Gender = structure(1:2, .Label = c("Female", "Male", "Not Specified"
        ), class = "factor")), row.names = c(NA, -2L), class = "data.frame", vars = "Gender", drop = TRUE))

This is my code:

ggplot() +
  geom_bar(data = ae_attendances_df, aes(x = AgeBand, fill = Attendances_Gender, y = Attend), 
           stat = 'identity', position = 'dodge') +
  geom_bar(data = ae_admissions_df, aes(x = AgeBand, fill = Admissions_Gender, y = Admitted), 
           stat = 'identity', position = position_dodge(0.9), width = 0.6) + 
  xlab("Age Group") +
  ylab("ED Attendances and Admissions") + 
  ggtitle("Unscheduled ED Attendance and Admission") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  scale_y_continuous(breaks = scales::pretty_breaks(15)) +
  scale_fill_manual("", values = c("Female Admitted" = "coral3", 
                                   "Female Not Admitted" = "lightcoral", 
                                   "Male Admitted" = "steelblue4", 
                                   "Male Not Admitted" = "lightblue2"))

What I get isn't what I want and not able to get what is needed:

enter image description here

But this is what I am trying to get and obviously something does not work:

enter image description here

3
  • Your data only has females. Are males supposed to be also in there? Commented Aug 29, 2018 at 17:01
  • 1
    Seems that everything works until your scale_fill_manual. Look again at your values. They are not correct. You use "Female Admitted", but I see "Female_admitted", etc. If you use the values that actually exist in your dataframe you'll solve the problem. :) Commented Aug 29, 2018 at 17:01
  • @ AntoniosK and avid_useR - I've edited my code. Sorry about it Commented Aug 29, 2018 at 17:14

1 Answer 1

1

I think the problem lies in your use of scale_fill_manual() try this:

ggplot() +
   geom_bar(data = ae_attendances_df, aes(x = AgeBand, fill = Attendances_Gender, y = Attend),
            stat = 'identity', position = 'dodge') +
   geom_bar(data = ae_admissions_df, aes(x = AgeBand, fill = Admissions_Gender, y = Admitted),
            stat = 'identity', position = position_dodge(0.9), width = 0.6) +
   xlab("Age Group") +
   ylab("ED Attendances and Admissions") +
   ggtitle("Unscheduled ED Attendance and Admission") +
   theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
   scale_y_continuous(breaks = scales::pretty_breaks(15)) +
   scale_fill_manual(values = c("coral3", "lightcoral"))

enter image description here

Namely, you don't need to provide names to the scale_fill, it will detect that automatically based on the order of your groups.

Sign up to request clarification or add additional context in comments.

5 Comments

I've edited my code as obviosly, my reproduced dataframes did not capture the other category - Male
Did this work for you? I can update the photo but I would rather not if it already worked. The gist is that you would just add the colours as you want them to values = c(...) and let scale_fill_manual(...) do the rest of the work.
it did. Yet the legend shall capture the category "Female not admitted" instead of "Female_not_admitted". Additionally, the age group on x axis does not put my 5-9 barplots.
Sorry, I kept editing my answer above. And thank you a lot bk18!
bk18 thank you a lot. I have solved the issue by renaming the categories. :) Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.