# INBO CODING CLUB 28 januari 2020 Welcome! ## Deelnemers Dirk Patrik Sander ## Share your code snippet; If you want to share your code snippet, copy paste your snippet within a section of three backticks (```): As an **example**: ``` library(tidyverse) ... ``` (*you can copy paste this example and add your code further down*) Your snippets: ### Challenge 1 #### step by step ``` p1 <- ggplot(fit_results, aes(x = year, y = obs)) + geom_col() + facet_wrap(~scientificName, scales = "free_y") p1 p2 <- p1 + geom_line(aes(y = fit), colour = "red", linetype = "dotted") p2 p3 <- p2 + geom_ribbon(aes(ymin = lcl, ymax = ucl), alpha = 0.3, fill = "purple") p3 p4 <- p3 + ggtitle("Number of observations in Belgian Natura2000 protected areas", subtitle = "Source: GBIF") p4 p5 <- p4 + theme(plot.title = element_text(hjust = 0), plot.subtitle = element_text(hjust = 0)) p5 ``` I like it with points instead of bars: ``` p <- ggplot(fit_results, aes(x = year)) + #geom_bar(aes(y = obs), stat = "identity") + geom_point(aes(y = obs)) + geom_line(aes(y = fit), colour = "red", linetype = "dashed", size = 1) + geom_ribbon(aes(ymin = lcl, ymax = ucl), fill = "purple", alpha = 0.3) + facet_wrap(vars(scientificName), scales = "free", nrow = 2, ncol = 2) + ggtitle("Number of observations in Belgian Natura2000 protected areas", subtitle = "Source: GBIF") + scale_y_continuous("number of observations") + theme(plot.title = element_text(hjust = 0), plot.subtitle = element_text(hjust = 0)) print(p) ``` ### Challenge 2 #### alternative only difficult for the subtitle ``` library(gridExtra) library(gridGraphics) p <- ggplot(fit_results_sum, aes(x = year, y = obs)) + geom_point() s <- ggplot(fit_results, aes(x= year, y= obs)) + facet_wrap(~scientificName, nrow = 2, ncol = 2, scales = 'free') + geom_line(linetype = "dotted", color = "red") + geom_ribbon(aes(ymin =lcl, ymax= ucl), colour = "purple", alpha=0.3) lay <- rbind(c(1,1), c(2,2), c(2,2)) grid.arrange(p, s, layout_matrix = lay, top = textGrob("Number of observations in Belgian Natura2000 protected areas source: GBIF", hjust = 0.65)) ... ``` ``` pgrid <- ggplot(fit_results) + geom_line(aes(y = obs, x = year))+ facet_wrap(.~scientificName, nrow = 2, scales= 'free') + geom_line(aes(x=year, y=fit),color='red', linetype='dotted')+ geom_ribbon(aes(ymin=lcl,ymax=ucl,x=year),alpha=0.3, fill='purple') psummary<-ggplot(fit_results_sum)+ geom_line(aes(y = obs, x = year))+ labs(title = 'Number of observations in Belgian Natura2000 protected areas',subtitle = 'Source: GBIF')+ theme(plot.title = element_text(hjust = 0),plot.subtitle = element_text(hjust = 0)) plot_grid(psummary, pgrid, nrow=2) ``` ``` p1 <- fit_results %>% ggplot(aes(x = year, y = obs)) + geom_bar(stat = "identity") + facet_wrap(~scientificName, scales = "free") + geom_line(aes(y = fit), color = "red", linetype = "dashed") + geom_ribbon(aes(ymin = lcl, ymax = ucl), color = "purple", alpha = 0.3) + labs(y = NULL, x = NULL) p2 <- fit_results_sum %>% ggplot(aes(x = year, y = obs)) + geom_area(stat = "identity") + labs(title = "Number of observations in Belgian Natura2000 protected areas", subtitle = "Source: GBIF", y = NULL, x = NULL) + theme(plot.title = element_text(hjust = 0), plot.subtitle = element_text(hjust = 0)) ggarrange(p2, p1) ``` #### one step more ``` q <- fit_results_sum %>% ggplot(aes(x = year, y = obs)) + geom_area() + ggtitle("Number of observations in Belgian Natura2000 protected areas", subtitle = "Source: GBIF") egg::ggarrange(q, p3, ncol = 1) ``` ### Challenge 3 #### Option 1 Damiano: I forgot to put link of GBIF logo in R code. I did it now. You can download R script again, or just copy this line in your R script: ``` link <- "https://github.com/inbo/coding-club/raw/master/docs/assets/images/20191219_logo_gbif.png" ``` ``` logo<- image_read(link) library(grid) pgrid <- ggplot(fit_results) + geom_line(aes(y = obs, x = year))+ facet_wrap(.~scientificName, nrow = 2, scales= 'free') + geom_line(aes(x=year, y=fit),color='red', linetype='dotted')+ geom_ribbon(aes(ymin=lcl,ymax=ucl,x=year),alpha=0.3, fill='purple') psummary<-ggplot(fit_results_sum)+ geom_line(aes(y = obs, x = year))+ labs(title = 'Number of observations in Belgian Natura2000 protected areas')+ theme(plot.title = element_text(hjust = 0)) Pboth <- plot_grid(psummary, pgrid, nrow=2) #Add figure using cowplot ggdraw() + draw_plot(Pboth) + draw_image(logo, x = 0, hjust = 0, vjust=-4.2,width = 0.1, height = 0.2) ``` ``` # Add an animated farting chicken to your plot # Save your plot (the one from fit_results_sum, I called it p1) and load it as a magick image object ggsave(p1, file="p1.png", width = 7, height = 5, dpi = 100) plot <- image_read("p1.png") plot # Load the chicken gif kip <- image_read("https://media.giphy.com/media/kGLpbileUNXfdb5SKl/giphy.gif") %>% image_scale("100") # Set the plot as the background of the final image background <- image_background(image_scale(plot, "500"), "white", flatten = TRUE) # Combine background and chicken frames <- image_composite(background, kip, offset = "+150+100") animation <- image_animate(frames, fps = 5) print(animation) #Save gif image_write(animation, "kip.gif") ``` #### Option 2 ``` dynamic_plot <- ggplot(survey_timestamp) + geom_point(aes(weight, hindfoot_length, color = sex),alpha = 0.3) + transition_time(timestamp) + shadow_mark()+ labs(title = 'hindfoot length vs weight, \n Time: {frame_time}') animate(dynamic_plot) ``` ##### with a nice title ``` static_plot + transition_time(timestamp) + shadow_mark() + ggtitle('Showing timestamp {frame_time}', subtitle = 'Frame {frame} of {nframes}') ```