1

I have been working on this for some time, and am re-posting this hoping to simplify the definition of the problem and to bring some clarity from feedback of my previous attempt. I am able to label each individual column value, but not able to put the code together necessary to sum the total. The examples I have looked at never work the way I try to put them together, for example with goup_by, or summarize etc.. I would like to only sum the values of "Confirmed Cases", and not show the other column values as with many c("x", "Y", ... "data"), it becomes impossible to read.

Here is the data frame:

dput(COVID1[1:12, ])
structure(list(COUNTY = c("Antrim", "Antrim", "Antrim", "Charlevoix", 
  "Charlevoix", "Grand Traverse", "Grand Traverse", "Grand Traverse", 
  "Antrim", "Grand Traverse", "Grand Traverse", "Grand Traverse"
  ), Date = structure(c(18453, 18456, 18457, 18453, 18455, 18453, 
  18456, 18457, 18455, 18453, 18456, 18457), class = "Date"), CASE_STATUS = c("Confirmed", 
  "Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed", 
  "Confirmed", "Confirmed", "Probable", "Probable", "Probable", 
  "Probable"), Cases = c(1L, 1L, 2L, 1L, 3L, 2L, 2L, 1L, 1L, 1L, 
  1L, 1L)), row.names = c(NA, 12L), class = "data.frame")

Code:

        ggplot(filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0)) +
            geom_col(aes(x = Date, y = Cases, fill = CASE_STATUS), position = position_stack(reverse = TRUE), width = .88)+
            geom_text(aes(x = Date, y = Cases, label = (Cases)), position = position_stack(reverse = TRUE), vjust = 1.5, size = 3, color = "white") +
            scale_fill_manual(values = c('blue',"tomato"))+
            scale_x_date(labels = date_format("%m/%d"), limits = as.Date(c('2020-07-09','today()')), breaks = "1 week")+
    theme(axis.text.x = element_text(angle=0))+
labs(title = "Antrim - Grand Traverse - Charlevoix")

enter image description here

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
John
  • 45
  • 6
  • Hello :) please could you use `dput()` to post your data? In fact, we should be able to copy-paste your code an reproduce your problem. You may want to have a look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Paul Jul 17 '20 at 13:46
  • Added dput(), thanks for the help. – John Jul 17 '20 at 14:42

1 Answers1

1

I'm not sure if I understood the question but I think you want to add the sum of the confirmed cases as labels. There might be a ggplot way of doing it but I think the most straightforward way is to make another dataset with your labels and feed it in.

date_labels <- filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0) %>% group_by(Date) %>% summarise(confirmed_cases = sum(Cases[CASE_STATUS == "Confirmed"]))

ggplot(filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0)) +
  geom_col(aes(x = Date, y = Cases, fill = CASE_STATUS), position = position_stack(reverse = TRUE), width = .88)+
  geom_text(data = date_labels, aes(x = Date, y = 1, label = confirmed_cases), position = position_stack(reverse = TRUE), vjust = 1.5, size = 3, color = "white") +
  scale_fill_manual(values = c('blue',"tomato"))+
  scale_x_date(labels = label_date("%m/%d"), limits = as.Date(c('2020-07-09','today()')), breaks = "1 week")+
  theme(axis.text.x = element_text(angle=0))+
  labs(title = "Antrim - Grand Traverse - Charlevoix")

Gives me this result: My Results

Dharman
  • 30,962
  • 25
  • 85
  • 135
S. Kelly
  • 155
  • 10
  • Exactly, thanks so much. I do see a message " `summarise()` ungrouping output (override with `.groups` argument) " – John Jul 18 '20 at 11:23
  • Found the reason for the statement, as the code needed to be ungrouped. Works fine. – John Jul 18 '20 at 11:35
  • Would be nice to put the data label above the two bars instead of at a y value? – John Jul 18 '20 at 13:08
  • @Dharman I also added aes(x = Date, y = confirmed_cases, label, which puts the number at the top of the blue bar. Just need to figure out now how to accept your answer. – John Jul 18 '20 at 14:03
  • @John I am not the author of this answer. If you want to accept an answer which helped you, you can click the ✓ mark on the left side of this answer. You don't have to do so but it will help future readers – Dharman Jul 18 '20 at 14:05