ggplot2: fortify copies all SpPolyDaFr entries multiple times

182 views Asked by At

My goal is to plot this shapefile colored by a specific column.

It contains 100 polygons. I apply fortify() on it and join some missing columns

# convert SpPolyDaFrame into normal dataFrame for plotting
data.df = fortify(data) 

# join missing columns
data@data$id = rownames(data@data)
data.df$perc_ch = data@data$perc_ch
data.df = left_join(data.df, data@data, by=c('id'='id'))

After calling fortify(), every entry exists five times. (see 'order').

Calling str() on 'data.df':

'data.frame':   500 obs. of  11 variables:
$ long     : num  421667 421667 416057 416057 421667 ...
$ lat      : num  8064442 8060421 8060421 8064442 8064442 ...
$ order    : int  1 2 3 4 5 1 2 3 4 5 ...
$ hole     : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
$ piece    : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
$ id       : chr  "0" "0" "0" "0" ...
$ group    : Factor w/ 100 levels "0.1","1.1","2.1",..: 1 1 1 1 1 2 2 2 2 2 ...
$ perc_ch.x: num  17.4 11.4 20.5 12 15 ...
$ z        : int  1 1 1 1 1 2 2 2 2 2 ...
$ Ch_area  : num  3914498 3914498 3914498 3914498 3914498 ...
$ perc_ch.y: num  17.4 17.4 17.4 17.4 17.4 ...

This is introduced by fortify(). However, it does not change the plot outcome as long as I join the missing columns based on a matching column (= perc_ch.y). If I add missing columns without a matching index (=perc_ch.x), I run in troubles because of the redundant entries because wrong values are assigned to the polygons.

I do not see a reason for this copy effect?

1

There are 1 answers

2
hrbrmstr On BEST ANSWER

No need to bind the data to the polygons:

library(rgeos)
library(maptools)
library(rgdal)

URL <- "https://www.dropbox.com/s/rsr49jwm1pf9abu/data.zip?dl=1"
fil <- "sodata.zip"
if (!file.exists(fil)) download.file(URL, fil)

fils <- unzip(fil)
shp <- grep("shp$", fils, value=TRUE)

geo <- readOGR(shp, ogrListLayers(shp)[[1]], stringsAsFactors=FALSE, verbose=FALSE)

geo_map <- fortify(geo, region="z")

gg <- ggplot()
gg <- gg + geom_map(data=geo_map, map=geo_map,
                    aes(x=long, y=lat, map_id=id),
                    color=NA, size=0, fill=NA)
gg <- gg + geom_map(data=geo@data, map=geo_map,
                    aes(fill=perc_ch, map_id=z), 
                    color="#2b2b2b", size=0.15)
gg <- gg + viridis::scale_fill_viridis()
gg <- gg + ggthemes::theme_map()
gg <- gg + theme(legend.position="right")
gg

enter image description here