Composite plots: grid.arrange

I really like composite plots, where there’s a top part that describes a phenomenon and a bottom part with a synthetic time view of the overall process.
I’ve recently discovered this beautiful representation of educational differentials by gender, by Sara Lopus and Margaret Frye, and the beauty of this dataviz is that it tells a story on its own. (Click on the link for the publication)

I have used a random generated data to reproduce the graph in ggplot and used grid.arrange from gridExtra package to bind grobs, the top and bottom components.

grid.arrange(top, bottom, heights=c(10,5), widths=c(20), padding=0)

I have saved the map as a .png file png package and used rasterGrob from package grid to create a raster image graphical object.

Screen Shot 2018-08-30 at 11.26.50

Arranging ggplot2 graphs on a page

How to arrange graphs in ggplot2 without the help of the layout matrix

How do you arrange non-simmetric plots in ggplot2?
With the print command:

After installing these two packages: install.packages(“grid”, “ggplot2”), load the  libraries:
library(grid)
library(ggplot2)

The data and code for the three graphs is taken from this website:

# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5), labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1), labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8), labels=c("4cyl","6cyl","8cyl"))

# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
a <- qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
main="Distribution of Gas Milage", xlab="Miles Per Gallon",
ylab="Density")

# Scatterplot of mpg vs. hp for each combination of gears and cylinders
# in each facet, transmittion type is represented by shape and color
b <- qplot(hp, mpg, data=mtcars, shape=am, color=am,
facets=gear~cyl, size=I(3),
xlab="Horsepower", ylab="Miles per Gallon")

c <- qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"),
fill=gear, main="Mileage by Gear Number",
xlab="", ylab="Miles per Gallon")

a, b, and c are our graphs. Here we decide how to place the plots on the plotting surface:

grid.newpage() # Open a new page on grid device
pushViewport(viewport(layout = grid.layout(3, 1))) #this can really be anything... just remember to change accordingly the print commands below
print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1:1))
print(b, vp = viewport(layout.pos.row = 2, layout.pos.col = 1:1))
print(c, vp = viewport(layout.pos.row = 3, layout.pos.col = 1:1))

The layout=grid.layout is the command dividing the plotting surface, in the example I have divided it into three rows and one column, hence the layout.pos.row = 1, 2, 3 and the layout.pos.row = 1:1 equal for all three plots.

image-29-09-2016-at-16-07

What if I need something asymmetrical? For instance two small plots on one column and one taking up more space… The reasoning is very similar to that of the layout matrix: divide the space into 4 squares grid.layout(2, 2) and then plot the third graph over two rows layout.pos.row=1:2

grid.newpage() # Open a new page on grid device
pushViewport(viewport(layout = grid.layout(2, 2))) #this can really be anything... just remember to change accordingly the print commands below
print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1:1))
print(b, vp = viewport(layout.pos.row = 2, layout.pos.col = 1:1))
print(c, vp = viewport(layout.pos.row = 1:2, layout.pos.col = 2:2))

image-29-09-2016-at-16-18

Pyramid-like bar chart for climate change barriers

I was scrolling through the Independent and got hooked on a graph displaying the percentage of people’s concerns regarding climate change by country, and was extremely surprised by the results. UK and US lag far behind countries including China in wanting their governments to pursue a meaningful commitment to successfully address climate change.

newplot

library(ggplot2)
library(grid)
library(plyr)

dta<-
structure(list(country = structure(c(15L, 3L, 4L, 5L, 14L, 6L,
10L, 12L, 1L, 2L, 7L, 8L, 9L, 11L, 13L, 15L, 3L, 4L, 5L, 14L,
6L, 10L, 12L, 1L, 2L, 7L, 8L, 9L, 11L, 13L), .Label = c("Australia",
"China", "Denmark", "Finland", "France", "Germany", "Hong Kong",
"Indonesia", "Malaysia", "Norway", "Singapore", "Sweden", "Thailand",
"UK", "US"), class = "factor"), issue = c("Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage who think climate \nchange is 'not a serious problem' ",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change",
"Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change"
), perc = c(32L, 14L, 23L, 10L, 26L, 11L, 22L, 18L, 11L, 4L,
5L, 3L, 2L, 5L, 6L, -17L, -4L, -8L, -3L, -7L, -4L, -10L, -8L,
-3L, -1L, -1L, -1L, -1L, -1L, -1L)), .Names = c("country", "issue",
"perc"), row.names = c(NA, -30L), class = "data.frame")

p <- ggplot(dta, aes(reorder(country,perc),perc,fill=issue)) +
geom_bar(subset = .(issue == "Percentage who think climate \nchange is 'not a serious problem' "), stat = "identity",colour="black",alpha=0.5) +
annotate("text",x = 16.5, y = -12,label=dta$issue[16], fontface="bold")+
geom_bar(subset = .(issue == "Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change"),colour="black", stat = "identity",alpha=0.5) +
annotate("text",x = 16.5, y = 15,label=dta$issue[1], fontface="bold")+
scale_fill_manual(values = c("#F7320B", "#2BC931"))+
geom_text(subset = .(issue == "Percentage who think climate \nchange is 'not a serious problem' "),
aes(label=perc.a), position="dodge", hjust=-.35)+
geom_text(subset = .(issue == "Percentage that want their country's strategy not to agree \nto any international agreement that addresses climate change"),colour="black", stat = "identity",aes(label=perc.b), position="dodge", hjust=2)+
coord_flip() +
xlab("")+
ylab("")+
scale_x_discrete(expand=c(0.2,0.55))+
scale_y_continuous(limits=c(-22,32),
breaks = c(-17,-10,0,10,32),
labels = paste0(as.character(c(17,10,0,10,32), "%")))+
theme(axis.text.y  = element_text(size=13,hjust=1),
axis.text = element_text(colour = "black"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.background =element_rect("white"),
legend.position="none",
strip.background = element_rect(fill = "white", colour = "white"),
strip.text.x = element_text(size = 13))

ggsave("newplot.pdf",p,scale=2)

Moran plots in ggplot2

Moran plots are one of the many way to depict spatial autocorrelation:
moran.test(varofint,listw)
where “varofint” is the variable we are studying, “listw” a listwise neighbourhood matrix, and the function “moran.test” performs the Moran’s test (duh!) for spatial autocorrelation and is included in the spdep funtionality. The same plot can be done using ggplo2 library. Provided that we already have our listwise matrix of neighborhood relationships listw, we first define the variable and the lagged variable under study, computing their mean and saving them into a data frame (there are a lot of datasets you can find implemented in R: afcon, columbus, syracuse, just to cite a few). The purpose is to obtain something that looks like this (I have used my own *large* set of Spanish data to obtain it):

ggplot2.moranplot1

Upload your data. Here is Anselin (1995) data on African conflicts, afcon:

data(afcon)
varofint listw varlag var.name <- "Total Conflicts"
m.varofint m.varlag
and compute the local Moran's statistic using localmoran:

lisa
and save everything into a dataframe:
df

use these variables to derive the four sectors "High-High"(red), "Low-Low"(blue), "Low-High"(lightblue), "High-Low"(pink):
df$sector significance vec =df$m.varofint & df$varlag>=df$m.varlag]  df$sector[df$varofint<df$m.varofint & df$varlag<df$m.varlag]  df$sector[df$varofint<df$m.varofint & df$varlag>=df$m.varlag]  =df$m.varofint & df$varlag<df$m.varlag]

df$sec.data

df$sector.col[df$sec.data==1] <- "red"
df$sector.col[df$sec.data==2] <- "blue"
df$sector.col[df$sec.data==3] <- "lightblue"
df$sector.col[df$sec.data==4] <- "pink"
df$sector.col[df$sec.data==0] <- "white"

df$sizevar df$sizevar 0.1)
df$FILL df$BORDER
to get the ggplot graph:
p 0.05", "High-High", "Low-Low","Low-High","High-Low"))+
scale_x_continuous(name=var.name)+
scale_y_continuous(name=paste("Lagged",var.name))+
theme(axis.line=element_line(color="black"),
axis.title.x=element_text(size=20,face="bold",vjust=0.1),
axis.title.y=element_text(size=20,face="bold",vjust=0.1),
axis.text= element_text(colour="black", size=20, angle=0,face = "plain"),
plot.margin=unit(c(0,1.5,0.5,2),"lines"),
panel.background=element_rect(fill="white",colour="black"),
panel.grid=element_line(colour="grey"),
axis.text.x  = element_text(hjust=.5, vjust=.5),
axis.text.y  = element_text(hjust=1, vjust=1),
strip.text.x  = element_text(size = 20, colour ="black", angle = 0),
plot.title= element_text(size=20))+
stat_smooth(method="lm",se=F,colour="black", size=1)+
geom_vline(xintercept=m.varofint,colour="black",linetype="longdash")+
geom_hline(yintercept=m.varlag,colour="black",linetype="longdash")+
theme(legend.background =element_rect("white"))+
theme(legend.key=element_rect("white",colour="white"),
legend.text =element_text(size=20))

Check out the interactive shiny version on pracademic