Primarily, MBERCr is a package of simple utility features for data manipulation and simple statistics. It also contains personalisable palettes and themes for ggplot2 plots. Plus, any random crap that Molly adds.
You can install the development version of MBERCr from Github with:
install.packages("devtools")
devtools::install_github("MBERC/MBERCr")
Before showing the features we need to gather some data to be able to look at the functions of the package. To do this we will first of all load some packages (all available from CRAN apart from MBERCr).
library(marmap)
library(tidyverse)
library(sf)
library(lme4)
library(MBERCr)
library(mapdata)
library(maps)
library(palmerpenguins)
library(patchwork)
Once the packages are loaded we can then start getting our data ready for the examples. For some example data we will grab the Palmer Penguins data then also get some bathymetry data from the marmap package that extracts bathymetry from the NOAA databases.
# get palmer penguins df
data(package = 'palmerpenguins')
df<-penguins
# get bathymetry data
b <- getNOAA.bathy(lon1 = 160, lon2 = 180, lat1 = -28.5, lat2 = -49.2,
resolution = 5)
# convert bathymetry matrix to spatial object
B <- as.SpatialGridDataFrame(b)
# convert spatial object to special features
sf_bathy <- st_as_sf(B) %>%
dplyr::filter(layer<0.1)
# get land outline
nz <- map_data("nz")
However, brilliant your data are there will often be NA values. Sometimes this is fine but specific columns may be used for analysis later and inclusion of NA’s might therefore be a nuisance. We can use the completeFun(), where we define the dataframe to use and then the column/s we want to check to remove rows if they contain NAs.
head(df)
#> # A tibble: 6 × 8
#> species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex
#> <fct> <fct> <dbl> <dbl> <int> <int> <fct>
#> 1 Adelie Torge… 39.1 18.7 181 3750 male
#> 2 Adelie Torge… 39.5 17.4 186 3800 fema…
#> 3 Adelie Torge… 40.3 18 195 3250 fema…
#> 4 Adelie Torge… NA NA NA NA <NA>
#> 5 Adelie Torge… 36.7 19.3 193 3450 fema…
#> 6 Adelie Torge… 39.3 20.6 190 3650 male
#> # … with 1 more variable: year <int>
# remove rows where sex is NA
df2<-MBERCr::completeFun(df,"sex")
head(df2)
#> # A tibble: 6 × 8
#> species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex
#> <fct> <fct> <dbl> <dbl> <int> <int> <fct>
#> 1 Adelie Torge… 39.1 18.7 181 3750 male
#> 2 Adelie Torge… 39.5 17.4 186 3800 fema…
#> 3 Adelie Torge… 40.3 18 195 3250 fema…
#> 4 Adelie Torge… 36.7 19.3 193 3450 fema…
#> 5 Adelie Torge… 39.3 20.6 190 3650 male
#> 6 Adelie Torge… 38.9 17.8 181 3625 fema…
#> # … with 1 more variable: year <int>
Often we use regression models to assess data. If we want to check the assumptions of these models the base r functions suck. There are good packages to check these assumptions () but we also have another here.
mod1<-lm(body_mass_g~.,data=df2)
MBERCr::Model_Check(mod1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
In MBERCr we have many functions to make figures look nicer (we think). Lets do a basic plot.
ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Species")
#> `geom_smooth()` using formula 'y ~ x'
Now lets make the theme look better.
themeplot1<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Species")+
MBERCr::theme_Bede()
themeplot2<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Species")+
MBERCr::theme_Amelia()
themeplot1+themeplot2&
theme(legend.position = c(0.2,0.8))
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
Okay that is better but lets make the colours nicer.
colourplot1<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="main",
title="Main Palette from Bede")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Bede("main")
colourplot2<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="mixed",
title="Mixed Palette from Bede")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Bede("mixed")
colourplot3<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="cool",
title="Cool Palette from Bede")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Bede("cool")
colourplot4<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="hot",
title="Hot Palette from Bede")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Bede("hot")
colourplot5<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="PhD Bede",
title="PhD Palette from Bede")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Bede("PhD")
colourplot6<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="PhD Amelia",
title="PhD Palette from Amelia")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Amelia("PhD")
colourplot7<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Pal_1 Clara",
title="Palette 1 from Clara")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Clara("Pal_1")
colourplot8<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Pal_2 Clara",
title="Palette 2 from Clara")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Clara ("Pal_2")
colourplot9<-ggplot(data = df2, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species),
size = 1)+
geom_smooth(aes(color=species),method = "lm")+
labs(x="Flipper Length (mm)",
y="Body Mass (g)",
color="Pal_3 Clara",
title="Palette 3 from Clara")+
MBERCr::theme_Bede()+
MBERCr::scale_colour_Clara("Pal_3")
colourplot1+colourplot2+
colourplot3+colourplot4+
colourplot5+colourplot6+
colourplot7+colourplot8+
colourplot9 &
theme(legend.position = c(0.15,0.7))
There also a few colours palettes for gradients by putting discrete=FALSE.
map1<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Bede(name="Depth (m) Bathy_Blues Pallette",'Bathy_Blues',
discrete=FALSE,reverse=TRUE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map2<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Bede(name="Depth (m) Mixed Pallette",'mixed',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map3<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Bede(name="Depth (m) PhD Pallette",'PhD',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map4<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Bede(name="Depth (m) Main Pallette",'main',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map5<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Amelia(name="Depth (m) PhD Palette from Amelia",
'PhD',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map6<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Clara(name="Depth (m) Pallette 1 from Clara",
'Pal_1',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map7<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Clara(name="Depth (m) Pallette 2 from Clara",
'Pal_2',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
map8<-ggplot()+
geom_sf(data=sf_bathy,mapping=aes(colour=layer))+
geom_polygon(data = nz, aes(x = long, y = lat, group = group),
fill = "forestgreen", colour = "black",alpha=0.4)+
MBERCr::scale_colour_Clara(name="Depth (m) Pallette 3 from Clara",
'Pal_3',discrete=FALSE)+
MBERCr::theme_Bede()+
labs(x="Longitude",
y="Latitude")
(map1+map2)/(map3+map4)/(map5+map6)/(map7+map8)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.