I want to plot kind of isarhytmoc points map like in this tutorial, but with population data. However, unfortunately, I have no enough skill and only came with such code:
require(sp)
require(rgdal)
require(RColorBrewer)
require(ggplot2)
library(plyr)
library(maptools)
library(rgeos)
require(gpclib)
gpclibPermitStatus()
gpclibPermit()
rus<-url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")
print(load(rus))
proj4.str <- CRS("+init=epsg:3413 +lon_0=105")
gadm.prj <- spTransform(gadm, proj4.str)
popul <- read.csv2(file="C:\\unempl11.txt", header = TRUE,
sep = ";",quote = "", dec=",", stringsAsFactors=F)
df <- fortify(gadm.prj, region = "ID_1")
df2 <- merge(df, gadm.prj, by.x="id", by.y="ID_1")
p <- ggplot(df2, aes(x = long, y = lat, group=group)) +
geom_point(data = popul,
mapping = aes(x=lon, y=lat, colour=abs),
size = 3,
alpha = 0.8) +
scale_colour_gradient2(name = "Population",
low = "darkred",
mid = "white",
high = "blue",
guide = "colorbar") +
ggtitle("Population in Russia)")
p + geom_path(data = df2,
mapping = aes(x=long, y=lat, group=group),
size = 0.125)
Here is the data I use, if anyone can help me with some ideas i will be grateful.
You are trying to display points but you are using an
SpatialPolygonsDataFrame
source. You can extract the coordinates
of the centroids with coordinates
and use them as coordinates of
your data, but I am not sure if that is what you need:
library(sp)
library(rgdal)
rus <- url("http://www.filefactory.com/file/4h1hb5c1cw7r/n/RUS_adm1_RData")
load(rus)
proj4.str <- CRS("+init=epsg:3413 +lon_0=105")
gadm.prj <- spTransform(gadm, proj4.str)
## IDs needed to match polygons and data
nms <- gadm.prj$NAME_1
ll <- coordinates(gadm.prj)
On the other hand, I cannot retrieve correctly the data
column
of your file. I fill it with some noise:
popul <- read.csv2('/tmp/popul.txt')
popul$data <- runif(nrow(popul))
Now it's time to match coordinates and data (similar to what we did in this previous question):
ord1 <- match(nms, popul$region)
popul <- popul[ord1,]
row.names(popul) <- nms
row.names(ll) <- nms
popSP <- SpatialPointsDataFrame(ll, popul["data"], proj4string=proj4.str)
This SpatialPointsDataFrame
can be directly displayed with
spplot
with the boundaries below using sp.layout
.
spplot(popSP, sp.layout=list('sp.polygons', gadm.prj))