24 Days of R: Day 21

December 21, 2013

(This article was first published on PirateGrunt » R, and kindly contributed to R-bloggers)

I drove through Tennessee today. Despite having grown up in Kentucky, I had never really noticed the Tennessee flag, but earlier this year, while in Nashville, I saw it and thought it was one of the cooler state flags. I downloaded a PNG file of the flag from wikicommons and tried to do what I thought would be a very simple thing. I wanted to draw the state of Tennessee and have the flag appear on top, but clipped along the borders of the state.

Drawing a picture of the flag is fairly easy.

pngTN = readPNG("./figure/800px-Tennessee_state_flag.png")
plot(x = c(0, 1000), y = c(0, 1000), type = "n", xaxt = "n", yaxt = "n", xlab = "", 
    ylab = "", bty = "n")
rasterImage(pngTN, 0, 0, 1000, 1000)

plot of chunk TN_Flag

As is drawing a picture of the state.

tn = getData("GADM", country = "USA", level = 1)
tn = tn[match("tennessee", tolower(tn$NAME_1)), ]

plot of chunk TN

I can even draw one on top of the other.

tn.bbox = [email protected]
xleft = tn.bbox[1, 1]
xright = tn.bbox[1, 2]
ybottom = tn.bbox[2, 1]
ytop = tn.bbox[2, 2]
plot(x = c(xleft, xright), y = c(ybottom, ytop), type = "n", xaxt = "n", yaxt = "n", 
    xlab = "", ylab = "", bty = "n")
rasterImage(pngTN, xleft, ybottom, xright, ytop)
plot(tn, add = TRUE)

plot of chunk Both

This next bit required about an hour of work, fussing and research and it still doesn't work right. The assignment of the raster values to the RasterLayer won't accept character data. I converted to factors, which means that the shape of the image is fine, but the colors are off. It's also a very slow operation.

mojo = raster(xmn = xleft, xmx = xright, ymn = ybottom, ymx = ytop, nrow = 480, 
    ncol = 800)
rasterTN = as.raster(pngTN)
rasterTN = as.factor(rasterTN)
mojo[] = rasterTN
masked = mask(mojo, tn)
## Found 1 region(s) and 1 polygon(s)

plot of chunk Clipped

Until tonight, I had thought of a “raster” as something that sounds like a person from Jamaica. It's clear that I need to learn more as this is a hot topic in GIS circles.

As usual, there's virtually nothing new here. Here are a few links to other dabblings, inspiration and full code examples.:
Use of mask is something that I saw on gis.stackexchange

I came across this site while doing basic interweb research. I didn't spend a great deal of time with that code as it was a bit mysterious.

Last year, is.R() did a number of posts about flags. This is where I first saw the use of the rasterImage function

## function (package = NULL) 
## {
##     z <- list()
##     z$R.version <- R.Version()
##     z$platform <- z$R.version$platform
##     if (nzchar(.Platform$r_arch)) 
##         z$platform <- paste(z$platform, .Platform$r_arch, sep = "/")
##     z$platform <- paste0(z$platform, " (", 8 * .Machine$sizeof.pointer, 
##         "-bit)")
##     z$locale <- Sys.getlocale()
##     if (is.null(package)) {
##         package <- grep("^package:", search(), value = TRUE)
##         keep <- sapply(package, function(x) x == "package:base" || 
##             !is.null(attr(as.environment(x), "path")))
##         package <- sub("^package:", "", package[keep])
##     }
##     pkgDesc <- lapply(package, packageDescription, encoding = NA)
##     if (length(package) == 0) 
##         stop("no valid packages were specified")
##     basePkgs <- sapply(pkgDesc, function(x) !is.null(x$Priority) && 
##         x$Priority == "base")
##     z$basePkgs <- package[basePkgs]
##     if (any(!basePkgs)) {
##         z$otherPkgs <- pkgDesc[!basePkgs]
##         names(z$otherPkgs) <- package[!basePkgs]
##     }
##     loadedOnly <- loadedNamespaces()
##     loadedOnly <- loadedOnly[!(loadedOnly %in% package)]
##     if (length(loadedOnly)) {
##         names(loadedOnly) <- loadedOnly
##         pkgDesc <- c(pkgDesc, lapply(loadedOnly, packageDescription))
##         z$loadedOnly <- pkgDesc[loadedOnly]
##     }
##     class(z) <- "sessionInfo"
##     z
## }
## <bytecode: 0x0000000013b2c778>
## <environment: namespace:utils>

To leave a comment for the author, please follow the link and comment on their blog: PirateGrunt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training





CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)