[R-sig-Geo] fortify.SpatialPolygonsDataFrame error

O'Hanlon, Simon J simon.ohanlon at imperial.ac.uk
Sun Aug 19 14:25:58 CEST 2012


Hey Christian,
As your example is not reproducible (i.e. no access to NUTS1 shapefile) its a bit hard to say exactly, but perhaps this might work for you:

# Try row.names() rather than rownames()
NUTS1 at data$id <- row.names(NUTS1 at data)   
NUTS1.points <- fortify(NUTS1, region="id")
NUT1.df <- dataframe(NUTS1)

# This join operation allows you to join attribute data from the shapefile (which I assume you have as NUTS1 is a SpatialPolygonsDataFrame) to each row of the data frame used for plotting
NUTS1.map <- join(NUTS1.points, NUTS1.df , by = "id" ) 

# To ggplot2 a geom_polygon is simply a filled geom_path - if you want transparent polygons you may as well use geom_path instead
p <- ggplot() + geom_path( data=NUTS1.map , aes( long , lat , group = group ) , colour="black" ) + coord_map()
print(p)

HTH,

Simon
________________________________________
From: r-sig-geo-bounces at r-project.org [r-sig-geo-bounces at r-project.org] on behalf of christiaan pauw [cjpauw at gmail.com]
Sent: 19 August 2012 09:12
To: r-sig-geo
Subject: Re: [R-sig-Geo] fortify.SpatialPolygonsDataFrame error

Hi everybody

I have the same problem. It seems there is no such function as
invert() . I googled quite a bit an could not find such a function?
Does anyone know of an invert() function in R?

regards
Christiaan

On 27 February 2012 12:27, Paul Hiemstra <paul.hiemstra at knmi.nl> wrote:
> On 02/23/2012 04:48 PM, marion le texier wrote:
>> Hi everyone,
>>
>> I would like to know if someone of you has already got troubles when trying to use ggplot2 with a shape file?
>> Here is what I've done:
>>
>> NUTS1<-readShapeSpatial("nuts1_2003", proj4string=CRS(projargs=("+init=epsg:3035")),IDvar="NUTS1")
>> plot(NUTS1)
>>
>> NUTS1 at data$id <- rownames(NUTS1 at data)
>>
>> # as codes are characters and not numeric values, I've changed them into:
>>
>> test<-as.numeric(seq(1:94))
>> NUTS1 at data$id<-test
>>
>> NUTS1.points <- fortify.SpatialPolygonsDataFrame(NUTS1, region="id")
>>
>> # The function doesn't want to be applied and I get this error message:
>>
>> Erreur dans unionSpatialPolygons(cp, invert(polys)) : input lengths differ
>> De plus : Message d'avis :
>> In split(as.numeric(row.names(attr)), addNA(attr[, region], TRUE)) :
>>   NAs introduits lors de la conversion automatique
>>
>> (sorry for the French comments...)
>>
>>
>> # What I would like to do at the end is :
>>
>> NUTS1.map <- ggplot()+geom_polygon(data=NUTS1.points, aes(long, lat, group=group), fill=NA, colour="black") + coord_map()
>>
>>
>> Do you have any idea of what I'm doing wrong?
>>
>> Thanks you very much for your help!!!
>> Marion
>>
>>
>>       [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

Christiaan Pauw
Nova Institute
www.nova.org.za

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list