## A space-time box plot of Spain’s TFR for 910 comarcas.

The idea behind spatial analysis is that space matters and near things are more similar: a variable measured in city A is (ideally) different from the same variable measured in city B. A simple way to get a feeling and to represent this hypothesis is through graphical visualization, usually a map(s).

However, when dealing with time series maps are cumbersome and  with sometimes some information is lost, such as the national average or path convergence. Box plots are a simple yet very effective way to synthesize a lot of information in one graph. The following plot depicts TFR over a 30 years period for 910 Spanish areas with respect to the national average value (thick black line in the middle of the boxes).
``` p <- ggplot(dat, aes(x=factor(YEAR), y=dat\$TFR)) p <- p + geom_boxplot() p <- p + scale_y_continuous(limits=c(0,2.5)) + scale_x_discrete("YEAR", breaks=seq(1981,2011,by=5))```

## 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):

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