I was trying to replicate this chart from The Economist (the one on the left). The chart plots the number of billionaires in Russia on the left y-axis and the number of billionaires in rest of the world on the right.
- Create the chart for Russian billionaires (
p1
). - Create the chart for the others (
p2
). - Combine
p1
andp2
into a dual y-axis chart using the code by Kohske.
Data: (content of billionaire.csv
)
,Russia,World
1996,0,423
1997,4,220
1998,1,221
1999,0,298
2000,0,322
2001,8,530
2002,6,466
2003,17,459
2004,25,562
2005,27,664
2006,33,760
2007,53,893
2008,87,1038
2009,32,761
2010,62,949
2011,101,1109
2012,96,1130
2013,110,1317
2014,111,1535
2015,88,1738
Code:
library(ggplot2)
library(gtable)
library(grid)
library(extrafont) # for Officiana font
dat <- read.csv("billionaire.csv")
rus <- dat[,1:2]
world <- dat[,-2]
grid.newpage()
p1 <- ggplot(rus, aes(X, Russia)) + geom_line(colour = "#68382C", size = 1.5) + ggtitle("Number in Russia") +
ylim(0, 200) + labs(x="",y="") +
theme(#plot.margin = unit(c(2,1,0,0), "cm"),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "gray50", size = 0.5),
panel.grid.major.x = element_blank(),
text=element_text(family="ITCOfficinaSans LT Book"),
axis.text.y = element_text(colour="#68382C", size = 14),
axis.text.x = element_text(size = 14),
axis.ticks = element_line(colour = 'gray50'),
plot.title = element_text(hjust = -0.17, vjust=2.12, colour="#68382C", size = 14, family = "ITCOfficinaSans LT Bold"))
p2 <- ggplot(world, aes(X, World)) + geom_line(colour = "#00a4e6", size = 1.5) + #ggtitle("Rest of world") +
ylim(0, 2000) + labs(x="",y="") +
theme(#plot.margin = unit(c(2,1,0,0), "cm"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
text = element_text(family="ITCOfficinaSans LT Book"),
axis.text.y = element_text(colour="#00a4e6", size=14),
axis.text.x = element_text(size=14),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.2, vjust=2.12, colour="#00a4e6", size = 14, family = "ITCOfficinaSans LT Bold"))
# Combining p1 and p2
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t,
pp$l, pp$b, pp$l)
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
ggsave("plot.pdf",g, width=5, height=5)
To format the texts "Number in Russia" and "Rest of the world" with my chosen font and color, I put them in ggtitle
. But after combining the charts together in step 3 the title of p2
is missing, so this is all I got
What I'm trying to achieve is
1. Add the text "Rest of world" in a color and font family of my choice (not the default Helvetica.)
2. Add the label 1996 on the x-axis.
Any help is appreciated. Thanks!
EDIT: Data set and full code added.
EDIT2: Just FYI, I got all the Officiana fonts from here: http://people.oregonstate.edu/~hanshumw/Specie%20I.D./Signage%20Backup/FONT%20Officina%20full/
EDIT3: Ok I finally how to make it work by fiddling with the plot at the grid level
g$grobs[[8]]$children$GRID.text.526$label <- c("Number in Russia", "Rest of World")
g$grobs[[8]]$children$GRID.text.526$gp$col <- c("#68382C","#00a4e6")
g$grobs[[8]]$children$GRID.text.526$x <- unit(c(-0.175, 0.774), "npc")
Put this chunk before ggsave(...)
, and here's the result:
ggthemes
packages, there is atheme_economist()
. This may not help with your question, but might come in handy to save some typing. – Sundbergtheme_excel
? – Predominantrus
andworld
– Picaresquedput(rus)
&dput(world)
OR the steps to fully reproduce them from the ZIP file you provided. – Alfons