library(knitr); library(ggplot2); library(pkrf)
rootdir <- 'ENTER WORK DIRECTORY HERE'
opts_chunk$set(
  root.dir=rootdir,
  tidy=FALSE,
  warning=FALSE,
  message=FALSE,
  comment=NA,
  cache=F,
  verbose=FALSE,
  fig.width=6,
  fig.height=4,
  fig.align='center',
  dev.args=list(bg="transparent"))
opts_knit$set(root.dir=rootdir)


theme_set(theme_light(base_size=12))
theme_update(panel.background = element_rect(fill = "transparent", colour = NA),
             plot.background  = element_rect(fill = "transparent", colour = NA))

This is a slide | With a subtitle

This is a subsection

This is a 'chapter' slide {data-background=r pkPal()[7]}

Color scheme

The color scheme used in this template is, next to many others, available via the custom palette function of the pkrf package:

library(pkrf)
mhcolors <- pkPal('mnthydro')



The mountain hydrology palette consists of the following colors:

par(mar=c(1.5,0,0,0)); image(matrix(seq(mhcolors)),col=mhcolors,axes=F)
mtext(1:length(mhcolors), at=seq(0,1,length.out=length(mhcolors)), side=1, line=0.5)



To show all palettes and their colors available in pkrf in a plot use the following syntax:

pkPal(show=TRUE)

Equations

Equations can simply be added inline using latex syntax, for example $f(x)=sin(x)$.


They can also be put centered on a separate line:
$${Q_i} = \chi {a_i}H_i^5\nabla z_i^3$$

Tables

Tables can be added in multiple ways.
For instance a raw HTML table using knitr::kable, or a nicely formatted table using DT::datatable

library(DT); library(pkrf)
glacsum <- aggregate(glaciers[,c(7,11)], by=list(glaciers$region), FUN=sum)
datatable(glacsum, rownames=F, options=list(pageLength=5),
          colnames=c('Region','Area (km2)','Mass (Gt)'))

Figures

Figures can be added by running R code directly. For instance a curve of $f(x)=sin(x)$:

par(mar=c(4,4,1,0.5), mgp=c(2.5,0.75,0), cex.axis=0.75, tck=-0.02, las=1)
curve(sin(x), xlim=c(-2*pi,2*pi), xaxs='i', col=mhcolors[1], lwd=3)
abline(v=0,h=0,lty=3)

Interactive figures

Since ioslides is html, any <script> can be added, enabling interactive figures (see html widgets).


For instance, it is very easy to convert a ggplot2 plot to an interactive plotly plot:

library(ggplot2); library(plotly); library(pkrf)
p <- ggplot(warming) +
     geom_point(aes(x=dt_glob,y=dv_hma, label=gcm, color=rcp)) +
     scale_color_manual(values=mhcolors[1:4]) + 
     geom_smooth(aes(x=dt_glob,y=dv_hma), method='lm') + 
     labs(x='Global temperature rise (K)', y='Glacier mass loss in HMA (%)')
ggplotly()


Plot on next page...

Interactive figures


Dynamic maps with Leaflet

You can also easily make interactive multilayer maps using leaflet.

library(leaflet); library(magrittr); library(rgdal);
library(raster); library(pkrf); library(base64enc);

# calculate data derivatives
glac              <- glaciers
coordinates(glac) <- c(1,2)
glac$percentage   <- glac$mass_debris_ela / glac$mass_total * 100
glacrast          <- rasterFromXYZ(glaciers)
crs(glac) <- crs(glacrast) <- crs("+init=EPSG:4326")
precip <- projectRaster(glacrast$precip, res=10000, crs=crs("+init=EPSG:3857"))

# construct colors palettes
rastpal <- colorBin(palette='Blues', bins=c(seq(0,2000,250),5000), na.color='#00000000')
fillpal <- colorBin(palette = 'Greys', bins=c(seq(0,30,5),100))
linepal <- colorFactor(palette=pkPal('uumain',rand=T), domain=glac$region)

# make some simple base64 encoded figures to embed in popup labels
b64img <- lapply(1:nrow(glaciers), function(i){
  tmpfn <- tempfile('subplot_',fileext='.png')
  png(tmpfn, width=200, height=100, res=100)
  par(mar=c(1.25,6,0.2,0.5), mgp=c(2,0.25,0), las=1, cex.axis=0.7, tck=-0.03)
  barplot(unlist(glaciers[i,11:14]), ylab='', col=mhcolors, horiz=T,
           names.arg=c('Total mass','Mass blw. ELA','Debris-covered','DC blw. ELA'))
  dev.off()
  b64 <- paste0('data:image/png;base64, ',base64encode(tmpfn))
  return(b64)
})

# construct html labels, incl the base64 images
labels <- sprintf('<strong>%s</strong></br>Glaciers: %.0f</br>ELA: %.0f m',
                   glac$region, glac$count, glac$ela) 
labels <- sprintf('%s<br><div style="width:200px"><img src="%s"></div>',labels,b64img)
labels <- lapply(labels, htmltools::HTML)

# call leaflet and add multiple layers of data
leaflet(glac) %>% 
  addProviderTiles(providers$Esri.WorldImagery, option=list(opacity=1.0)) %>%
  addProviderTiles(providers$Esri.WorldPhysical, option=list(opacity=0.6)) %>%
  addRasterImage(precip, color=rastpal, opacity=0.85) %>%
  addCircleMarkers(radius=~sqrt(count)/3, stroke=T, opacity=0.8, fillOpacity=1,
                   weight=2, color=~linepal(region), fillColor=~fillpal(percentage),
                   label=labels, labelOptions=list(textsize='13px')) %>%
  addLegend('topright',pal=rastpal, values=c(0,cellStats(glacrast$precip,max)), opacity=0.85, title = "Precip (mm)") %>%
  addLegend("topright", pal=fillpal, values=~percentage, opacity=0.85, title = "Debris-cover (%)") %>%
  addLegend("topright", colors=mhcolors[2], labels='Region', opacity=1, title = NA)

References

Included references to a bib file will always be put on the final slide.

One can use inline references, for instance one to @Kraaijenbrink2016a, or parenthesized references, such as to this study [@Kraaijenbrink2018a].



kraaijenbrink/pkrf documentation built on July 4, 2023, 10:16 p.m.