I´m trying to produce a worldmap using ggplot2 in R. I want to show GDP per capita from Brazilian States. There are 2 problems:
In the center of Country have two labels aren't easy to read the information. They are overlapping.Reducing the numbers not resolved.
I would like to change the colour breaks and don't know how to access it. I would like to use colors to become easy to note the diferences of GDP.
My data is: https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit
Here is my code:
library(maptools)
gpclibPermit()
library(ggplot2)
library(rgdal)
library(rgeos)
library(ggmap)
# read administrative boundaries (change folder appropriately)
brMap <- readShapePoly("BRASIL.shp")
brMap
# read downloaded data (change folder appropriately)
brRen <- read.csv("Renda.csv", sep = ";", quote = "\"", dec=".", stringsAsFactors = FALSE)
brRen$rendapc <- as.numeric(as.character(brRen$rendapc)) # format as numeric
#convert shp to UTF8
library(descr)
brMap$ESTADO<-toUTF8(brMap$ESTADO, "IBM850")
# convert shp data to data frame for ggplot
as.data.frame(brMap) # para definir a region
brMap = gBuffer(brMap, width=0, byid=TRUE) #correct problem with Polygons - TopologyException
brMapDf <- fortify(brMap, region="UF")
brMapDf
# merge map and data
brRenMapDf <- merge(brMapDf, brRen, by.x="id", by.y="Iden")
brRenMapDf <- brRenMapDf[order(brRenMapDf$order),]
# limit data to main Europe
brazil.limits <- geocode(c("Monte Caburaí", "Barra do Chuí", "Serra do Divisor", "Ilhas Martin Vaz"))
brRenMapDf <- subset(brRenMapDf, long > min(brazil.limits$lon) & long < max(brazil.limits$lon) & lat > min(brazil.limits$lat) & lat < max(brazil.limits$lat))
# ggplot mapping
# data layer
m0 <- ggplot(data=brRenMapDf)
# empty map (only borders)
m1 <- m0 + geom_path(aes(x=long, y=lat, group=group), color='gray') + coord_equal()
m1
# fill with education expenditure data
m2 <- m1 + geom_polygon(aes(x=long, y=lat, group=group, fill=rendapc))
m2
# inverse order (to have visible borders)
m0 <- ggplot(data=brRenMapDf)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=group, fill=rendapc)) + coord_equal()
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group), color='black')
m2
# over a GoogleMap (not working if not correctly projected)
map <- get_map(location = 'Brazil', zoom=4)
m0 <- ggmap(map)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=group, fill=rendapc), data=brRenMapDf, alpha=.9)
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group), data=brRenMapDf, color='black')
m2
# add text
library(doBy)
txtVal <- summaryBy(long + lat + rendapc ~ id, data=brRenMapDf, FUN=mean, keep.names=T)
m3 <- m2 + geom_text(aes(x=long, y=lat, label=rendapc), data=txtVal, col="yellow", hjust=0.5, vjust=0.5, cex=3)
m3
My result I want to improve: