1

I am trying to make some nice spatial maps in R. I am trying to understand how to upload the data in case you would like to have a look at them but I have not figured that out (I am sorry for that but being a new user means looking for all these things).

What is my situation. I have a shapefile of the whole USA, I only need some states, and I can select my grid when I plot it (as you see from the code in the plot section).

I also have some yield data (points) which have latitude, longitude and Yield. I have 4 different Yield data which are called ("All Stations", "0.5", "1.0", and "2.0").

I am trying to plot these 4 yields data on the spatial map to have 4 different spatial maps. Which is done.

I have done this by reading on stackoverflow here and there I used bits and pieces to do that, although I have never done it before I was surprised how fast I could advance (Thank you people on StackOverflow!!!).

Can someone help me to understand if my code is correct?

Also, how can I make the legends of the 4 maps a regularr scale? E.g. from 4000 to 9000 with 500 intervals for each of the 4 maps. What I have done is to create a separate text file ("Yield for Legend.txt") which I use to generate the colour scales in the maps and the legends. Is that correct?

Again, your critics are mostly welcome!

Thank you, David

    rm(list=ls())
    setwd("C:\\Users\\.....\\Shape File")
    library(spatstat)
    library(rgdal)
    library(shapefiles)
    library(maptools)
    library(RColorBrewer)
    library(classInt)


    # read in shapefiles

    counties.rg <- readOGR("C:\\Users\\......\\Shape File", "tl_2011_us_county")


    Yields <- read.table("Yield.txt", skip=1, header = F)

    Yield.g <- as.ppp(Yields, owin( c(-89, -76), c(25, 37)))

    ## Reading Data for Legend and colouring breaks

     Y.LE <- read.table("Yield for Legend.txt", header=F)
     Y.L.I <- classIntervals(Y.LE$V1, n=9, style = "quantile")
     Y.L.I <- Y.L.I$brks

    #select color palette and the number colors (levels of income) to represent on the map
    #colors <- brewer.pal(9, "RdYlGn")
    colors <- brewer.pal(9, "Greys")

    ################################################
    ### Generating MAPS ############################
    ################################################

    #set breaks for the 9 colors
      #par(mfrow=c(2,2))

     pdf("13 August Spatial Maps.pdf") 
    # All Points 

    brks.all <-classIntervals(Yields$V3, n=9, style = "quantile")
    brks.all <- brks.all$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V3, Y.L.I,all.inside=TRUE)], pch=21)

    #add a title
    title(paste ("Rainfed Yield (kg/ha)All Stations"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)



    # 0.5 Grid  
    brks.05 <-classIntervals(Yields$V4, n=9, style = "quantile")
    brks.05 <- brks.05$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V4, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GF$V1, col="grey40")
    #abline(h=GF$V2, col="grey10", lty="dotted")
    #backup
    #points(Yield.g, cex= Yields$V4/9000, col=colors[findInterval(Yields$V4, brks.05,all.inside=TRUE)], pch=19)

    #add a title
    title(paste ("Rainfed Yield (kg/ha)0.5"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)


    # 1.0 Grid
    brks.1 <-classIntervals(Yields$V5, n=9, style = "quantile")
    brks.1 <- brks.1$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V5, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GO$V1, col="grey40")
    #abline(h=GO$V2, col="grey10", lty="dotted")
    #add a title
    title(paste ("Rainfed Yield (kg/ha)1.0"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)


    # 2.0 Grid

    brks.2 <-classIntervals(Yields$V6, n=9, style = "quantile")
    brks.2 <- brks.2$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V6, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GG$V1, col="grey40")
    #abline(h=GG$V2, col="grey10", lty="dotted") 
    #add a title
    title(paste ("Rainfed Yield (kg/ha)2.0"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)
dev.off()
david
  • 113
  • 1
  • 8
  • 1
    You want us to check very long code that is not reproducible (since we have no access to your data). You neither specify what the problem is. Is there even a problem? And furthermore you don give us a vague discription of what the output should be. Please update your question. – Thierry Aug 13 '12 at 14:09
  • 1
    Welcome to StackOverflow. Perhaps if you made a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that demonstrates your question / problem, people would find it easier to answer. – Andrie Aug 13 '12 at 15:45
  • Hi! thank you very much for pointing out this shortcoming. I will attach some data and try to make the question a bit more clear. The thing is that I am very new to StackOverflow but I have used it a lot and helped me to be learn R very quickly. Since this code is a paste of bits and pieces from all over I was wondering if it made sense. Let me work out how to attach some data and I will be happy to put them here. – david Aug 13 '12 at 19:16

1 Answers1

2

In response to your specific query about setting the breaks and having your legend match, there is a fairly straightforward fix.

You use the classInterval function with style="quantile" to define your breaks. If you want the maps to show "from 4000 to 9000 with 500 intervals for each of the 4 maps" why not use style="fixed"

brks.all <-classIntervals(Yields$V3, n=10, style = "fixed",
  fixedBreaks=seq(from=4000, to=9000, by=500)
brks.all <- brks.all$brks 

Note that 4k to 9k by 500's creates 10 intervals by my count and 9 color gradations doesn't often make for a pretty map.

Alternatively, the dataPrecision variable within classInt may also help you get labels and breaks that are closer to what you want, but still based on quantiles (if not uniform across maps)

csfowler
  • 428
  • 2
  • 12
  • Great Thanks!! that is a great help. When I type this code it appears this Error message: "Warning message: In classIntervals(Yields$V3, n = 12, style = "fixed", fixedBreaks = seq(from = 4000, : variable range greater than fixedBreaks" However, I kept going and when I enter "brks.all" I can see the new intervals, which I can also display on the maps!! I think this could be the way to go then. – david Aug 13 '12 at 20:02
  • I see your point about having 10 intervals and 9 colour gradients....I can possibly have only 9 intervals and so I will have a match. I am looking around to see if there are other ways of colouring in R. – david Aug 13 '12 at 20:08
  • Note that your error message is telling you that there are values in V3 that are less than 4000 or greater than 9000. These will not be assigned to any class (color) in your map as a result. As for color, it looks like you already have RColorBrewer installed. Go to their web site to learn more about colors and mapping. It is a real art form. – csfowler Aug 14 '12 at 01:40
  • Thank you! I was doing it yesterday evening, it is really amazing the website! Thank you for the suggestions! – david Aug 14 '12 at 13:53