Package 'luplot'

Title: Landuse Plot Library
Description: Some useful functions to plot data such as a map plot function for MAgPIE objects.
Authors: Benjamin Leon Bodirsky [aut, cre], Jan Philipp Dietrich [aut], Michael Krause [aut], Miodrag Stevanovic [aut], Florian Humpenoeder [aut], Isabelle Weindl [aut], Lavinia Baumstark [aut], David Klein [aut], Susanne Rolinski [aut], Xiaoxi Wang [aut], David Chen [aut], Pascal Sauer [aut]
Maintainer: Benjamin Bodirsky <[email protected]>
License: LGPL-3 | file LICENSE
Version: 4.0.1
Built: 2024-11-03 04:36:30 UTC
Source: https://github.com/pik-piam/luplot

Help Index


Landuse Plot Library

Description

Some useful functions to plot data such as a map plot function for MAgPIE objects

Details

Package: luplot
Type: Package
Version: 3.11
Date: 2017-02-06
License: LGPL-3
LazyLoad: yes

Author(s)

Benjamin Bodirsky, Jan Philipp Dietrich, Michael Krause, Miodrag Stevanovic, Florian Humpenoeder

Maintainer: Benjamin Bodirsky <[email protected]>

See Also

Useful links:


aggregateForPlot

Description

Aggregates Data for an area chart. The value of all underlying regions/years/timesteps are added to the value of the current region/year/timestep.

Usage

aggregateForPlot(plotdata,
dimension="region",regionPlotorder=NULL)

Arguments

plotdata

An MAgPIE object

dimension

by "region", "name" or "year"

regionPlotorder

allows to change the order of the regions by giving a vector with the order

Value

A MAgPIE object of the same dimensions as plotdata

Author(s)

Benjamin Bodirsky

Examples

## Not run: 
data(population_magpie)
#no aggregation
scratch_plot(population_magpie[,,2])
#aggregation
scratch_plot(aggregateForPlot(population_magpie[,,2],regionPlotorder=c(10,1:9)))

## End(Not run)

as.ggplot

Description

Converts a MAgPIE object or a list of MAgPIE objects into a dataframe usable for ggplot

Usage

as.ggplot(x,scenario="default",asDate=T,rev=F,useDimNames=FALSE)

Arguments

x

MAgPIE object or list of MAgPIE objects. For a list of MAgPIE objects the name of the list entry (has to be character) is used as scenario name.

scenario

Name of the scenario (only used if x is not a list) or position of the entry in the third dimension that contains the scenario name (only if the scenario name is stored in the third dimension) or NULL (automatic detection)

asDate

Format the years as date (TRUE) or keep as is (FALSE); deprecated (only kept for compatibility with older scripts)

rev

reverse legend order (TRUE) or not (FALSE)

useDimNames

Use dim names of 3rd dimension instead Data1, Data2, Data3, Data4; works only if x is a MAgPIE object (no list) and scenario=NULL

Value

Dataframe usable for ggplot

Author(s)

Florian Humpenoeder, David Klein

Examples

## Not run: 
as.ggplot(croparea(gdx))
## End(Not run)
## Not run: 
prices <- list()
prices[["Scenario1"]] <- prices("Scenario1.gdx",crops=c("tece","maiz"))
prices[["Scenario2"]] <- prices("Scenario2.gdx",crops=c("tece","maiz"))
as.ggplot(prices)
## End(Not run)

deco_plot

Description

Plot for decomposition analysis. Use function deco in library magpie4 to create the necessary object.

Usage

deco_plot(x, color = gray.colors(6))

Arguments

x

magpie object with decomposition data (e.g. from the function magpie4:::deco)

color

color vector

Value

a ggplot object

Author(s)

Benjamin Leon Bodirsky, Ina Neher


gginput

Description

Converts a MAgPIE object into a dataframe usable for ggplot

Usage

gginput(data, scatter=NULL, mappings=NULL, na.rm=FALSE, verbose=TRUE)

Arguments

data

MAgPIE object which provides the data for ggplot

scatter

In the case you want to make a scatterplot you have to specify here which data dimension of the MAgPIE object should be used for the scatter plot (name of the dimension)

mappings

A list of mappings used to create combinations of columns which then can be used for aesthetics in ggplot

na.rm

Boolean which decides whether NA values should be removed from the data.

verbose

Boolean which decides whether info messages should be printed to screen or not.

Value

Dataframe usable for ggplot

Author(s)

Jan Philipp Dietrich

Examples

## Not run: 
library(ggplot2)
data(population_magpie)
ggplot(gginput(population_magpie,scatter="scenario"),aes(x=.value.A2,y=.value.B1,color=i)) 
+ geom_point() + geom_smooth(method="loess") + facet_wrap(~.spat1)

## End(Not run)

magpie2ggplot2

Description

Function for plotting MAgPIE objects with ggplot2

Usage

magpie2ggplot2(data,scenario="default",ylab="Value",title=NULL,
xaxis="Year",yaxis="Value",facet_x="Region", facet_y=NULL,
geom="line",stack=F,color="Scenario",fill="Region",
shape=NULL,linetype=NULL,alpha=NULL,labs=NULL,stat=NULL,
text_size=11,hline=NULL,legend_position="right",
scales="fixed",ncol=5,xlim=NULL,ylim=NULL,file=NULL,
scale=NULL, breaks_x=waiver(),breaks_y=waiver(),
xaxis_angle=90,xlab="Year",linewidth=1,pointwidth=2,
color_pal=NULL,bar_width=NULL,show_grid=FALSE,
group="Region",axis_text_col="black",zoom=FALSE,
space="fixed",legend_nrow=NULL,legend_ncol=NULL,
facet_style="default",na.rm=FALSE,
stack_share=FALSE,point_position="identity",
normalize=NULL,...)

Arguments

data

MAgPIE object or list of MAgPIE objects or dataframe of MAgPIE object(s); the name of the list entries is used as scenario name in the legend

scenario

If the scenario name is stored in the third dimension: position of the entry in the third dimension that contains the scenario name. Default: "default"

ylab

y-axis text

title

title appering at the top of the plot

xaxis

x-axis of the plot, default: "Year"

yaxis

y-axis of the plot, default: "Value"

facet_x

x-axis facet, default: "Region"

facet_y

y-axis facet, default: NULL

geom

"line","point","bar", or "area". If "none" only the ggplot base object is returned.

stack

stacked bar plot (TRUE) or not (FALSE)

color

Dimension to be colored, default: "Scenario"

fill

Dimension to be filled in stacked plots, default: NULL

shape

Dimension to be shaped, default: NULL

linetype

Dimension to differ in linetypes, default: NULL

alpha

Dimension to be transparent in stacked plots, default: NULL

labs

vector with legend titles for color,fill,shape,alpha,linetype

stat

adds statistics (e.g. "sum" or "mean"), default: NULL

text_size

text size of axis, legend and title

hline

NULL or MAgPIE objects. Adds a horizontal line.

legend_position

right (default), left, top, bottom or none

scales

fixed (default), free, free_y or free_x; ?facetgrid for details

ncol

Number of columns used in facet_wrap function

xlim

x axis limits; NULL or vector with limits

ylim

y axis limits; NULL or vector with limits

file

File name the output should be written to using ggsave

scale

scaling of ggplot2 object before saving to file

breaks_x

Vector of x-axis breaks, by default ggplot2 takes the decision

breaks_y

Vector of y-axis breaks, by default ggplot2 takes the decision

xaxis_angle

Angle of xaxis text in degree, default=90

xlab

x-axis text

linewidth

linewidth, default=1

pointwidth

pointwidth, default=3

color_pal

vector of colors defining the color palette, if NULL colors are chosen automaticly

bar_width

width of bars in geom bar, default value: NULL

show_grid

show minor and major grid lines; FALSE (default) or TRUE

group

dimension used for grouping; default value: "Region"

axis_text_col

color of axis text and ticks; default value: "black"

zoom

TRUE zooms the plot according to xlim and ylim, FALSE omits values not the range of xlim and ylim

space

fixed (default), free, free_y or free_x; ?facetgrid for details

legend_nrow

Number of rows used in legend

legend_ncol

Number of columns used in legend

facet_style

style of facets, default or paper

na.rm

Boolean deciding whether NA values should be filtered out of the data or not.

stack_share

stacked bar plot showing shares (TRUE) or absolut values (FALSE)

point_position

position of points; "identity" or position_dodge(width = 1)

normalize

NULL (default) or year which should be used to normalize the data

...

Further options passed to as.ggplot

Value

ggplot2 object representing one or more MAgPIE objects.

Author(s)

Florian Humpenoeder, David Klein

Examples

## Not run: 
crop_area_reg <- list()
crop_area_reg[["scenario_name"]] <- croparea("fulldata.gdx")
magpie2ggplot2(crop_area_reg,ylab="10^6 ha",title="Croparea",stack=T,facet_x="Scenario",color=NULL)
## End(Not run)
## Not run: 
prices <- list()
prices[["Scenario1"]] <- prices("Scenario1.gdx",crops=c("tece","maiz"))
prices[["Scenario2"]] <- prices("Scenario2.gdx",crops=c("tece","maiz"))
magpie2ggplot2(prices,ylab="US$/ton DM",title="Agricultural prices")
## End(Not run)

magpieResolution

Description

Returns the Resolution of a MAgPIE object

Usage

magpieResolution(object)

Arguments

object

An MAgPIE object

Value

"glo", "reg" or "cell"

Author(s)

Benjamin Bodirsky


nice_colors

Description

Nice Color Schemes

Usage

nice_colors(style="contrast_area",saturation=1,alpha=1,value=1)

Arguments

style

pre-defined Color-Scheme. "contrast_area" returns partial-complementary colors, with reduced saturation with rising number of colors. This is useful for area-diagramms, where the focus of the eye shall be forced to the bottom of the diagramm.

saturation

saturation (0-1)

alpha

