```{css, echo=FALSE} img { border: 0; }
```r knitr::opts_chunk$set( collapse = TRUE, comment = "#>", dev = "ragg_png" ) library("ragg") library("hypoimg")
The hypoimg package provides illustrations to annotate plots within studies of the Caribbean hamlets (Hypoplectrus spp).
It contains the basic illustrations of different hamlet species and countries where hamlets occur. Additionally, the package provides a set of functions to construct legends which link a color map with a set of hamlet species, pairwise species comparisons (or countries).
Additionally to this package, resources for the analysis of genomic data of hamlets are provided within the hypogen package.
Using the remotes package, hypoimg can be installed directly from github:
install.packages("remotes") remotes::install_github("k-hench/hypoimg")
The core of the hypoimg package is the selection of hamlet illustrations:
ggplot(tibble(x = c(0,5.2), y = c(0,0)),aes(x = x, y = y))+ geom_blank()+theme_void()+ coord_fixed()+ hypo_anno_r('puella', xmin = 0, xmax = 1)+ hypo_anno_l('providencianus', xmin = 1.2, xmax = 2.2)+ hypo_anno_l('indigo', xmin = 2.2, xmax = 3.2)+ hypo_anno_l('gummigutta', xmin = 3.2, xmax = 4.2)+ hypo_anno_l('unicolor', xmin = 4.2, xmax = 5.2)+ scale_y_continuous(limits = c(-.2,.2))+ theme(panel.background = element_rect())
The package contains one left and one right facing illustration for 19 different hamlet species and morphotypes. They are made available as grid objects (grob) in the tibble hypo_img
:
hypo_img
A pure version of the hamlet annotations can be added to any ggplot using the the functions hypo_anno_l()
and hypo_anno_r()
. These functions are simple wrappers around the ggplot2 function annotation_custom(()
.
ggplot(tibble(x = c(3, 3.25, 3.75),y = c(-.1, -.3, .3)), aes(x = x, y = y))+ geom_line(size = 4, color = "#35B779")+ hypo_anno_r('puella', xmin = 0, xmax = 1)+ hypo_anno_l('indigo', xmin = 1.5, xmax = 2.5)+ coord_fixed()+ scale_x_continuous(limits = c(0, 3.75))+ scale_y_continuous(limits = c(-.35, .35))
A common task is a contrast different hamlet species in genomic or ecologic terms. Here it might be useful to assign individual colors to the individual hamlet species. The hypoimg package provides a way to create a custom legend for such a situation.
An individual color - species assignment can be shown using the hypo_anno_single()
function:
hypo_anno_single(species = 'aberrans', circle_fill = "#35B779", circle_color = 'black', plot_names = TRUE, plot_name_size = 5)
The same is possible for pairs of species using hypo_anno_pair()
:
hypo_anno_pair(left = 'ecosur', right = 'maya', circle_fill = "#F16913", circle_color = 'black', plot_names = TRUE, plot_name_size = 5)
If several different species are used within one plot a custom legend can be generated based on the species list and the color map using hypo_legend_single()
.
To be able to use this legend the it needs to be transformed into a grob using ggplot2::ggplotGrob()
.
set.seed(5) # For this demonstration we'll create a fake length - weight reltionship data table. # To do this, we'll implement the relationship as stated by fishbase for H. puella: # http://www.fishbase.se/summary/Hypoplectrus-puella.html hamlet_length_weight <- function(x, a = 0.009, b = 3.04 , rand = 0){ a * x^b + rnorm(length(x)) * rand } # The species involved in the fake data set hamlet_species <- c('nigricans', 'unicolor', 'gemma') # The fake dataset hamlet_data <- tibble(species = rep(hamlet_species, each = 10), len = sample(5:17, replace = TRUE , size = 30)+rnorm(30)) %>% mutate(weig = ifelse(species == 'nigricans', hamlet_length_weight(x = len, rand = 1) + rnorm(10), ifelse(species == 'unicolor', hamlet_length_weight(x = len, b = 3.14,rand = 1.5), hamlet_length_weight(x = len, b = 2.94,rand = 1.5)))) # Our color map clr_single <- viridis::viridis(3) # The custom legend is created based on the species list and the color map legend_grob_single <- hypo_legend_single(species = hamlet_species, color_map = clr_single, circle_color = 'black', plot_names = TRUE) %>% ggplotGrob() # We add the custom legend using ggplot2::annotation_custom() ggplot(hamlet_data, aes(x = len, y = weig, color = species, fill = species)) + geom_line(size = 1.2)+ geom_point(color = "black", size = 2.5, shape = 21)+ annotation_custom(legend_grob_single,xmin = 3,xmax = 9.5, ymin = 42)+ labs(x = "Length (cm)", y = "Weight (g)")+ scale_color_manual(values = clr_single, guide = FALSE)+ scale_fill_manual(values = clr_single, guide = FALSE)
Similarly, the pair wise elements can be combined into a legend using hypo_legend_pair()
# The species involved in the fake data set left <- c("aberrans", "aberrans", "chlorurus") right <- c("aberrans", "chlorurus", "chlorurus") # For this demonstration we'll create a fake pairing experiment with egg counts. # this function samples counts for 15 pairings egg_c <- function(x){sample(x,size = 15,replace = TRUE)} # The fake dataset pairing_data <- tibble(pair = rep(str_c(left, right, sep = "-"), each = 15), pairing_egg_counts = c(egg_c(90:110),egg_c(50:95), egg_c(60:100)), time_point = factor(rep(rep(c('morning','noon','evening'), each=5), 3), levels = c('morning','noon','evening'))) # Our color map clr_pair <- RColorBrewer::brewer.pal(3,"RdBu") # The custom legend is created based on two species lists and the color map legend_grob_pair <- hypo_legend_pair(left = left, right = right, color_map = clr_pair, circle_color = 'black', plot_names = TRUE, circle_lwd = .5) %>% ggplotGrob() # We add the custom legend using ggplot2::annotation_custom() ggplot(pairing_data, aes(y = pairing_egg_counts, x = time_point, fill = pair))+ geom_boxplot()+ annotation_custom(legend_grob_pair, xmin = 3.25, ymin = 70)+ coord_cartesian(xlim = c(0.75,4.6))+ labs(x = "Time point", y = "Egg count / pairing (n)")+ scale_fill_manual(values = clr_pair,guide = FALSE)
ggplot(tibble(x = c(0,5.2), y = c(0,0)),aes(x = x, y = y))+ geom_blank()+ theme_void()+ coord_fixed()+ hypo_anno_flag('cuba', xmin = 0, xmax = 1)+ hypo_anno_flag('caribbean', xmin = 2, xmax = 3)+ hypo_anno_flag('panama', xmin = 4, xmax = 5)+ scale_y_continuous(limits = c(-.2,.2))+ theme(panel.background = element_rect())
Besides the hamlet illustrations, the hypoimg also provides labels for all countries of the Caribbean and the Golf of Mexico:
hypo_flag
An overview of all the flags can be generated with hypo_flag_palette()
.
hypo_flag_palette()
Apart from this there exist one flag version for all of the previously discussed functions:
hypo_anno_flag()
hypo_anno_flag_single()
hypo_anno_flag_pair()
hypo_legend_flag_single()
hypo_legend_flag_pair()
set.seed(3) p1 <- ggplot(data = tibble(x=seq(0,10,length.out = 50),y=sin(x)+.3*rnorm(50)),aes(x=x,y=y))+ geom_area(fill = alpha('black',.2))+ geom_point()+ hypo_anno_flag('panama',xmin = 4.2,xmax = 5.2,ymin = -.5,ymax = -.05)+ scale_x_continuous(limits = c(0,10))+coord_fixed(ratio = 3) p2 <- hypo_anno_flag_single(geo = 'trinidad_and_tobago', flag_lwd = 1, flag_line_color = 'black', flag_radius_scale = 1, circle_fill = RColorBrewer::brewer.pal(3,'Blues')[3], circle_color = 'black',circle_lwd = 1,plot_names = TRUE) p3 <- hypo_legend_flag_pair(left = c('panama','mexico','cuba'), right = c('mexico','cuba','panama'), color_map = RColorBrewer::brewer.pal(3,'PRGn'), flag_lwd = 1, flag_line_color = 'black', flag_radius_scale = 1, circle_color = 'black', plot_names = TRUE) cowplot::plot_grid(p1, p2, p3, nrow = 1)
grImport2::grid.picture(grImport2::readPicture("logo.c.svg"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.