Skip to content

Instantly share code, notes, and snippets.

@expersso
Last active May 27, 2019 22:42
Show Gist options
  • Save expersso/78407461c064a1a30b70 to your computer and use it in GitHub Desktop.
Save expersso/78407461c064a1a30b70 to your computer and use it in GitHub Desktop.
Example of using ggplot2's %+% to follow DRY principle
library(eurostat)
library(dplyr)
library(ggplot2)
library(scales)
#### Original version (not using %+%) ####
# Originally posted at http://www.r-bloggers.com/european-debt-and-interest/ on June 7, 2015
r1 <- get_eurostat('gov_10dd_edpt1')
# add country names
r2 <- get_eurostat_dic('geo') %>%
mutate(.,
geo=V1,
country=V2,
country=gsub('\\(.*$','',country)) %>%
select(.,geo,country) %>%
merge(.,r1) %>%
# filter countries
filter(.,
!grepl('EA.*',geo),
!grepl('EU.*',geo),
geo!='NO')
filter(r2,
sector=='S13', # general government
na_item=='B9', # Net lending (+) /net borrowing (-)
unit=='PC_GDP' # % GDP
) %>%
ggplot(.,aes(x=time,y=values)) +
geom_line()+
ylab('% GDP')+
facet_wrap(~country,nrow=4) +
ggtitle('Net lending (+) /net borrowing (-)') +
xlab('Year') +
geom_hline(yintercept=-3,colour='red') +
scale_x_date(
breaks=c(as.Date("2000-01-01"),as.Date("2010-01-01") )
,labels = date_format("%Y"))
#########
filter(r2,
sector=='S13', # general government
na_item=='GD', # Gross debt
unit=='PC_GDP' # % GDP
) %>%
ggplot(.,aes(x=time,y=values)) +
geom_line()+
ylab('% GDP')+
facet_wrap(~country,nrow=4) +
ggtitle('Gross Debt') +
xlab('Year') +
geom_hline(yintercept=60,colour='red') +
scale_x_date(
breaks=c(as.Date("2000-01-01"),as.Date("2010-01-01") )
,labels = date_format("%Y"))
#########
filter(r2,
sector=='S13', # general government
na_item=='D41PAY', # Interest, payable
unit=='PC_GDP' # % GDP
) %>%
ggplot(.,aes(x=time,y=values)) +
geom_line()+
ylab('% GDP')+
facet_wrap(~country,nrow=4) +
ggtitle('Interest, payable') +
xlab('Year') +
scale_x_date(
breaks=c(as.Date("2000-01-01"),as.Date("2010-01-01") )
,labels = date_format("%Y"))
#### My version (using %+%) ####
r1 <- get_eurostat('gov_10dd_edpt1')
# add country names
r2 <- get_eurostat_dic("geo") %>%
mutate(geo = V1,
country = V2,
country = str_replace(country, "\\(.*$","")) %>%
select(geo, country) %>%
inner_join(r1) %>%
filter(!str_detect(geo, "[0-9]$"),
geo != "NO",
sector == "S13",
unit == "PC_GDP") %>%
tbl_df()
p_base <- r2 %>%
ggplot(aes(x = time, y = values)) +
geom_line() +
facet_wrap(~country, nrow = 4) +
scale_x_date(breaks = as.Date(c("2000-01-01", "2010-01-01")),
labels = date_format("%Y")) +
labs(x = "Year", y = "% GDP")
p1 <- p_base %+% filter(r2, na_item == "B9") +
geom_hline(yintercept = -3, colour = "red") +
ggtitle("Net lending (+) /net borrowing (-)")
p2 <- p_base %+% filter(r2, na_item == "GD") +
geom_hline(yintercept = 60, colour = "red") +
ggtitle("Gross Debt")
p3 <- p_base %+% filter(r2, na_item == "D41PAY") +
ggtitle("Interest, payable")
#### Using function instead of %+% ####
make_plot <- function(df = r2, plot_var, title) {
df %>%
filter(na_item == plot_var) %>%
ggplot(aes(x = time, y = values)) +
geom_line() +
facet_wrap(~country, nrow = 4) +
scale_x_date(breaks = as.Date(c("2000-01-01", "2010-01-01")),
labels = date_format("%Y")) +
labs(x = "Year", y = "% GDP", title = title)
}
p1 <- make_plot(plot_var = "B9", title = "Net lending (+) /net borrowing (-)") +
geom_hline(yintercept = -3, colour = "red")
p2 <- make_plot(plot_var = "GD", title = "Gross Debt") +
geom_hline(yintercept = 60, colour = "red")
p3 <- make_plot(plot_var = "D41PAY", title = "Interest, payable")
#### Benchmarking ####
library(microbenchmark)
microbenchmark(
p3 <- p_base %+% filter(r2, na_item == "D41PAY") +
ggtitle("Interest, payable"),
p3 <- make_plot(plot_var = "D41PAY", title = "Interest, payable")
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# %+% 3.911188 4.076719 4.241338 4.147733 4.215931 12.39172 100
# make_plot 7.997508 8.245144 8.754945 8.444444 8.579021 16.44030 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment