Add units to scale bar in spplot() - r

I am working with R version 2.14.1-1 on Ubuntu 12.04, using spplot to plot GIS data, specifically 2008 vote tallies by precinct.
I am a bit new to R. I have researched my problem without finding a solution. I would like to add units to the scale bar, in this case something like "% of vote" running vertically down the bar.
My code so far is:
library(gpclib)
gpclibPermit()
library(maptools)
library(maps)
library(classInt)
library(RColorBrewer)
library(sp)
require(graphics)
library(rgdal)
library(lattice)
pennStateMap <- readShapeSpatial("/home/jgoodson/stateData/Pennsylvania/shapeFiles/pa_final.shp")
plotvar <- pennStateMap#data$USPP2008
nclr <- 10
class <- classIntervals(plotvar, nclr, style="equal")
poliPallet <- colorRampPalette(c("red1", "blue1"), space="Lab")
pennCounties <- unionSpatialPolygons(pennStateMap, as.numeric(pennStateMap#data$COUNTYFP10))
spCounties <- list("sp.polygons", as(pennCounties, "SpatialPolygonsDataFrame"), col.regions="transparent")
spplot(pennStateMap, "USPP2008", main="2008 Presidential Election Results by Precinct in Pennsylvania", col.regions=poliPallet(10), col="transparent", at=round(class$brks, digits=1), sp.layout = list(spCounties))
I tried adding the units via:
barLabel <- list("sp.text", c(40, -71), "Dem % of Two-Party Vote", srt=-90)
and then adding that to sp.layout. But this will not go outside the drawing box for the geographic data, so it disappears when I try to get to the correct side of the scale bar.
I would appreciate any help I can get on how to accomplish this.

Related

trouble using RColorBrewer with spplot

I feel like many similar questions have been asked but I haven't found a solution to my problem yet in the related questions.
I am trying to change the colors on my map using spplot and the col.region command.
I have tried setting up a palette with the RColorBrewer package. I have tried using various values for the n: 5,6,7 (I am using 5 quantiles for my cuts) and even 1 to see if it would result in any observable change, but to no avail. I have also changed the choice of color several times but it also didn't bring any change.
I have also tried setting up a color function the colorRampPalette function but also unsuccessfully.
The data I'm using can be downloaded here (sorry, I couldn't figure out how to upload it with the question) : http://www.data.drees.sante.gouv.fr/ReportFolders/reportFolders.aspx, by typing "APL" in search bar and taking the first excel file (2015); apologies can't manage to link it properly either)
My code is the following:
library(maptools)
library(raster)
library(RColorBrewer)
library(readxl)
library(classInt)
apl15 <- read_excel("DATA USED")
france_com<- getData(name = "GADM",
country="FRA",
level=5)
paca <- subset(fr_com,NAME_1=="Provence-Alpes-Côte d'Azur")
apl15 <- data.frame(apl15)
names(apl15) <- c("code_com","name_com","apl","apl_young","pop_com","dept","apl*pop")
id_paca <- match(paca$NAME_5,apl15$name_com)
key_paca<- apl15[id_paca,"apl"]
paca$apl <- key_paca
apl.qt <- classIntervals(paca$apl,
n=5,
style="quantile")
br <- apl.qt$brks
offs <- 0.0000001
br[1] <- br[1] - offs
br[length(br)] <- br[length(br)] + offs
paca$apl_q <- cut(paca$apl,br)
display.brewer.all(type="seq")
my.palette <- brewer.pal(6, "OrRd")
spplot(paca,"apl_q",
col.region=my.palette,
main="APL en PACA", cex.=.7)
I expect this map but with colors corresponding to the palette I have selected.
my data mapped onto a region:
.
Thanks in advance for any help!

Plot connecting lines on a projected map (mapproj, gcIntermediate)

I am unsuccessfully trying to plot lines on a world map with Mollweide projection. I also plotted points on that same map, and it worked out fine. For the lines, I tried to adapt this example to my needs: http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/.
I failed with the pre-test (Step 4 in the ex.) already. In the following code, the line is supposed to connect Kenya and Australia. It runs without errors, but there's no line in the output. (I also tested the example without mapproj and the line is there.)
library("maps")
library("mapproj")
library("geosphere")
map("world",proj="mollweide", orientation= c(90,0,0), fill=TRUE, col="white", bg="lightblue")
lon_ke <- 38
lat_ke <- 1
lon_aus <- 133
lat_aus <- -27
inter <- gcIntermediate(c(mapproject(lon_ke,lat_ke), proj="mollweide", orientation= c(90,0,0)),
c(mapproject(lon_aus,lat_aus), proj="mollweide", orientation= c(90,0,0)),
n=50, addStartEnd=TRUE)
lines(inter)
I found a solution to my problem, based on Thomas Rahlf's book (see comment). Here's my script (it helps visualizing where authors publish articles).
library(maps)
library(geosphere)
library(mapproj)
#load data
locations <- read.csv("articles-authors-locations.csv", header=TRUE, check.names = FALSE)
#plot map with Mollweide projection
myProj.type<-"mollweide"
myProj.orient<-c(90,0,0)
x<-map(proj=myProj.type,orient=myProj.orient,wrap=T,fill=TRUE, col="white", bg="lightblue",xlim=range(locations$ArticleLong),ylim=range(locations$ArticleLat)
)
#plot jittered points for authors' locations
myStartP<-mapproject(jitter(locations$AuthorLong,amount=3),jitter(locations$AuthorLat, amount=1),proj=myProj.type,orient=myProj.orient)
points(myStartP,col="darkblue",pch=20,cex=1)
#set transparent colors
myTColour<-rgb(0,0,0,50,maxColorValue=255)
red_transp <- adjustcolor("red", alpha.f = 0.4)
#plot lines and jittered points, connecting authors' and articles locations
for (i in 1:nrow(locations))
{
myGC1<-gcIntermediate(c(locations$AuthorLong[i],locations$AuthorLat[i]),c(locations$ArticleLong[i],locations$ArticleLat[i]),addStartEnd=T, n=50)
moll<-mapproject(myGC1[,1],myGC1[,2],projection=myProj.type,orientation=myProj.orient)
lines(moll$x,moll$y,lwd=2.5,col=myTColour)
myDestP<-mapproject(
jitter(locations$ArticleLong[i], amount=3),
jitter(locations$ArticleLat[i], amount=1),
proj=myProj.type,orient=myProj.orient)
points(myDestP,col=red_transp,pch=20,cex=1)
}

R: Custom cuts using `choropleth` in `{GISTools}`

I am plotting some data on a shapefile using R and choropleth in {GISTools}
I would like to modify my code below in order to assign custom breaks.
using the World Borders Dataset, download from: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
library(GISTools)
library(rgdal)
shape_file <- readOGR(dsn ="/home/hamid/TM_WORLD_BORDERS_SIMPL-0.3", layer = "TM_WORLD_BORDERS_SIMPL-0.3")
shades <-auto.shading(shape_file#data$POP2005, cutter = quantileCuts, n=5, cols = brewer.pal(5, "Blues"))
choropleth(shape_file, shape_file#data$POP2005, shading = shades)
choro.legend(70.00000,32.50000, shades)
Can anyone help please?

Plotting points on a map

I am trying to plot coordinate points on a map, but I get the plot.new error. Could you please help?
library(maptools)
library(ggmap)
library(mapproj)
table <- read.table("table.txt", header=TRUE, sep=",")
map <- get_map(location = 'France', zoom = 6, maptype = c("toner"))
points(table$LONG, table$LAT, pch=21, bg=color, cex=0.7, lwd=.4)
ggmap(map)
Here is an idea of what the table looks like:
CITY,LAT,LONG
Paris,48.856667,2.351944
Lyon,45.766944,4.834167
Bordeaux,44.838611,0.578334
Try geom_point:
library(maptools)
library(ggmap)
library(mapproj)
city <- c("Paris", "Lyon", "Bordeaux")
my.lat <- c(48.856667, 45.766944, 44.838611)
my.long <- c(2.351944, 4.834167, 0.578334)
points <- data.frame(lon=my.long, lat=my.lat)
map <- get_map(location = c(left = -5, bottom = 42, right=9, top = 51 ), source = 'stamen', maptype = 'toner')
france <- ggmap(map, extent = 'normal')
france + geom_point(data=points, col="red")
Try the command ?ggmap for a list of great examples. I think the manual has done a good job, because before I read your question, I didn't even know of any of these functions. Thanks! I've learned something new.
Learn to walk before you try and run.
The points function adds points to an existing graphic. You haven't got an existing graphic yet (unless you've already done something you've not showed us).
Hence if you do points before starting a plot, you'll get an error. eg:
points(1:10,1:10) # plot.new error
plot(1:10,1:10) # no error, starts a new plot
points(10:1,1:10) # adds extra points, no error.
All your stuff with ggplot is irrelevant. Also, this is not about statistics, so you should have posted to StackOverflow. I've flagged this and it might get migrated...

Intersect the contour and polygon in R

I have been trying for a few days to create the contour and then plot the shapefile and contour on the same file. Now, that I am able to create the contour and shapefile on the same plot. I want to clip the contour with the shapefile an only show the shapefile.
The data temp.csv can be found on this link https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv
Shapefile can be found on the following location: https://www.dropbox.com/sh/ztvmibsslr9ocmc/YOtiwB8p9p
The script file image.scale.R can be found on the following location "https://www.dropbox.com/s/2f5s7cc02fpozk7/image.scale.R "
The code that I have used so far is as follows:
## Required packages
library(maptools)
library(rgdal)
library(sp)
library(maptools)
library(sm)
require(akima)
require(spplot)
library(raster)
library(rgeos)
## Set Working Directory
setwd("C:\\Users\\jdbaba\\Documents\\R working folder\\shape")
## Read Data from a file
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
x <- age2100$x
y <- age2100$y
z <- age2100$z
####################################
##Load the shape file
#####################################
shapefile <- readShapePoly("Export_Output_4.shp")
fld <- interp(x,y,z)
par(mar=c(5,5,1,1)) filled.contour(fld)
###Import the image.scale
source source("image.scale.R")
# http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html
x11(width=8, height=7)
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(6,1), height=6, respect=TRUE)
layout.show(2)
par(mar=c(4,4,1,2))
image(fld,axes=T)
contour(fld, add=TRUE)
#points(age2100$x,age2100$y, pch=".", cex=2,legend=F)
plot(shapefile,add=T,lwd=2)
box()
par(mar=c(4,0,1,4))
image.scale(fld, xlab="Eastings", ylab="Northings", xaxt="n", yaxt="n", horiz=FALSE)
axis(4)
mtext("Salinity", side=4, line=2.5)
The output of the above code is as follows:
Now, I want to get rid of the colored gradients and the contours from the polygon shapefile and only leave the intersection part.
Any help is highly appreciated.
Research: I found this link https://gis.stackexchange.com/questions/25112/clip-depth-contour-with-spatial-polygon on Stack exchange Gis and I tried to follow this method I always get error while creating the contour.
I found another similar thread on https://stat.ethz.ch/pipermail/r-sig-geo/2009-May/005793.html . But I couldn't make it work on my dataset.
I would like to acknowledge Marc in the box for helping me in getting to this point.
Thanks.
Indeed, #baptiste gave you a strong hint for the solution, the recent paper by Paul Murrell. Paul was generous to provide us with the code for his entire paper manuscript, which you can get from his personal website. On the side topic, Paul shows beautiful example of reproducible research with this paper. Generally, I took the following approach:
extract latitude and longitude coordinates from the shapefile (a function to do this is here, by Paul Hiemstra),
plot everything with your code,
and use polypath to remove everything outside the boundaries defined by shapefile, using extracted coordinates as a baseline.
#function to extract coordinates from shapefile (by Paul Hiemstra)
allcoordinates_lapply = function(x) {
polys = x#polygons
return(do.call("rbind", lapply(polys, function(pp) {
do.call("rbind", lapply(pp#Polygons, coordinates))
})))
}
q = allcoordinates_lapply(shapefile)
#extract subset of coordinates, otherwise strange line connections occur...
lat = q[110:600,1]
long = q[110:600,2]
#define ranges for polypath
xrange <- range(lat, na.rm=TRUE)
yrange <- range(long, na.rm=TRUE)
xbox <- xrange + c(-20000, 20000)
ybox <- yrange + c(-20000, 20000)
#plot your stuff
plot(shapefile, lwd=2)
image(fld, axes=F, add=T)
contour(fld, add=T)
#and here is the magic
polypath(c(lat, NA, c(xbox, rev(xbox))),
c(long, NA, rep(ybox, each=2)),
col="white", rule="evenodd")

Resources