Learn R Programming

MaddisonData (version 1.0.2)

ggplotPath: ggplot paths

Description

ggplotPath plots y vs. x (typically year) with a separate line for each group with options for legend placement, vertical lines and labels.

Usage

ggplotPath(
  x = "year",
  y,
  group,
  data,
  scaley = 1,
  logy = TRUE,
  legend.position,
  vlines,
  labels
)

Value

an object of class ggplot2::ggplot, which can be subsequently edited, and whose print method produces the desired plot.

Arguments

x

name of column in data to pass as x in aes(x=.data[[x]], ...); default = year.

y

name of column in data to pass as y in aes(y=.data[[y]], ...); must be supplied.

group

name of grouping variable, i.e., plot a separate line for each level of group using aes(group=.data[[group]], ...), unless group is missing or length(unique(data[, group])) = 1.

data

data.frame or tibble::tibble with columns x, y, and group.

scaley

factor to divide y by for plotting. Default = 1, but for data in monetary terms, e.g., for MaddisonData, y = 'gdppc' is Gross domestic product (GDP) per capita in 2011 dollars at purchasing power parity (PPP), for which we typically want scaley = 1000.

logy

logical: if TRUE, y axis is on a log scale; default = TRUE.`

legend.position

argument passed to ggplot2::theme. Default depends on nGps <- length(unique(data[, group]: If nGps = 1, there is no legend. If nGps > 10, legend.position = 'right'. In between, legend.position = c(.15, .5) = center left. For alternatives, see ggplot2::theme.

vlines

= locations on the x axis for vertical lines using ggplot2::geom_vline(aes(xintercept = .data[[x]]), data=vlines, ...) with color='grey', lty='dotted' unless color or colour and / or lty are available as attr(x, ...).

labels

= data.frame with columns x, y, label, srt, col, where x, y, and srt are numeric, label is character, and col are acceptable values for color in with(labels, annotate('text', x=x, y=y, label = label, srt=srt, color=col)).

Examples

Run this code
str(GBR_USA <- subset(MaddisonData::MaddisonData, ISO %in% c('GBR', 'USA')))
GBR_USA1 <- ggplotPath('year', 'gdppc', 'ISO', GBR_USA, 1000)

GBR_USA1+ggplot2::coord_cartesian(xlim=c(1500, 1850)) # for only 1500-1850 
GBR_USA1+ggplot2::coord_cartesian(xlim=c(1600, 1700), ylim=c(7, 17)) 

# label the lines
ISOll <- data.frame(x=c(1500, 1750), y=c(1.4, .7), label=c('GBR', 'USA') )
(GBR_USA2 <- ggplotPath('year', 'gdppc', 'ISO', GBR_USA, 1000, 
                        labels=ISOll) ) 
# vlines 

Vlines = c(1849, 1929, 1933, 1939, 1945)
(GBR_USA3 <- ggplotPath('year', 'gdppc', 'ISO', GBR_USA, 1000, 
                vlines=Vlines, labels=ISOll) ) 

Run the code above in your browser using DataLab