将数据拟合到R中的映射中

时间:2018-05-25 12:13:04

标签: r maps

首先,我想为我糟糕的英语道歉。我正在努力使我的数据符合巴西的地图,这就是我得到的:

enter image description here

请注意,我的数据从-0.53变为0.73,这些值在图中的颜色最深。我想要一个从-1到1的传奇,同时我的异常值(-0.53 / 0.73)在地图中使用较浅的颜色,而-1.0 / 1.0则使用图例中最暗的颜色。此外,我希望负值为红色,正值为蓝色。我怎么能这样做?

这是我目前正在使用的代码:

# Packages

library(maptools)
library(descr)
library(RColorBrewer)
library(plotrix)

# Reading shape files

estados <- readShapePoly(fn = 'estados_2010.shp')
estados2 <- readShapePoly(fn = 'estados_2010.shp') 

# Adding an order vector in the data frame

estados@data$order <- 1:nrow(estados@data) 

# Data to fit in the map

teste <- c(0.567072774466371,
           -0.144934811865636,
           0.595967865326944,
           0.479904421295722,
           0.251495518688696,
           0.195815073242536,
           0.548223329774688,
           0.463845521774005,
           0.272145251828454,
           0.580334048486482,
           0.649672015127772,
           0.584054575484519,
           0.671373957229477,
           0.436391947145788,
           0.522605768431274,
           0.641067907529059,
           0.570339843565797,
           0.381297082036819,
           0.474587396032187,
           0.462810571821925,
           0.558684078070584,
           -0.197881941278306,
           0.720551728476599,
           0.489189630257170,
           -0.538560893510226,
           0.732280520987057,
           0.555364159479124)

# Adding the data vector in the data frame

estados@data$dados <- teste

# Colours

myPaletteBlue <- brewer.pal(9,'RdBu')
createColors  <- colorRampPalette(myPaletteBlue, bias=3.3)
myColorsBlue  <- createColors(nrow(estados@data))

# Counting the number of colours

numberColors <- length(unique(myColorsBlue))

cuts <- quantile(estados@data$dados,
                 probs=(0:numberColors)/numberColors)

estados@data$dados2 <- cut(estados@data$dados,breaks = cuts,
                           include.lowest=T)

# Merging the data

colorOfdados2 <- data.frame(dados2=levels(estados@data$dados2),
                            colors=unique(myColorsBlue))
estados@data <- merge(estados@data, colorOfdados2)


# Ordering the data frame back to the first order

estados@data <- estados@data[order(estados@data$order),]

# Plot

plot(estados, col=as.character(estados@data$colors),
     lty=0)

plot(estados2, add=T) # Destaca as linhas dos estados no mapa

# Legend

color.legend(xl=-70, xr=-60,yb=-25,yt=-26, 
             legend=c('-0.53','','0.73'), 
             rect.col=myColorsBlue, gradient='x',
             cex=.8, pos=c(1,1,1))

0 个答案:

没有答案