问题
I'm trying to create a visualisation of the UK for different time periods where the fill for each region depends on a numeric variable. When I split the dataframe by time period and create the charts independently in ggplot2, they are correct. However, when I use the facet_wrap function to have them appear in a single chart, the outputs are incorrect (the numbers for each region are not correct).
(Note, I have tried to follow this tutorial for spatial data in R as closely as possible: http://spatial.ly/2013/12/introduction-spatial-data-ggplot2/)
The command I am trying to use is:
Map <- ggplot(data, aes(long, lat, group = group, fill = data$numericvariable))
+ geom_polygon() + coord_equal() + facet_wrap(~Date.ordered) +
scale_fill_gradientn(limits = c(55,70),colours=c("white","red"))
The data is spatial data which has been coerced to a dataframe using the fortify command.
The output works independtly when I use the following commands:
data.split<-split(data,data$Date.ordered)
Maps<-list()
for (i in 1:length(data.split){
Maps[[i]]<- ggplot(data.split[[i]], aes(long, lat, group = group,
fill = data.split[[i]]$numericvariable)) + geom_polygon() + coord_equal() +
scale_fill_gradientn(limits = c(55,70),colours=c("white","red"))
print(Maps[[i]])
}
But this is very hard to coerce nicely into one visualisation, even using multiplot.
Because the visualisations work correctly when done independently, I'm currently assuming this is a coding error on my behalf as opposed to a data issue, but I can post an example of what the data looks like if this would help.
Thanks.
来源:https://stackoverflow.com/questions/30083382/r-incorrect-output-from-ggplot2-and-facet-wrap-when-using-spatial-data