# reading the data
pet_data <- read_csv("train.csv")
adoptionspeed_data <- data_frame(AdoptionSpeed = (c(0:4)),
                                 AdoptionLabels = c("Immediately", "Up to a week",
                                                    "Up to a month",
                                                    "Up to three months",
                                                    "Never get adopted"))
# making the data ready for visualization
pet_data <- pet_data %>% left_join(adoptionspeed_data, by = "AdoptionSpeed") %>%
    mutate(AdoptionLabels= factor(AdoptionLabels, 
                                     levels = c("Immediately", "Up to a week",
                                                "Up to a month",
                                                "Up to three months",
                                                "Never get adopted")))

## preparing the data for further analyses

pet_data <- within(pet_data, {
    AdoptionSpeed <- factor(AdoptionSpeed)
    Type <- factor(ifelse(Type == 1, "dog", "cat"))
})


# plotting the data
ggplot(data = pet_data, aes(x = Type, y = Age, 
                            group = Type,
                            color = AdoptionLabels))+
    geom_boxplot()+
    scale_y_continuous("Age (months)",
                       limits = c(0,30))+
    theme(legend.position = "none",
          plot.title = element_text(hjust = 0),
          axis.text=element_text(size=12),
          axis.title = element_text(size = 14))+
    theme_bw()+
    labs(title = "Relation between adoption speed and age for cats and dogs", 
                       subtitle = "Adoption Speed: {closest_state}")+
    transition_states(AdoptionLabels,
                      transition_length = 2,
                      state_length = 1) +
    enter_fade() + 
    exit_shrink() +
    ease_aes('sine-in-out')