Showing posts with label raster. Show all posts
Showing posts with label raster. Show all posts

Friday, May 3, 2013

RG#106: add satellite imagery, or open street maps to your plots using openmap package (bing, mapquest)


library(OpenStreetMap)
library(rgdal)

# get world map
map <- openmap(c(70,-179), c(-70,179))
plot(map)

bingmap <- openmap(c(70,-179), c(-70,179), type = "bing")
plot(bingmap)


# types available 

# type = c("osm", "osm-bw", "maptoolkit-topo", "waze", "mapquest", "mapquest-aerial", "bing", "stamen-toner", "stamen-terrain", "stamen-watercolor", "osm-german", "osm-wanderreitkarte", "mapbox", "esri", "esri-topo", "nps", "apple-iphoto", "skobbler", "cloudmade-<id>", "hillshade", "opencyclemap", "osm-transport", "osm-public-transport", "osm-bbike", "osm-bbike-german")


#zoom maps, plot a portion
upperLeft, lowerRight
lat <- c(43.834526782236814, 30.334953881988564)
lon <- c(-85.8857421875, -70.0888671875)
southest <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=7,'osm')
plot(southest) 




library(UScensus2000tract)
data(south_carolina.tract)
lat <- c(35.834526782236814, 30.334953881988564)
lon <- c(-85.8857421875, -70.0888671875)
southest <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=7,'osm')
south_carolina.tract <- spTransform(south_carolina.tract,osm())

plot(southest)
plot(south_carolina.tract,add=TRUE,col=(south_carolina.tract@data$med.age>35)+4)


plot(southest)
plot(south_carolina.tract,add=TRUE,col=(south_carolina.tract@data$med.age>55)+4)






Thursday, April 25, 2013

RG#89: Raster in R


 require(grid)
grid.raster(matrix(colors()[1:600], ncol=20))





set.seed(1234)

mt <- matrix (sample(c("red","red1", "yellow", "purple", "green1", "green4", "blue"), 10000, replace = TRUE), ncol = 100)

grid.raster(mt)


 
rgb.palette <- colorRampPalette(c("red", "orange", "blue"),
                                space = "rgb") 
 
bg.palette <- colorRampPalette(c("blue", "green"), space = "rgb")

gr.palette <-  colorRampPalette(c("green", "red"),
                                space = "rgb")
 
 
colrs <- matrix(c(rgb.palette(20),bg.palette(20),gr.palette(20)),   nrow=6, ncol=10)
grid.newpage()
 grid.raster(colrs)
  grid.raster(colrs, interpolate=FALSE)
# raster in ggplot2
require(ggplot2)

# Generate data
funp <- function (n,r=2) {
 xv <- seq(-r*pi, r*pi, len=n)
 df1 <- expand.grid(x=xv, y=xv)
 df1$r <- sqrt(df1$x^2 + df1$y^2)
 df1$z <- cos(df1$r^2)*exp(-df1$r/6)
 df1
}
qplot(x, y, data = funp(1000), fill = z, geom = "raster") + theme_bw()