【问题标题】:Geographical heat map in RR中的地理热图
【发布时间】:2011-12-06 13:46:02
【问题描述】:

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

【问题讨论】:

标签: r map


【解决方案1】:

ggplot2 包中有一个完整的例子,见?map_data

library(ggplot2)
example(map_data)

【讨论】:

    【解决方案2】:

    UScensus2000tract 软件包中有一个人口驱动的 cloropleth 示例。

    【讨论】:

      【解决方案3】:

      (希望回答对某人仍然有帮助)

      RevolutionAnalytics 使用spplot() 函数具有出色的地图可视化example。这是那里的图片:

      【讨论】:

        【解决方案4】:

        代码

        # 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 
        

        【讨论】:

          猜你喜欢
          • 2020-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-23
          • 2013-05-01
          相关资源
          最近更新 更多