Last active
January 27, 2019 11:31
-
-
Save ottlngr/9681ca3b7b34901e6b745b6da32fa3e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(LexisPlotR) | |
# Create Lexis grid | |
l <- lexis.grid(year.start = 2014, year.end = 2017, age.start = 33, age.end = 37) | |
# Create a data.frame that contains the labels | |
labels <- data.frame( | |
x = c( | |
as.Date("2014-05-01"), | |
as.Date("2015-06-15"), | |
as.Date("2015-06-15"), | |
as.Date("2015-06-15"), | |
as.Date("2016-01-01") | |
), | |
y = c( | |
33.7, | |
35.9, | |
35.8, | |
35.7, | |
33.5 | |
), | |
label = c( | |
"G: 1980", | |
"D: 0", | |
"I: 26", | |
"U: 19", | |
"S: 814" | |
) | |
) | |
# Use geom_text to plot the labels | |
l + geom_text(aes(x = labels$x, y = labels$y, label = labels$label)) | |
# Alternatively, annotate() does the same without the need for a data.frame | |
l + annotate("text", x = as.Date("2014-05-01"), y = 33.7, label = "G: 1980") | |
# Add custum rectangles to a Lexis grid | |
l <- lexis.grid(year.start = 2012, year.end = 2018, age.start = 68, age.end = 73) | |
xs <- c(as.Date("2015-01-01"), as.Date("2016-12-31"), as.Date("2016-12-31"), as.Date("2015-01-01")) | |
ys <- c(70, 70, 71, 71) | |
polygon <- data.frame(x = xs, y = ys) | |
l + geom_polygon(data = polygon, aes(x = x, y = y), colour = "black", fill = NA, size = 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment