Hello,

In brief: I want to labels no overlapping.


I´m trying to produce a worldmap using ggplot2 in R. I want to show GDP per capita from Brazilian States, but the income per capita labels in some states are overlapping. For example, in the center of country have two labels overlapping and others states the label are close the state's frontier.


My data is: <https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit>


My result is: <https://drive.google.com/?tab=wo&authuser=0#folders/0B_coFit6AovfRHJDM0JtY1F1QVE>


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 Brazil
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



Thank you for taking the time to read this and for any help you may offer.
 

Sheila
 		 	   		  
	[[alternative HTML version deleted]]

