Skip to content

Instantly share code, notes, and snippets.

@YiLi225
Created March 29, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YiLi225/7b26121c446bda7bf56de1ece3edd279 to your computer and use it in GitHub Desktop.
Save YiLi225/7b26121c446bda7bf56de1ece3edd279 to your computer and use it in GitHub Desktop.
ggplot line chart helper function
#### helper function for trajectory line chart:
#### note: there is global variable in this function!!!
helper_vis_continuous <- function(dat = country_data, country = 'US') {
## vertical structure for ggplot
current_dat = dat %>%
filter(Country.Region == country) %>%
reshape2::melt(.) %>%
set_colnames(c('Country', 'Status', 'Date', 'Total'))
## starts with the date when 1st case was confirmed for this country
case1_idx = which(current_dat$Total > 0)[1]
current_dat = current_dat[case1_idx:nrow(current_dat), ]
plot <-
ggplot(dat = current_dat,
aes_string(x = 'Date', y = 'Total', color = 'Status', group = 'Status', linetype = 'Status')) +
geom_line(lwd = 1.2) +
labs(subtitle = sprintf('%s', country)) +
xlab('Date') + ylab('Total Numbers') +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 75, hjust = 1),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = 'None')
return(plot)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment