Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to reproduce the result of my post -- https://ikashnitsky.github.io/2017/ggplot2-microbenchmark/
################################################################################
#
# ikashnitsky.github.io 2017-07-15
# The speed of ggplot2 using canvas
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#
################################################################################
# load required packages
library(tidyverse) # data manipulation and viz
library(ggthemes) # themes for ggplot2
library(viridis) # the best color palette
library(rgdal) # deal with shapefiles
library(microbenchmark) # measure the speed of executing
library(extrafont) # nice font
myfont <- "Roboto Condensed"
library(RColorBrewer)
################################################################################
# simple plot
canv_mt <- ggplot(mtcars, aes(hp, mpg, color = cyl))+
coord_cartesian()
# test speed with mocrobenchmark
test <- microbenchmark(
without_canvas = ggplot(mtcars, aes(hp, mpg, color = cyl))+
coord_cartesian()+
geom_point()
,
with_canvas = canv_mt+
geom_point()
,
times = 100
)
test
autoplot(test)+
aes(fill = expr)+
scale_fill_viridis(discrete = T)+
theme_bw(base_size = 15, base_family = myfont)+
theme(legend.position = "none",
axis.text = element_text(size = 15))+
labs(title = "The speed of creating a simple ggplot")
################################################################################
# map
# load the already prepared data
load(url("https://ikashnitsky.github.io/doc/misc/map-subplots/df-27-261-urb-rur.RData"))
load(url("https://ikashnitsky.github.io/doc/misc/map-subplots/spatial-27-261.RData"))
# fortify spatial objects
neib <- fortify(Sneighbors)
bord <- fortify(Sborders)
fort <- fortify(Sn2, region = "id")
# join spatial and statistical data
fort_map <- left_join(df, fort, "id")
# pal for the subregions
brbg3 <- brewer.pal(11,"BrBG")[c(8,2,11)]
# create a blank map
basemap <- ggplot()+
geom_polygon(data = neib,
aes(x = long, y = lat, group = group),
fill = "grey90",color = "grey90")+
coord_equal(ylim = c(1350000,5450000),
xlim = c(2500000, 6600000),
expand = c(0,0))+
theme_map(base_family = myfont)+
theme(panel.border = element_rect(color = "black",size = .5,fill = NA),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.background = element_rect(colour = NA, fill = NA),
legend.title = element_text(size = 15),
legend.text = element_text(size = 15))+
labs(x = NULL, y = NULL)
# test speed with mocrobenchmark
test_map <- microbenchmark(
without_canvas =
ggplot()+
geom_polygon(data = neib,
aes(x = long, y = lat, group = group),
fill = "grey90",color = "grey90")+
coord_equal(ylim = c(1350000,5450000),
xlim = c(2500000, 6600000),
expand = c(0,0))+
theme_map(base_family = myfont)+
theme(panel.border = element_rect(color = "black",
size = .5,fill = NA),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.background = element_rect(colour = NA, fill = NA),
legend.title = element_text(size = 15),
legend.text = element_text(size = 15))+
labs(x = NULL, y = NULL) +
geom_polygon(data = fort_map,
aes(x = long, y = lat, group = group,
fill = subregion), color = NA)+
scale_fill_manual(values = rev(brbg3)) +
theme(legend.position = "none")
,
with_canvas =
basemap +
geom_polygon(data = fort_map,
aes(x = long, y = lat, group = group,
fill = subregion), color = NA)+
scale_fill_manual(values = rev(brbg3)) +
theme(legend.position = "none")
,
times = 100
)
autoplot(test_map)+
aes(fill = expr)+
scale_fill_viridis(discrete = T)+
theme_bw(base_size = 15, base_family = myfont)+
theme(legend.position = "none",
axis.text = element_text(size = 15))+
labs(title = "The speed of creating a map with ggplot2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.