# INBO CODING CLUB 26 March 2024 Welcome! ## 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*) ## Yellow sticky notes No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below). ## Participants Name | Challenges --- | --- Raïsa Carmen | *** Jorre Vannieuwenhuyze | *** Adriaan Seynaeve | Wouter Depaepe | Heleen Deroo | * Anja Leyman | Leen Govaere | An |** ## Challenge 1 ### Raïsa's solution (example) Copy paste this section to show your solutions. ```r # dummy code print("This is how to insert code.") ``` ```r ### Heleen's solution plot1 <- ggplot(rodents) + geom_bar(aes(x = year, fill = institutionCode)) + labs(y = "observations") plot1 ggplotly(plot1) plot2 <- ggplot(rodents) + geom_bar(mapping = aes(x = month, fill = species)) + facet_wrap(~species, scales = "free") + labs(y = "observations") + scale_x_continuous(breaks = pretty_breaks()) plot2 rodents_n_2000 <- rodents %>% dplyr::filter(year >= 2000) %>% dplyr::group_by(species,year, month) %>% dplyr::count() %>% mutate(date = make_date(year, month, 1)) plot3 <- ggplot(data = rodents_n_2000, mapping = aes(x = date, y = n, color = species)) + geom_line() + facet_wrap(~species, scales = "free") + labs(y = "observations") + scale_x_date( NULL, breaks = scales::breaks_width("5 years"), labels = scales::label_date("'%y")) + scale_y_continuous( labels = scales::label_number(scale_cut = scales::cut_short_scale())) plot3 ``` ```r #jorre plot1 <- plot1 |> ggplotly() plot2 <- plot2 + scale_x_continuous(breaks=pretty_breaks()) plot3 <- plot3 + scale_x_date(labels = scales::label_date("'%y")) + scale_y_continuous( labels = scales::label_number(scale_cut = scales::cut_short_scale())) ``` ## Challenge 2 ```r # Jorre plot1 + plot2 + plot3 + plot4 + plot_layout(nrow = 2, byrow = FALSE) plot1 + facet_zoom(x = year %in% 2016:2020) ```` ```r # Wouter plot1 + plot2 + plot3 + plot4 plot4 | (plot1 / plot2) + plot_annotation(title = "I like this layout, I disabled plot4's legend") plot1 + facet_zoom(x = year == c(2016,2020)) ``` ### Raïsa's solution ```r #Save some space by grouping the legends as much as possible library(viridis) #other colors for the institution then for the species ((plot1 + scale_fill_viridis(discrete = TRUE)) + plot2 + (plot3 + theme(legend.position = "none")) + (plot4 + theme(legend.position = "none"))) + plot_layout(guides = 'collect') #puts all legends to the side # Using ggforce, add a "facet zoom" to `plot1` focussing on the last 5 years. plot1 + facet_zoom(year >= 2015) ``` ## Challenge 3 ```r # jorre plot4_anim <- rodents |> filter(!is.na(decimalLatitude),!is.na(decimalLongitude)) |> sample_n(2000) |> # sample 2000 rows for this exercise to increase speed ggplot() + geom_point(mapping = aes(x = decimalLongitude, y = decimalLatitude, color = species)) + labs(x = "latitude", y = "longitude") + transition_states(species) + ggtitle('Now showing {closest_state}', subtitle = 'Frame {frame} of {nframes}') plot4_anim plot4_anim2 <- rodents |> filter(!is.na(decimalLatitude),!is.na(decimalLongitude)) |> filter(year %in% 2015:2020) |> # only last 5 years for speed ggplot() + geom_point( mapping = aes( x = decimalLongitude, y = decimalLatitude, color = species, group= seq_along(year) ) ) + labs(x = "latitude", y = "longitude") + transition_states(year) + ggtitle('Now showing {closest_state}', subtitle = 'Frame {frame} of {nframes}' ) + enter_fade() + exit_shrink() plot4_anim2 |> animate(nframes=50,fps=5) ``` ### Raïsa's solution ```r anim1 <- plot4 + ggtitle('Now showing {closest_state}', subtitle = 'Frame {frame} of {nframes}') + transition_states(species) + enter_fade() + exit_fade() animate(anim1) anim_save("./src/20240326/20240326_animation_plot4_by_species.tif") #' 2. Create an animation showing the observations year by year. By default the animation has 100 frames and 10 frame per seconds # (fps), i.e. a duration of 10s. How to change these values? # rewritten plot4 with `year` as "group" in aesthetics of `geom_point()`. # Without the group gganimate thinks to group by color. anim2 <- ggplot(rodents %>% filter(!is.na(decimalLatitude), !is.na(decimalLongitude))) + geom_point(mapping = aes(x = decimalLongitude, y = decimalLatitude, group = seq_along(year), color = species)) + transition_time(year) + labs(x = "latitude", y = "longitude", title = 'Year: {frame_time}') + enter_fade() + exit_shrink() animate(anim2, nframes = 400) # It can take long anim_save("./src/20240326/20240326_animation_plot4_by_year.tif") ``` ## BONUS challenge ### Raïsa's solution ```r logo_file <- "./data/20240326/coding_club_logo_1.png" logo <- image_read(logo_file) # Add logo as an annotation raster with ggplot image <- image_fill(logo, 'none') raster <- as.raster(image) plot1 + annotation_raster(raster, xmin = 2010, xmax = 2020, ymin = 14000, ymax = 17500) ``` ### Jorre Function below was taken from https://themockup.blog/posts/2019-01-09-add-a-logo-to-your-plot/. It is not the most efficient solution because it doesn't work on your ggplot object directly, you first have to save it as a another image on disk. But it might be more robust. ```r add_logo <- function(plot_path, logo_path, logo_position, logo_scale = 10){ # Requires magick R Package https://github.com/ropensci/magick # Useful error message for logo position if (!logo_position %in% c("top right", "top left", "bottom right", "bottom left")) { stop("Error Message: Uh oh! Logo Position not recognized\n Try: logo_positon = 'top left', 'top right', 'bottom left', or 'bottom right'") } # read in raw images plot <- magick::image_read(plot_path) logo_raw <- magick::image_read(logo_path) # get dimensions of plot for scaling plot_height <- magick::image_info(plot)$height plot_width <- magick::image_info(plot)$width # default scale to 1/10th width of plot # Can change with logo_scale logo <- magick::image_scale(logo_raw, as.character(plot_width/logo_scale)) # Get width of logo logo_width <- magick::image_info(logo)$width logo_height <- magick::image_info(logo)$height # Set position of logo # Position starts at 0,0 at top left # Using 0.01 for 1% - aesthetic padding if (logo_position == "top right") { x_pos = plot_width - logo_width - 0.01 * plot_width y_pos = 0.01 * plot_height } else if (logo_position == "top left") { x_pos = 0.01 * plot_width y_pos = 0.01 * plot_height } else if (logo_position == "bottom right") { x_pos = plot_width - logo_width - 0.01 * plot_width y_pos = plot_height - logo_height - 0.01 * plot_height } else if (logo_position == "bottom left") { x_pos = 0.01 * plot_width y_pos = plot_height - logo_height - 0.01 * plot_height } # Compose the actual overlay magick::image_composite(plot, logo, offset = paste0("+", x_pos, "+", y_pos)) } logo_file <- "./data/20240326/coding_club_logo_1.png" plot_file <- "./data/20240326/plot1.png" ggsave(plot_file,plot1) add_logo( plot_path=plot_file, logo_path=logo_file, logo_position="top right", logo_scale = 10 ) ``` ```