Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. library(plyr)
  2. library(ggplot2)
  3. library(sp)
  4. library(maptools)
  5. #library(threejs)
  6. library(reshape2)
  7. library(rgdal)
  8. library(rgeos)
  9.  
  10. #library(tools)
  11. #write_PACKAGES("~/scripts/mappingflows/")
  12.  
  13.  
  14.  
  15. #delete everything... start with a clean slate:
  16. rm(list=ls())
  17.  
  18. setwd("~/scripts/mappingflows/")
  19.  
  20. GBshapefiledir <- "~/scripts/mappingflows/CTRY_DEC_2013_GB_BGC.shp"
  21. GBshape <- readOGR(dsn=".",layer="CTRY_DEC_2013_GB_BGC")
  22.  
  23. fGBshape <- fortify(GBshape)
  24. #load the matrix, careful to have stringsAsFactors as FALSE
  25.  
  26. df<-read.csv("RMatrixs.csv",header = F,stringsAsFactors = F)
  27. rownames(df)<-df[,1] # msoa codes as rownames
  28. colnames(df)<-df[1,] # msoa codes as columns
  29. df = df[-1,] # delete first row
  30. df = df[,-1] # delete first column
  31.  
  32.  
  33. # tranform it to edgelist of the form origin,destination, total number of trips
  34. input <- melt(as.matrix(df))
  35.  
  36. # name columns created by melt
  37. names(input) <- c("origin","destination","total")
  38.  
  39.  
  40. #load centroid data,
  41.  
  42. centroids <- read.csv ("MSOA_based_LAD_PWCs.csv",header = T,stringsAsFactors = F)
  43.  
  44.  
  45. # merge the edgelist with the centroid dataframes by common cell
  46. # (called origin in edgelist and GSS_code in the MSOA file)
  47.  
  48.  
  49. orig.xy <- merge (input,centroids,by.x="origin",by.y="GSS_code")
  50. #name columns of resulting DF as follows("origin","destination","trips","oX","oY","o_name")
  51. names(orig.xy) <- c("origin","destination","trips","oX","oY","o_name")
  52.  
  53. #in order to avoid problems force fields to characters
  54. orig.xy$origin <- as.character(orig.xy$origin)
  55. orig.xy$destination <- as.character(orig.xy$destination)
  56.  
  57.  
  58. # merge the resultant DF with the centroid dataframes again by common cell
  59. # (called destination in edgelist and GSS_code in the MSOA file)
  60.  
  61. destination.xy <- merge (orig.xy,centroids,by.x="destination",by.y="GSS_code")
  62. #name columns of resulting DF and force fields to characters
  63. names(destination.xy) <- c("origin","destination","trips","oX","oY","o_name","dX","dY","d_name")
  64. destination.xy$origin <- as.character(destination.xy$origin)
  65. destination.xy$destination <- as.character(destination.xy$destination)
  66. destination.xy$trips <- as.numeric(destination.xy$trips)
  67.  
  68.  
  69. #hence now we have a DF with origin & destination ids + coords
  70.  
  71. #time for ggplot
  72.  
  73.  
  74. xquiet <- scale_x_continuous("",breaks=NULL)
  75. yquiet <- scale_y_continuous("",breaks=NULL)
  76.  
  77. quiet <-list(xquiet,yquiet)
  78.  
  79. myplot <- ggplot (destination.xy[which(destination.xy$trips>60),],aes(oX,oY)) +
  80. geom_polygon(data=fGBshape, aes(x = long, y = lat, group = group))+
  81. geom_segment(aes(x=oX,y=oY,xend=dX,yend=dY,alpha=trips),col="white") +
  82. scale_alpha_continuous(range=c(0.03,0.33)) +
  83. geom_point(data=destination.xy, aes(x=oX, y=oY, color="red"))+
  84. geom_text(data=destination.xy,size = 2,
  85. aes(label = o_name,color="white"))+
  86.  
  87.  
  88. theme (panel.background= element_rect(fill='grey60'))+ quiet + coord_equal()
  89.  
  90. myplot
  91. ggsave(myplot, file="sample.jpg", dpi = 600)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement