Intro to the caroline package

library(caroline)

Intro to the caroline package

# This code used to generate the test data. 

country.lu <- read.csv(system.file("extdata",'forest',"GFW-sub.nat-prim.loss.drivers-countries.csv", package="caroline")) 
drivers.lu <- read.csv(system.file("extdata",'forest',"GFW-sub.nat-prim.loss.drivers-drivers.csv", package="caroline")) 
subnats.lu <- read.csv(system.file("extdata",'forest',"GFW-sub.nat-prim.loss.drivers-subnationals.csv", package="caroline")) 

treeloss.sn <- nerge(all.x=T, by.x='ids',## demo of the 'nerge()' and 'nv()' caroline package R functions
  l=list(tl=treeloss.sn.f, country=nv(country.lu),subnational=nv(subnats.lu), driver=nv(drivers.lu))) 


plot.sparge(d$P)

save(treeloss, file="data/treeloss.RData")

The caroline package includes functions for data cleaning, transforming, and merging as well as for creating confounding-untangling plots: such as the sparge plot (an alternative to violins, raincloud, bean, or sina plots). The treeloss data.frame included with the package has results from the Global Forest W. Take a look at the data:

str(treeloss)
head(treeloss)
tail(treeloss)

merging tables together and converting them

#as.data.frame(table())

Creating sparge plots

Let's make a basic sparge plot.

plot.sparge(treeloss, "loss_ha_log ~ country")

We can also pass graphical parameters. Let's add a title (main=), increase the y-axis limit (ylim=), reduce the point size to 60% (cex=), and reduce the font size of the axis labels to 90% (cex.axis=). We can also change the colors (col=), and many other options passed to "boxplot.argument" or "legend.argument" for example.

plot.sparge(treeloss, main="Sparge Plot", ylim=c(0,10), cex=0.6, cex.axis=0.9, col=c("blue4", "orange3"), boxplot.notch=TRUE, legend.)

Now, let's drill down further and simultaneously look at another variable (in a nested sparge plot):

plot.sparge(treeloss, "loss_ha_log ~ continent | years")

We can also do some cross tabulations of this data to discover which countries have the highest total deforestation over the past several decades.

crosstabulate(treeloss)

A few notes on creating data frames for input to formula based plotting functions:

The essential input components of a sparge plot are x=(the data) f(=some way of specifying where the factors are in the data) and cat.names(=the names/order you want to use for the levels of said factor (a categorical predictor variable) The easiest way to get the most of a sparge plot, the caroline package, and R in general, is to utlize the combination of the data.frame format (for input data) and formulas (for communicating to the software what kinds of relationships between variables you're trying to investigate). It's worth reading the help pages for these two core R functions: use "?data.frame" OR "help(data.frame)" and "?formula" OR "help(formula)". *The nerge() function is great for merging data.frames and named vectors [caroline:nv()] together, but the (PERL-style) "m()" function can even parse raw text file (or even complicated but well-structured strings within columns of a [.csv] table) into new columns of a dataframe for you!



Try the caroline package in your browser

Any scripts or data that you put into this service are public.

caroline documentation built on July 6, 2026, 9:07 a.m.