Skip to content

Instantly share code, notes, and snippets.

@basilesimon
Last active November 10, 2016 15:54
Show Gist options
  • Save basilesimon/3150a8a5dc4959f589eb1f5dc5fc8cd9 to your computer and use it in GitHub Desktop.
Save basilesimon/3150a8a5dc4959f589eb1f5dc5fc8cd9 to your computer and use it in GitHub Desktop.
UK trade in goods and services balance with selected non-EU countries, 1999 to 2015, in £ millions
Year Canada USA China India Japan
1999 -124 3346 -2041 -228 -3642
2000 -282 5350 -3178 337 -4340
2001 14 4384 -3911 -300 -3297
2002 4 10501 -5352 -755 -1834
2003 238 15778 -6275 -421 -1840
2004 -9 18601 -7030 25 -1300
2005 -95 19260 -9236 -185 -1845
2006 -174 22911 -11597 -747 -1361
2007 -811 21558 -14248 -441 -2988
2008 -898 24242 -17226 -687 -4355
2009 750 21931 -19122 -4002 -1636
2010 212 22899 -22036 -3416 -2781
2011 536 30175 -20379 -2209 -4323
2012 1211 34694 -18968 -2693 -2448
2013 351 40250 -19610 -3742 -517
2014 -2436 31372 -19293 -2025 -401
2015 -951 39318 -22975 -3248 845
Year Total trade balance
1999 -4077
2000 -10664
2001 -9244
2002 -4615
2003 2455
2004 557
2005 815
2006 -6365
2007 -4972
2008 -12972
2009 -5283
2010 -11867
2011 -4979
2012 5591
2013 17797
2014 21923
2015 29968
library(ggplot2)
library(reshape2)
library(scales)
library(grid)
library(timesTheme)
library(extrafont)
library(RColorBrewer)
# read CSV fileso
data <- read.table("data2.csv", header=TRUE, sep=",")
line <- read.table("line.csv", header=TRUE, sep=",")
# metls it by year, our future x axis
df <- melt(data, id.var="Year")
dfline <- melt(line, id.var="Year")
# colour palette
# canada us china india japan
cpalette <- c("#96887a", "#254251", "#dacfc1", "#6c3c5e", "#3292a6")
# set up basic plot
ggplot() +
# stacked bar chart
geom_bar(data=df,
aes(Year, value, fill=variable),
stat="identity",
position="identity") +
# line + points on line
geom_line(data=dfline,
aes(Year, value),
colour="#f37f2f",
linetype=1) +
geom_point(data=dfline,
aes(Year, value), # adds points to line
colour="#f37f2f") +
# axis
scale_x_continuous(breaks=seq(1999,2015,1)) +
scale_y_continuous(breaks=seq(-40000,50000,10000), # breaks every 10,000
limits=c(-40000,50000),
labels = scales::comma) +
# add theme
times_theme() +
theme(plot.title=element_text(family="Times Modern Bold")) +
theme(axis.title.x=element_blank()) + # no label on x axis
theme(axis.title.y=element_blank()) + # no label on y axis
theme(panel.grid.major.x = element_blank()) + # no vertical grids
scale_fill_manual(values=cpalette) +
theme(legend.position="top",
legend.key.size=unit(.02, "npc"),
legend.title=element_blank()) +
theme(plot.margin=unit(c(1,1,1.5,1), "cm")) # widern margins for text at bottom
grid.text("Source: Office for National Statistics", # custom text at bottom
x=unit(.1, "npc"), y=unit(.04, "npc"),
just=c("left", "bottom"),
gp=gpar(fontfamily="Gill Sans", fontsize=8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment