2

I was attempting to follow a tutorial to create a bubble plot of school district proficiency rates.

Data

shapefile of school districts (Census2010.shp)

District level data

Code

reading in data

dist <- read.csv('/Users/Path/to/District.csv', header = T)
state <- readShapePoly('/Users/Path/to/Census2010')

merge shapefile and district data

GA <- state
GA@data <-
    merge(GA@data, dist, by.x="NCESID", by.y="NCESID", all.x=T, sort=F )

creating bubble plot

plotvar <- GA@data$CCRPI
nclr <- 8
plotclr <- brewer.pal(nclr,"PuRd")
max.symbol.size=12
min.symbol.size=1
class <- classIntervals(plotvar, nclr, style="quantile") #Some school districts did not report data
colcode <- findColours(class, plotclr)
symbol.size <- ((plotvar-min(plotvar))/
    (max(plotvar)-min(plotvar))*(max.symbol.size - min.symbol.size) + min.symbol.size)
plot(GA)
GA.cntr <- coordinates(GA)
points(GA.cntr, pch=16, col = colcode, cex = symbol.size)
points(GA.cntr, cex = symbol.size)

Though the resulting map is just hollow school district outlines. What am I missing here?

1
  • +1 for reproducible spatial data and an interesting R problem.
    – Aaron
    Commented Jul 17, 2013 at 0:32

1 Answer 1

2

This code seems to work. You cannot use merge on an sp dataframe object. Merge resorts the dataframes during its operation breaking the rownames and the internal relationship in the resulting sp object. I changed the code to use readOGR in rgdal to read the shapefile. You were also using some R internals as objects (i.e, dist, class). This confuses R and often causes errors.

I am using version(s) R 3.0.1, classInt 0.1-19, RColorBrewer 1.0-5, rgdal 0.8-9, and sp 1.0-9

Note: if you omit dividing by pi the bubbles are far too big to plot.

    require(sp)
    require(rgdal)
    require(RColorBrewer)
    require(classInt)

    setwd("D:/TEST/Bubble")
      sdist <- read.csv(paste(getwd(), "District.csv", sep="/"), header = T)
      state <- readOGR(getwd(), "Census2010")
        GA <- state

    # Merge sp dataframe
    merge.sp.df <- function(x, y, xcol, ycol) {
      x@data$sort <- 1:nrow(as(x@data, "data.frame"))  
        xdf <- as( x@data, "data.frame")
         xdf <- merge(xdf, y, by.x=xcol, by.y=ycol) 
           xdf <- xdf[order(xdf$sort), ]
             row.names(xdf) <- xdf$sort
          xdf <- xdf[,- which(names(xdf) == "sort")]     
        x@data <- xdf  
       return(x)
    }

    GA <- merge.sp.df(GA, sdist, "NCESID", "NCESID")   

    # create bubble plot
    plotvar <- GA@data$CCRPI
    nclr <- 8
    plotclr <- brewer.pal(nclr,"PuRd")
      max.symbol.size=12
      min.symbol.size=1

    qclass <- classIntervals(plotvar, nclr, style="quantile") 
      colcode <- findColours(qclass, plotclr)

GA@data$symbol.size <- ((GA@data$CCRPI-min(GA@data$CCRPI,na.rm=TRUE))/(max(GA@data$CCRPI,na.rm=TRUE)-
                         min(GA@data$CCRPI,na.rm=TRUE))*(max.symbol.size - min.symbol.size) + min.symbol.size)/pi
      pt.pch <- rep(16,dim(GA)[1])  
        pt.pch[which(is.na(GA@data$symbol.size))] <- NA

plot(GA)
  points(coordinates(GA), pch=pt.pch, col=colcode, cex=GA@data$symbol.size)
    legend("topright", legend=names(attr(colcode, "table")), pch=16, 
           col=unique(colcode)[2:nclr+1])
2
  • thanks for taking a stab at this! I was trying to replicate your code, as I agree that the merge function is not ideal for this task. When i get to the readOGR function, I keep getting a "error in readOGR... missing layer" message. I re-downloaded the shape files to make sure nothing was missing to no avail. Am I missing a package?
    – Java_Sum
    Commented Aug 12, 2013 at 18:36
  • Addendum. At R 3.0.2 and sp 1.0-14 there is now a merge function that works on sp class objects. You just call the function as merge and it recognizes an sp class object. Commented Dec 6, 2013 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.