Transparency (Attention, there are problems with certain graphic devices or printers!

value

Brightness. If brightness is part of the color-scheme, value is a reduction factor of existing brightness.

Value

Vector of Hexa-Format of colors.

Author(s)

Benjamin Bodirsky

Examples

## Not run: 
pie(1:12,col=nice_colors())

## End(Not run)

correlationDataSets

Description

Plots 2D density plots to evaluate the relationship between two datasets. It also returns the R-square for the correlation

Usage

plotCorrHist2D(
  x,
  y,
  title = NULL,
  xlab = "x",
  ylab = "y",
  bins = 40,
  limx = NULL,
  limy = NULL,
  folder = NULL,
  file = "",
  breaks = waiver(),
  nrows = 2,
  ncols = 2,
  axisFont = 13,
  axisTitleFont = 13,
  TitleFontSize = 15,
  legendTitleFont = 12,
  legendTextFont = 10,
  statFont = 4,
  table = FALSE,
  stat = TRUE,
  palette = "RdYlBu",
  tag = NULL
)

Arguments

x

First data set (x axis) (observed) as magpie object with one or more items in 3rd dimension

y

Second data set (y axis) (predicted) as magpie object with one or more items in 3rd dimension

title

title of plot

xlab

x axis title

ylab

y axis title

bins

number of bins in histogram

limx

limits y axis (if NULL, fitting limits are calculated within the function)

limy

limits x axis (if NULL, fitting limits are calculated within the function)

folder

path in which to save the plots. if "." is used, it is saved in the current working directory. If NULL, no plots are saved.

file

name of file

breaks

breaks of the legend. It can be a vector, waiver()=the ones from the transformation, NULL for no breaks

nrows

number of rows in pdf file where plots are printed

ncols

number of columns in pdf file where plots are printed

axisFont

Font size of text of axis of the correlation plot

axisTitleFont

Font size of title of axis of the correlation plot

TitleFontSize

Font size of title of correlation plot

legendTitleFont

Font size of the title of the legend

legendTextFont

Font size of legend

statFont

font size r2-MAE label

table

Conditional to include table with statistics in the output. TRUE (includes it), FALSE (it doesn't)

stat

Conditional to include R2 and MAE on the fiure. TRUE (includes it), FALSE (it doesn't)

palette

palette selection for heatd maps based on the RColorBrewer library

tag

for multiple items in the second and third dimensions of the magpie object, should the title include "year", "item", "year-item".

Author(s)

Edna Molina Bacca

Examples

## Not run: 
x <- plotCorrHist2D(x, y, folder = ".")

## End(Not run)

Function to plot maps

Description

Plots maps of map objects or MAgPIE objects

Usage

plotcountrymap(x, hatching=FALSE, ...)

Arguments

x

a magpie object with ISO3 country names, one year and one name dimension

hatching

if hatching is activated, the second name column will be used for hatching (stripes)

...

Further attributes which are passed on to the function mapCountryData of the package rworldmap

Details

This function is an easy application of the mapCountryData function for ISO3 country-level magpie objects

Author(s)

Benjamin Leon Bodirsky

See Also

plotmap2

Examples

## Not run: 
data(population_magpie)
test<-population_magpie
dimnames(test)[[1]]<-c("AFG","DEU","FRA","EGY","IND","IDN","RUS","CHN","USA","YEM")
plotcountrymap(test[,"y2005",1])

## End(Not run)

Function to plot maps using ggplot2

Description

Plots maps MAgPIE objects

Usage

plotmap2(data,file=NULL,title="World map",legend_range=NULL,
legendname="Cell share",lowcol="grey95",midcol="orange",
highcol="darkred",midpoint=0.5, facet_grid="Year~Data1", nrow=NULL, ncol=NULL,
scale=2,breaks=TRUE,labs=TRUE,borders=TRUE,MAgPIE_regions=FALSE,
axis_text_col="black",legend_discrete=FALSE,legend_breaks=NULL,
show_percent=FALSE, sea=TRUE,land_colour="white",
legend_height=2,legend_width=NULL,text_size=12,
legend_position="right",facet_style="default",plot_height=10,plot_width=20)

Arguments

data

MAgPIE object

file

File name the output should be written to using ggsave

title

Title of the map

legend_range

Legend range. Vector with minimum and maximum.

legendname

Name of the legend

lowcol

color for low values

midcol

color for medium values

highcol

color for high values

midpoint

Value indicating where the transformation from lowcol to highcol takes place.

facet_grid

facets formula used in ggplot2 with default "Year~Data1". Falls back to facet_wrap for "Data1".

nrow

number rows for facet_wrap

ncol

number columns for facet_wrap

scale

scaling of ggplot2 object before saving to file

breaks

TRUE or FALSE

labs

TRUE or FALSE

borders

TRUE or FALSE

MAgPIE_regions

TRUE or FALSE, if TRUE data is ignored

axis_text_col

color of axis text and ticks; default value: "black"

legend_discrete

TRUE or FALSE, discrete or continous legend

legend_breaks

NULL (automatically) or vector with breaks

show_percent

TRUE or FALSE, percent values for discrete lengeds

sea

TRUE or FALSE, blue sea or just white background

land_colour

Background colour for all land area. Defaults to white.

legend_height

NULL or legend height in cm

legend_width

NULL or legend width in cm

text_size

text size of axis, legend and title

legend_position

right (default), left, top, bottom or none

facet_style

style of facets, default or paper

plot_height

plot height in cm

plot_width

plot width in cm

Details

Makes use of ggplot2 map plotting capabilites

Author(s)

Florian Humpenoeder, David M Chen

Examples

# plotmap2(data)
#' #@importFrom  RColorBrewer

Function to plot maps

Description

This function plots a world region map directly from .csv files

Usage

plotRegionMapping(file, col = NULL, ...)

Arguments

file

a .csv file

col

color

...

Further attributes such as color palette and map name

Author(s)

Jan Philipp Dietrich, Ewerton Araujo

See Also

plotcountrymap,plotmap2

Examples

## Not run: 
plotRegionMapping("../regionmapping.csv", col = NULL, mapTitle = "")

## End(Not run)

plotregionscluster

Description

Plots world map showing world regions and cluster.

Usage

plotregionscluster(x, cells = "magpiecell")

Arguments

x

data frame with 59199 or 67420 cells in MAgPIE order that contains regional and cluster information. For 59199 cells: format of cluster dimension region.cluster where region is the name of the region and cluster is the number of the cluster. For 67420 cells: cell dimension with coordinate and country name information and format of cluster dimension region.cluster

cells

"magpiecell" for 59199 cells and "lpjcell" for 67420 cells

Examples

x <- paste(rep(paste0("REG", 1:5), each = 12000), rep(1:25, each = 2400), sep = ".")[1:59199]
plotregionscluster(x)

qualityMeasure

Description

Function that returns several efficiency indices of model performance wrt observed data

Usage

qualityMeasure(pd, od, wt, measures=c("Willmott",
"Nash Sutcliffe"),p_value=FALSE)

Arguments

pd

Predicted data. Format required: numeric vector.

od

Observed data. Must be the same format and length as pd.

wt

A vector of weights. The same length as pd and od. Missing weights are interprated as all weights equal to 1.

measures

A vector of supported statistical measures. Currently available are: "Willmott", "Willmott refined", "Nash Sutcliffe", "RMSE", "MAE", "Pearson","Kendall","Spearman".

p_value

p_value=TRUE,reports significance level of Pearson, Kendall and Spearmann coefficients.

Value

Returns a numeric vector with calculated measures. Names of vector elements are the measures applied.

Author(s)

Misko Stevanovic,Xiaoxi Wang

Examples

## Not run: 
od <- rnorm(1000)
pd <- rnorm(1000)

qualityMeasure(pd,od)
qualityMeasure(pd,od,measures=c("RMSE","Pearson","Willmott refined"))

## End(Not run)

Creates bar plot to display given colors

Description

For a quick translation of hexadecimal color codes into visible colors this function simply creates a bar plot with the hexadecimal colors specified by the user. It takes a vector of strings containing the hexadecimal color codes, e.g. "#D0DD1E".

Usage

showcolors(...)

Arguments

...

One ore more strings or a vector of strings containing hexadecimal color codes.

Author(s)

David Klein

Examples

## Not run: showcolors("#D0DD1E")
## Not run: showcolors("#beefee")
## Not run: showcolors(c("#beefee","#D0DD1E"),"#FFFFFF")

swoutput

Description

Creates LaTeX code for plot (magpie2ggplot2) and data table (print.xtable) based on MAgPIE objects and adds it to a "swStream" object.

Usage

swoutput(stream,data,unit="unit",plot=TRUE,table=TRUE,scenarioName="default",
textSize=16,digits=0,plotLevel=NULL,...)

Arguments

stream

The swStream object to be modified

data

MAgPIE object or list of MAgPIE objects. For a list of MAgPIE objects the name of the list entry (has to be character) is used as scenario name.

unit

unit of data (Character). Used as ylab in magpie2ggplot2 and table caption in print.xtable

plot

TRUE or FALSE. If TRUE, LaTeX code for plot (magpie2ggplot2) is added

table

TRUE or FALSE. If TRUE, LaTeX code for data table (print.xtable) is added

scenarioName

Name of the scenario. Only used if data is not a list.

textSize

Size of text used in magpie2ggplot2

digits

Number of digits for rounding. Used in xtable

plotLevel

NULL for all (default), reg or glo

...

Further options passed to magpie2ggplot2 and print.xtable

Details

Creates preformatted plots and data tables based on MAgPIE objects. Format is optimized for readability. Data tables with more than 12 columns are rescaled for fitting on portrait pages.

Value

No return value

Author(s)

Florian Humpenoeder

See Also

"swStream",swopen,swclose,swR,swtable,swfigure

Examples

## Not run: 
sw<-swopen("croparea_reg.pdf")
swlatex(sw,"\newpage")
swlatex(sw,"\section{Croparea - regional}")
swoutput(sw,croparea(gdx),"mio. ha",scenario="test")
swclose(sw)
## End(Not run)

whereplot

Description

Plots the results of a logical question on a mpa

Usage

whereplot(x)

Arguments

x

A logical statement with a magpie object on country resolution

Value

A map. Green is true, red is false, orange is true and false in the same country, purple is NA, and striped indicates a country that includes some NAs.

Author(s)

Benjamin Leon Bodirsky

Examples

## Not run: 
data(population_magpie)
 test<-population_magpie
 dimnames(test)[[1]]<-c("AFG","DEU","FRA","EGY","IND","IDN","RUS","CHN","USA","YEM")
 whereplot(test>500)
 
## End(Not run)

Water area map

Description

A map containing the water area (only used internally by plotmap)


World countries' boundaries

Description

A shapefile dataframe containing up to date countriy borders (only used internally by plotmap). The file is from the data provided in the maptools package

Author(s)

Miodrag Stevanovic


World countries' boundaries and MAgPIE mapping

Description

A dataframe containing up to date country borders and MAgPIE region mapping (only used internally by plotmap2). The file is based on the wrld_simpl dataset

Author(s)

Miodrag Stevanovic, Florian Humpenoeder