我正尝试生产使用R.我想表明从巴西国人均GDP GGPLOT2一个世界地图。 有2个问题:
在国家的中心有两个标签是不容易读取信息。 他们overlapping.Reducing没有解决的数字。
我想改变颜色休息和不知道如何访问它。 我想用的颜色变得容易注意到GDP的个体差异。
我的数据是: https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit
这里是我的代码:
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
我的结果,我想提高: