Here the code, I finally managed to do the merging, but I fear that I did not the simplest and best way to do it.

Katharina

PS: By the way, the new shape file did not help to get the fill colors right in the plotted map (see previous thread http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4645.html). The only (ugly) way that worked was to define the fill colours as palette.pat1<-c(rep("lightgreen", 10), rep("green", 40), rep("lightblue", 50), rep("blue",100), rep("yellow",200), rep("orange",300), rep("red",300)). 


**************************************************

# set working directory
setwd("E:\\Agegeography\\Data\\")
library(rgdal)
library(maps)
library(maptools)
library(gpclib)
library(spgpc)

#load data as polyshape and define Primary as id (=mapid_nr)
xx<-readShapePoly("germany.shp", "Primary")

# merge counties to obtain 343 out of 455 counties
# add IAB county ID to germany map
# read concordance data 445 MAP counties - 343 IAB counties
conc_Map_IAB<-read.table("conc_Map_IAB.txt", sep="\t", header=TRUE)
str(conc_Map_IAB)

for (i in 1:length(xx$Primary)) {
          for (j in 1:length(conc_Map_IAB$id)) {
                  if (xx$Primary[i]==conc_Map_IAB$id[j])   {
                            xx$IAB[i]<-conc_Map_IAB$idIAB[j]
                                                           }
                                               }
                                }

#now aggregate according to IABcounty numbers
aggregation=unionSpatialPolygons(xx, xx$IAB)

#get merge id, put it as new id
ID=getSpPPolygonsIDSlots(aggregation)
out<-SpatialPolygonsDataFrame(aggregation, data=data.frame(ID,row.names=ID))

#create shape file
writePolyShape(out, fn="aggregationIAB")

#read shape file, use ID as id
agg<-readShapePoly("aggregationIAB", ID="ID")


#now add patenting information
#read patenting data and merge it to dataframe belonging to polygon
patents<-read.table("mydata.txt", sep="\t", header=TRUE)
patents<-patents[order(patents$did_IAB),]
out2<-SpatialPolygonsDataFrame(agg, data=data.frame(patents, row.names=patents$did_IAB))
writePolyShape(out2, fn="patenting_ger")

#load data
end<-read.shape("patenting_ger")

#transfer to polylist object
end.poly<-Map2poly(end)

#plot map with no of corporate patents and save in pdf file
pdf("patbus_coloured.pdf")
brks.pat1<-c(1:1000)
#legend.text1<-c("1-10", "11-50", "51-100", "101-200", "201-300", "301-400", "400+")
palette.pat1<-c(rep("lightgreen", 10), rep("green", 40), rep("lightblue", 50), rep("blue",100), rep("yellow",200), rep("orange",300), rep("red",300))
palette.pat1
plot.polylist(end.poly, col=palette.pat1[findInterval(end$att.data$patbus, brks.pat1)])
legend(1200, -200, fill=palette.pat1, legend=legend.text1)
dev.off()


 


-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand@nhh.no]
Sent: Fr 10.11.2006 18:04
To: Frosch, Katharina
Cc: r-sig-geo@stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Best way to merge selected regions in a map?
 
On Fri, 10 Nov 2006, Frosch, Katharina wrote:

> Dear all, 
> 
> I work with a county map of Germany and have to merge 100 out of 440
> counties in total. I tried several ways, using unionSpatialPolygons from
> the spgpc package and combine union and append commands from the gpclib
> package, but did not succeed. What is the best and most "elegant" way to
> do the merging? Especially: Which objects do I have to use (+ how to
> keep and to retrieve the ID of the counties without mixing it up). 
> 
> Either I do not manage to choose the respective two counties to merge,
> or the commands do not correspond to the object I saved the two counties
> in. I start with a MIF/MID file of Europe, which I load into R and then
> save using writePolyShape. 

Please include some of the commands you have used. In principle 
unionSpatialPolygons() should work, given that the classifying factor has 

length(unique(factor)) < length(factor)

and the boundaries of the polygons are the same. If the boundaries are 
separated, for example by a river, the output Polygons object will be 
joined, but will be represented by two distinct Polygon objects.

So there are two possible reasons for the dissolve not working.

More advice on the arguments to the function will be easier to give if you 
paste the ones you used into your posting.

Roger

> 
> Any explanation how to proceed in general would be great. If this kind
> of question is not very specific and is covered, more or less, in any
> manual, I'd appreciate the link as I guess that I am lacking also some
> basics (and the documentation . 
> 
> Thanks in advance
> Katharina
> 
> 
> 
> 
> 
> Katharina Frosch
> Rostocker Zentrum
> Konrad-Zuse-Str. 1
> 18057 Rostock
> Tel.: (0381) 2081-148
> Fax: (0381) 2081-448
> Mail: frosch@rostockerzentrum.de 
> 
> 
> ----------
> This mail has been sent through the MPI for Demographic Rese...{{dropped}}
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand@nhh.no




----------
This mail has been sent through the MPI for Demographic Rese...{{dropped}}


