[R-sig-Geo] plotting counties of two states
Sarah Goslee
sarah.goslee at gmail.com
Mon Jun 17 15:31:02 CEST 2013
Hi,
Since you sent me your data (I'd still prefer dput() and you should
use that in the future so you can send your data to the list):
I don't see how your problem has anything to do with YNP. See below.
On Mon, Jun 17, 2013 at 9:18 AM, Milan Sharma <milansharma2012 at yahoo.com> wrote:
> Could you please run my code and see the problem? I spent couple days in it.
> please help me, I attached my data to u.
> library(maps)
> library(maptools)
> library(spdep)
> data<-read.csv("2data.csv",header=TRUE)
> dim(data)#79 rows
> data$stateco <- paste(tolower(data$state),tolower(data$county), sep='.')
> comap <- map("county",c("montana","wyoming"), fill=T, plot=F)
> commaReplace <- function(x) {
> return(gsub(",",".", x))
> }
> comap$names <- sapply(comap$names, commaReplace, simplify=TRUE)
> names(comap$names) <- NULL
> length(comap$names)
> comapP <- map2SpatialPolygons(comap, comap$names)
> proj4string(comapP) <- CRS("+proj=longlat")
> comapP<-comapP[-1]
^^^
This line deletes Montana's Beaverhead county from your SpatialPolygon
object. Is that really what you wanted to do?
> index <- match(comap$names,data$stateco)
> index
> data2 <- data[index,]
> row.names(data2) <- comap$names
This adds NA values for YNP, which makes sense.
> counties <- SpatialPolygonsDataFrame(comapP, data=data2)
But now you're trying to use an 80-element data frame with a
79-element Spatial Polygon object.
If you refrain from deleting a perfectly good county, your code runs.
Sarah
> ________________________________
> From: Sarah Goslee <sarah.goslee at gmail.com>
> To: Milan Sharma <milansharma2012 at yahoo.com>
> Cc: "r-sig-geo at r-project.org" <r-sig-geo at r-project.org>
> Sent: Monday, June 17, 2013 6:56 AM
> Subject: Re: [R-sig-Geo] plotting counties of two states
>
> Hi,
>
> On Sun, Jun 16, 2013 at 10:46 PM, Milan Sharma
> <milansharma2012 at yahoo.com> wrote:
>> Hi all,
>> As I already mentioned, this is the problem due to one extra county
>> "yellowstone national" in montana. I am trying to plot Montana and wyoming
>> together for further analysis.. At the last line, index does not match
>> between counties$names and data$co(see NA below). I know this is is due to
>> the extra county of montana. I would appreciate any help in this.
>
> Well, what do you want to have happen for Yellowstone National Park?
>
> You could import your own shapefile with the county boundaries you
> expect, you could insert NA values into your data for YNP, you could
> modify the file that you're using to remove that entry. And so on.
> There's not an extra county, but there is an extra-county geographic
> area. You need to decide what the best way for your purposes is to
> handle that.
>
>
>> I attached data if u want to have a look.
>
> The server blocks most attachments, and dput() is the best way to
> provide data as part of a reproducible example.
>
>> library(maps)
>> data<-read.csv("2data.csv",header=TRUE)
>> dim(data)#79 rows
>> [1] 79 6
>> data[1,]#first row
>> state county Y X Longitude Latitude
>> 1 Montana Beaverhead 5.393363 7.893883 -112.8929 45.13386
>> data$co <- paste(tolower(data$state),tolower(data$county), sep='.')
>> counties <- map("county",c("montana","wyoming"), fill=T, plot=F)
>> commaReplace <- function(x) {
>> + return(gsub(",",".", x))
>> + }
>> counties$names <- sapply(counties$names, commaReplace, simplify=TRUE)
>> names(counties$names) <- NULL
>> length(counties$names)
>> [1] 80
>> index <- match(counties$names,data$co)
>> index
>> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
>> 24 25
>> [26] 26 27 29 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
>> 49 50
>> [51] 51 52 53 54 55 56 NA 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
>> 73 74
>> [76] 75 76 77 78 79
>> #####
>> Thanks in advance,
>> Milan
>>
>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
>
--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org
More information about the R-sig-Geo
mailing list