Skip to content

Commit

Permalink
CRAN version
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothée Giraud committed Jun 7, 2016
1 parent 1760ca4 commit 156f26a
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 17 deletions.
8 changes: 3 additions & 5 deletions R/huff.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ huff <- function(knownpts,
#' # grid defined by its resolution
#' myhuff <- huff(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 750, beta = 2,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of huff values
#' myhuffraster <- rasterHuff(x = myhuff, mask = spatMask)
#' plot(myhuffraster)
Expand Down Expand Up @@ -151,12 +151,10 @@ rasterHuff <- function(x, mask = NULL){
#' # grid defined by its resolution
#' myhuff <- huff(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 750, beta = 2,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of huff values
#' myhuffraster <- rasterHuff(x = myhuff, mask = spatMask)
#' # Plot Huff values nicely
#' plotHuff(x = myhuffraster)
#' @import sp
#' plotHuff(myhuffraster)
#' @import raster
#' @export
plotHuff <- function(x, add = FALSE){
Expand Down
4 changes: 2 additions & 2 deletions R/reilly.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ reilly <- function(knownpts,
#' # grid defined by its resolution
#' myreilly <- reilly(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 750, beta = 2,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of reilly values
#' myreillyraster <- rasterReilly(x = myreilly, mask = spatMask)
#' plot(myreillyraster, col = rainbow(18))
Expand Down Expand Up @@ -164,7 +164,7 @@ rasterReilly <- function(x ,mask = NULL){
#' # grid defined by its resolution
#' myreilly <- reilly(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 750, beta = 2,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of reilly values
#' myreillyraster <- rasterReilly(x = myreilly, mask = spatMask)
#' # Plot the raster nicely
Expand Down
4 changes: 2 additions & 2 deletions R/stewart.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ stewart <- function(knownpts,
#' # grid defined by its resolution
#' mystewart <- stewart(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 1000, beta = 3,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of potentials values
#' mystewartraster <- rasterStewart(x = mystewart, mask = spatMask)
#' plot(mystewartraster)
Expand Down Expand Up @@ -164,7 +164,7 @@ rasterStewart <- function(x, mask = NULL){
#' # grid defined by its resolution
#' mystewart <- stewart(knownpts = spatPts, varname = "Capacite",
#' typefct = "exponential", span = 1000, beta = 3,
#' resolution = 50, mask = spatMask)
#' resolution = 100, mask = spatMask)
#' # Create a raster of potentials values
#' mystewartraster <- rasterStewart(x = mystewart, mask = spatMask)
#' # Plot stewart potentials nicely
Expand Down
5 changes: 2 additions & 3 deletions man/plotHuff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plotReilly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plotStewart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rasterHuff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rasterReilly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rasterStewart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions vignettes/SpatialPosition.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## ---- fig.width=5, fig.height=5------------------------------------------
library(SpatialPosition)
data(spatData)

# Compute potentials (accessibility)
globalAccessibility <- stewart(knownpts = spatPts, varname = "Capacite",
typefct = "exponential", span = 1000, beta = 3,
resolution = 50,
mask = spatMask)

# Create a raster
rasterAccessibility <- rasterStewart(x = globalAccessibility, mask = spatMask)

# Plot the raster
par(mar = c(4,2,2,1))

plotStewart(x = rasterAccessibility, add = FALSE, nclass = 6)

# The function returns break values
breakValues <- plotStewart(x = rasterAccessibility, add = FALSE, nclass = 6)

# Create contour lines and add them to the plot
contLines <- rasterToContour(x = rasterAccessibility, levels = breakValues)
plot(contLines, add = TRUE)
plot(spatMask, add = TRUE)

mtext("Global Accessibility to Public Hospitals", side = 3,cex = 1.5)
mtext(text = "Potential nb. of beds
distance function: exponential, span = 1 km, beta = 3",
side = 1, line = 1)



## ---- fig.width=5, fig.height=5------------------------------------------
row.names(spatPts)
catchReilly <- reilly(knownpts = spatPts, varname = "Capacite",
typefct = "exponential", span = 750, beta = 2,
resolution = 50, mask = spatMask)

# Create a raster
rasterCatch <- rasterReilly(x = catchReilly, mask = spatMask)

par(mar = c(4,2,2,1))
# Plot the raster and add the points
plotReilly(x = rasterCatch)
plot(spatPts, pch = 20, add = TRUE)

mtext("Catchment Areas of Public Hospitals", side = 3,cex = 1.5)
mtext(text = "distance function: exponential, span = 0.75 km, beta = 2",
side = 1, line = 0)

## ---- fig.width=5, fig.height=5------------------------------------------
###
catchHuff <- huff(knownpts = spatPts, varname = "Capacite",
typefct = "exponential", span = 750, beta = 2,
resolution = 50, mask = spatMask)

# Create a raster
rasterCatch <- rasterHuff(x = catchHuff, mask = spatMask)

# Plot the raster and add the points
par(mar = c(4,2,2,1))
plotHuff(x = rasterCatch)
plot(spatPts, pch = 20, col = "red", add = TRUE)

mtext("Probabilistic Catchment Areas \nof Public Hospitals",
side = 3,cex = 1.5, line=-1.5)
mtext(text = "distance function: exponential, span = 0.75 km, beta = 2",
side = 1, line = 0)

162 changes: 162 additions & 0 deletions vignettes/StewartExample.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
## ----regionalmap, fig.width=7, fig.height=6------------------------------
library(cartography)
library(SpatialPosition)
data(nuts2006)

# Compute the GDP per capita variable
nuts3.df$gdpcap <- nuts3.df$gdppps2008 * 1000000 / nuts3.df$pop2008

# Discretize of the variable
bv <- quantile(nuts3.df$gdpcap, seq(from = 0, to = 1, length.out = 9))

# Draw the map
opar <- par(mar = c(0,0,1.2,0))

# Set a color palette
pal <- carto.pal(pal1 = "wine.pal", n1 = 8)

# Draw the basemap
plot(nuts0.spdf, add = F, border = NA, bg = "#cdd2d4")
plot(world.spdf, col = "#f5f5f3ff", border = "#a9b3b4ff", add = TRUE)

# Map the regional GDP per capita
choroLayer(spdf = nuts3.spdf, df = nuts3.df,
var = "gdpcap",
legend.pos = "topright",
breaks = bv, col = pal,
border = NA,
legend.title.txt = "GDP per capita",
legend.values.rnd = -2,
add = TRUE)
plot(nuts0.spdf, add = TRUE, lwd = 0.5, border = "grey30")
plot(world.spdf, col = NA, border = "#7DA9B8", add = TRUE)

# Set a layout
layoutLayer(title = "Wealth Inequality in Europe",
sources = "Basemap: UMS RIATE, 2015 - Data: Eurostat, 2008",
author = "T. Giraud, 2015")
par(opar)

## ----regionalmappot, fig.width=7, fig.height=6---------------------------
# Create a distance matrix between units
mat <- CreateDistMatrix(knownpts = nuts3.spdf,
unknownpts = nuts3.spdf)

# Merge the data frame and the SpatialPolygonsDataFrame
nuts3.spdf@data <- nuts3.df[match(nuts3.spdf$id, nuts3.df$id),]

# Compute the potentials of population per units
# function = exponential, beta = 2, span = 75 km
poppot <- stewart(knownpts = nuts3.spdf,
unknownpts = nuts3.spdf,
matdist = mat,
varname = "pop2008",
typefct = "exponential",
beta = 2,
span = 75000)

# Compute the potentials of GDP per units
# function = exponential, beta = 2, span = 75 km
gdppot <- stewart(knownpts = nuts3.spdf,
unknownpts = nuts3.spdf,
matdist = mat,
varname = "gdppps2008",
typefct = "exponential",
beta = 2,
span = 75000)

# Create a data frame of potential GDP per capita
pot <- data.frame(id = nuts3.df$id,
gdpcap = gdppot$OUTPUT * 1000000 / poppot$OUTPUT,
stringsAsFactors = FALSE)

# Draw the map
par <- par(mar = c(0,0,1.2,0))

# Draw the basemap
plot(nuts0.spdf, add = F, border = NA, bg = "#cdd2d4")
plot(world.spdf, col = "#f5f5f3ff", border = "#a9b3b4ff", add = TRUE)

# Map the regional potential of GDP per capita
choroLayer(spdf = nuts3.spdf, df = pot,
var = "gdpcap",
legend.pos = "topright",
breaks = bv, col = pal,
border = NA,
legend.title.txt = "Potential\nGDP per capita",
legend.values.rnd = -2, add = TRUE)
plot(nuts0.spdf, add=T, lwd = 0.5, border = "grey30")
plot(world.spdf, col = NA, border = "#7DA9B8", add=T)

# Set a text to explicit the function parameters
text(x = 6271272, y = 3743765,
labels = "Distance function:\n- type = exponential\n- beta = 2\n- span = 75 km",
cex = 0.8, adj = 0, font = 3)

# Set a layout
layoutLayer(title = "Wealth Inequality in Europe",
sources = "Basemap: UMS RIATE, 2015 - Data: Eurostat, 2008",
author = "T. Giraud, 2015")
par(opar)

## ----smoothedmappot, fig.width=7, fig.height=6---------------------------
# Compute the potentials of population on a regular grid (50km span)
# function = exponential, beta = 2, span = 75 km
poppot <- stewart(knownpts = nuts3.spdf,
varname = "pop2008",
typefct = "exponential",
span = 75000,
beta = 2,
resolution = 50000,
mask = nuts0.spdf)

# Compute the potentials of GDP on a regular grid (50km span)
# function = exponential, beta = 2, span = 75 km
gdppot <- stewart(knownpts = nuts3.spdf,
varname = "gdppps2008",
typefct = "exponential",
span = 75000,
beta = 2,
resolution = 50000,
mask = nuts0.spdf)

# Transform the regularly spaced SpatialPointsDataFrame to a raster
popras <- rasterStewart(poppot)
gdpras <- rasterStewart(gdppot)

# Compute the GDP per capita
ras <- gdpras * 1000000 / popras

# Create a SpatialPolygonsDataFrame from the raster
pot.spdf <- rasterToContourPoly(r = ras,
breaks = bv,
mask = nuts0.spdf)

# Draw the map
par <- par(mar = c(0,0,1.2,0))

# Draw the basemap
plot(nuts0.spdf, add = F, border = NA, bg = "#cdd2d4")
plot(world.spdf, col = "#f5f5f3ff", border = "#a9b3b4ff", add = TRUE)

# Map the potential GDP per Capita
choroLayer(spdf = pot.spdf, df = pot.spdf@data, var = "center",
legend.pos = "topright",
breaks = bv, col = pal, add=T,
border = "grey90", lwd = 0.2,
legend.title.txt = "Potential\nGDP per capita",
legend.values.rnd = -2)
plot(nuts0.spdf, add=T, lwd = 0.5, border = "grey30")
plot(world.spdf, col = NA, border = "#7DA9B8", add=T)

# Set a text to explicit the function parameters
text(x = 6271272, y = 3743765,
labels = "Distance function:\n- type = exponential\n- beta = 2\n- span = 75 km",
cex = 0.8, adj = 0, font = 3)

# Set a layout
layoutLayer(title = "Wealth Inequality in Europe",
sources = "Basemap: UMS RIATE, 2015 - Data: Eurostat, 2008",
author = "T. Giraud, 2015")
par(opar)

0 comments on commit 156f26a

Please sign in to comment.