Incorrect plotting of data on Choropleth map with ggplot2 in R -
i trying make choropleth map of germany using ggplot2. data .csv file 2 rows (rs= containing numbers 1 16 each german state & tariff= 16 random positive , negative numbers).
rs tariff 1 01 -5.25 2 02 7.16 3 03 6.65 4 04 3.10 5 05 3.69 6 06 2.49 7 07 1.89 8 08 3.93 9 09 -5.84 10 10 -2.61 11 11 -0.21 12 12 2.35 13 13 -5.94 14 14 -7.54 15 15 -3.27 16 16 -8.75
also have shape file germany shape file. want plot positive , negative numbers onto map of germany each state, 2 colors (positive=green , negative=red). below code
library(xlconnect) library(sp) library(rgdal) library(ggplot2) library(plyr) library(rcolorbrewer) library(datacombine) library(rgeos) library(maptools) #### eeg data read #### eeg<-read.csv(file = "data/testdata1.csv", skip = 0, sep = ",", dec=".", header=true) colnames(eeg)<-c("rs", "tariff") eeg$rs<- c("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16") eeg$rs<-as.factor(eeg$rs) eeg$tariff<- as.numeric(eeg$tariff) #### shape data read #### bundesl<-readogr("data/new_shape/vg2500_bld.shp", "vg2500_bld") bundesl@data<- bundesl@data[order(bundesl$rs, na.last=na),] ### rearrange shape data better merging ### levels(bundesl$gen)<- c("schleswig-holstein", "mecklenburg-vorpommern", "hamburg", "bremen", "niedersachsen", "sachsen-anhalt", "brandenburg", "berlin", "nordrhein-westfalen", "hessen","thüringen","sachsen", "rheinland-pfalz", "saarland", "baden- württemberg", "bayern") bundesl$gen<- c("schleswig-holstein", "mecklenburg-vorpommern", "hamburg", "bremen", "niedersachsen", "sachsen-anhalt", "brandenburg", "berlin", "nordrhein-westfalen", "hessen","thüringen","sachsen", "rheinland-pfalz", "saarland", "baden-württemberg", "bayern") bundesl$shape_leng<- c("1217255.7","1780980.8","175253.8","154971.6","2016496.4","949096.8", "1295460.4","180751.2","1352108","1105092.8","961942.7","979294.3","910650.4", "282910.8","1298891.7","2046039.3") bundesl$shape_area<- c("15857425536","23044684847","760539820","405480872","47716406483","20494982327","29653902483","886480139","34047269991","21092318103","16178531941","18401642456","19834907486","2578541706","35801397076","70550070623") # #### shape data und eeg data join #### bundesl@data<-merge(bundesl@data, eeg, by="rs", all=true) # #### shapes plot #### bundesl@data$id <- (as.numeric(rownames(bundesl@data))-1) bundesl.df<-fortify(bundesland) bundesl.df <- join(bundesl.df, bundesl@data, by="id") ggp <- ggplot(data=bundesl.df, aes(x=long, y=lat, group=group)) ggp <- ggp + geom_polygon(aes(fill=tariff), col="black") ggp <- ggp + coord_map() ggp <- ggp + scale_fill_continuous(name=expression(tariff), low = "red", high = "green", space = "lab", na.value = "white", guide = "colourbar") ggp <- ggp + theme_minimal() ggp <- ggp + theme(axis.title=element_blank(), axis.ticks=element_blank(), axis.text=element_blank()) ggp
so far manage plot map, wrong data mapping. mean state positive tariff schleswig-holstein should green red , bavaria should red green.
my guess there problem fortify function. data 16 rows after fortify print 1000+ rows. why?? , causing mismatching of data. did search on internet possibly can solution. appreciate if can give me answer why problem occurring.
thank in advance!
fortify
puts polygons shapefile ggplot
can plot, hence 1,000+ rows. while can attach values fortified polygons, it's not necessary.
so, don't have go through trouble choropleth. take @ following. added in bits show values mapped rs
:
library(rgdal) library(ggplot2) library(gridextra) egg <- read.table(text="rs tariff 01 -5.25 02 7.16 03 6.65 04 3.10 05 3.69 06 2.49 07 1.89 08 3.93 09 -5.84 10 -2.61 11 -0.21 12 2.35 13 -5.94 14 -7.54 15 -3.27 16 -8.75", header=true, colclasses=c("character", "numeric")) bundesl <- readogr("vg2500_geo84/vg2500_bld.shp", "vg2500_bld") bundesl@data<- bundesl@data[order(bundesl$rs, na.last=na),] # projection germany if intende draw additional # lines or points you'll have project them before plotting # may more trouble it's worth , can use # coord_map("mollweide") or else works besides mercator bundesl <- sptransform(bundesl, crs("+proj=utm +zone=33 +ellps=wgs84 +datum=wgs84 +units=m +no_defs ")) bundesl_map <- fortify(bundesl, region="rs") # doing bit plot rs # @ center of each polygon # totally not necessary choropleth egg <- cbind(egg, data.frame(gcentroid(bundesl, byid=true))) gg <- ggplot() # bit ensures have outlines gg <- gg + geom_map(data=bundesl_map, map=bundesl_map, aes(x=long, y=lat, map_id=id), color="#7f7f7f", size=0.15) # bit here choropleth gg <- gg + geom_map(data=egg, map=bundesl_map, aes(fill=tariff, map_id=rs), color="#7f7f7f", size=0.15) gg <- gg + geom_text(data=egg, aes(x=x, y=y, label=rs), size=3) gg <- gg + coord_equal() # projected gg <- gg + scale_fill_continuous(name=expression(tariff), low="red", high="green", space="lab", na.value="white", guide="colourbar") gg <- gg + labs(x=null, y=null) # decent map theme gg <- gg + theme_bw() gg <- gg + theme(panel.grid=element_blank()) gg <- gg + theme(panel.border=element_blank()) gg <- gg + theme(axis.ticks=element_blank()) gg <- gg + theme(axis.text=element_blank()) gt <- tablegrob(cbind(bundesl@data[,c(2,4)], egg[,2])) grid.arrange(gg, gt, ncol=2)
08
& 16
have unicode in them, hence lack of display without conversion. realize plotting of rs
number on centroid problematic berlin & brandenburg, give general idea, not perfect.
i'd highly suggest using cut
define 5 or 6 standardized breaks values vs use continuous scale.
Comments
Post a Comment