R, Incorrect output from ggplot2 and facet_wrap when using spatial data

烂漫一生 提交于 2019-12-11 22:03:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!