Chapter 47 Ten Most Common Outcomes

We examine the Top Most Common Outcomes associated with the Adverse Events and plot them in the bar plot.

Outcomes = str_split(AdverseFoodEvents$Outcomes,',') 

AllOutcomes <- data.frame(matrix(unlist(Outcomes),byrow=T),stringsAsFactors=FALSE)

colnames(AllOutcomes) = c("OutcomeName")

#trimws returns a character string with leading and/or trailing whitespaces removed.

AllOutcomes$OutcomeName = trimws(AllOutcomes$OutcomeName)

AllOutcomes %>%
  group_by(OutcomeName) %>%
  summarise(Count = n()) %>%
  arrange(desc(Count)) %>%
  ungroup() %>%
  mutate(OutcomeName = reorder(OutcomeName,Count)) %>%
  head(10) %>%
  
  ggplot(aes(x = OutcomeName,y = Count)) +
  geom_bar(stat='identity',colour="white", fill = fillColor) +
  geom_text(aes(x = OutcomeName, y = 1, label = paste0("(",Count,")",sep="")),
            hjust=0, vjust=.5, size = 4, colour = 'black',
            fontface = 'bold') +
  labs(x = 'Outcome', 
       y = 'Count', 
       title = 'Outcome and Count') +
  coord_flip() + 
  theme_bw()