Chapter 46 Distribution of Ages

breaks = c(10,20,30,40,50,60,70,80,90,100)

AdverseFoodEvents %>%
  filter(!is.na(AgeAtAdverseEventInYears)) %>%
  group_by(AgeAtAdverseEventInYears) %>%
  summarise(Count = n()) %>%
  ungroup() %>%
  mutate(AgeAtAdverseEventInYears = as.numeric(AgeAtAdverseEventInYears)) %>%
  
  ggplot(aes(x = AgeAtAdverseEventInYears,y = Count)) +
  geom_bar(stat='identity',colour="white", fill = c("red")) +
  scale_x_continuous(limits = c(0, 100),breaks=breaks ) +
  labs(x = 'Age in Years', 
       y = 'Count', 
       title = 'Age and Count') +
  
  theme_bw()

Ages 50 onwards, the number of adverse events rises.

PlotProducts = function(ds,fillColorName)
{
  ds %>%
  group_by(ProductName) %>%
  summarise(Count = n()) %>%
  arrange(desc(Count)) %>%
  ungroup() %>%
  mutate(ProductName = reorder(ProductName,Count)) %>%
  head(10) %>%
  
  ggplot(aes(x = ProductName,y = Count)) +
  geom_bar(stat='identity',colour="white", fill = c(fillColorName)) +
  geom_text(aes(x = ProductName, y = 1, label = paste0("(",Count,")",sep="")),
            hjust=0, vjust=.5, size = 4, colour = 'black',
            fontface = 'bold') +
  labs(x = 'Products', 
       y = 'Count', 
       title = 'Products') +
  coord_flip() + 
  theme_bw()

}

We examine the Products which cause Adverse Events for the following age groups

  • 70 and Above
  • 60 to 69
  • 50 to 59
  • Less than 5

46.1 70 and Above Adverse Products

AdverseFoodEvents %>%
  mutate(AgeAtAdverseEventInYears = as.integer(AgeAtAdverseEventInYears)) %>%
  filter(AgeAtAdverseEventInYears >=70) %>%
  filter(ProductName != "REDACTED") %>%
  PlotProducts(fillColorName = fillColor)

46.2 Age 60 - 69 Adverse Products

AdverseFoodEvents %>%
  mutate(AgeAtAdverseEventInYears = as.integer(AgeAtAdverseEventInYears)) %>%
  filter(AgeAtAdverseEventInYears <70) %>%
  filter(AgeAtAdverseEventInYears >=60) %>%
  filter(ProductName != "REDACTED") %>%
  PlotProducts(fillColorName = fillColor2)

46.3 Age 50 - 59 Adverse Products

AdverseFoodEvents %>%
  mutate(AgeAtAdverseEventInYears = as.integer(AgeAtAdverseEventInYears)) %>%
  filter(AgeAtAdverseEventInYears <60) %>%
  filter(AgeAtAdverseEventInYears >=50) %>%
  filter(ProductName != "REDACTED") %>%
  PlotProducts(fillColorName = fillColor)

46.4 Less than 5 Adverse Products

AdverseFoodEvents %>%
  mutate(AgeAtAdverseEventInYears = as.numeric(AgeAtAdverseEventInYears)) %>%
  filter(AgeAtAdverseEventInYears < 5) %>%
  filter(ProductName != "REDACTED") %>%
  PlotProducts(fillColorName = fillColor)