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?
No need to bind the data to the polygons: