R中的地理热图

时间:2011-10-13 00:04:53

标签: r map

我想在R中创建美国地图,其中状态颜色基于度量标准以热图类型的方式编码。我知道如何使用googleVis api做到这一点,但我不能使用代码,没有翻转它不是那么好。完成这项工作的最快方法是什么?我熟悉地图包,但我无法让颜色合作。我相信这被称为等值地图。

4 个答案:

答案 0 :(得分:8)

ggplot2包中有一个完整的示例,请参阅?map_data

library(ggplot2)
example(map_data)

答案 1 :(得分:1)

(希望回答可能对某人有帮助)

RevolutionAnalytics使用spplot()功能可以很好地example地图可视化。这是从那里的图像:

enter image description here

答案 2 :(得分:0)

UScensus2000跟踪包中有一个以人口为导向的等值线示例。

答案 3 :(得分:0)

enter image description here

代码

# Transform dataset to correct format
crimes <- data.frame(state = tolower(row.names(USArrests)), USArrests)
crimes

#  Map data   
#  install.packages("maps") remember to install these two packages if you  
#  install.packages("mapproj") do not have them yet

library(mapproj)
library(maps)

states_map <- map_data("state")
states_map

# Merge datasets together 
crime_map <- merge(states_map, crimes, by.x = "region", by.y = "state")

# After merging, the order has changed, which leads to polygons drawn 
# in the incorrect order. Let's sort it 
crime_map

library(dplyr) # for arrange() function 

# Sort by group, then order 
crime_map <- arrange(crime_map, group, order)
crime_map

# Now data can be plotted
library(ggplot2)

plot1 <- ggplot(crime_map, aes(x = long, y = lat, group = group, fill = Assault)) + 
                    geom_polygon(colour = "black") + 
                    coord_map("polyconic")

plot1

# Add title 
plot1 <- plot1 + 
                ggtitle("                       Proportion of crimes in the USA")

plot1

# Improve on colours 
plot1 <- plot1 + 
    scale_fill_gradient2(low = "#559999", mid = "grey", high = "#BB650B",
                                                    midpoint = median(crimes$Assault))
plot1

# If you want white transparent backgroud only
plot1 <- plot1 + 
         theme_void()

plot1

# Note: if RStudio gives you this error when ploducing plot, then use this and try 
# again
devAskNewPage(ask = FALSE)

# Special thanks to Winston Chang and other ggplot developers at RStudio who made made 
#  many of these codes