5

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:

  1. In the center of Country have two labels aren't easy to read the information. They are overlapping.Reducing the numbers not resolved.

  2. 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:

enter image description here

4

1 回答 1

5

关于改变色标,ggplot2 有一个统一的处理比例的方法。您正在寻找的功能始终具有格式scale_{which_scale}_{scale_type},美学在哪里(例如,which_scale您在或中使用的内容),并且可以是连续的或离散的等。如果您想调整比例,您正在寻找的是, ,或 。查看这些功能的文档。aes()fillsizescale_typefillscale_fill_continuousscale_fill_gradientscale_fill_gradient2

代码示例:

m2 <- m1 + geom_polygon(aes(x=long, y=lat, group=group, fill=rendapc))
m2 + scale_fill_gradient(low = "blue", high = "red")
于 2013-03-09T21:42:27.063 回答