【问题标题】:ggplot won't populate with a map of the united statesggplot 不会填充美国地图
【发布时间】:2016-10-19 15:49:31
【问题描述】:

我正在尝试制作美国的热图,代码“有效”,但数据不会填充到美国地图中 - 只会显示州的名称 - 我也想填充,但是美国的实际地图是最重要的。

这是我的代码: #读入我的数据 rawdata_path

# Import the data into RStudio:
rawdata <- readxl::read_excel(rawdata_path, sheet = 1, col_names = TRUE)

#clean up the data
rawdata$Lattitude <- as.numeric(rawdata$Lattitude)
rawdata$Longitude <- as.numeric(rawdata$Longitude)

#plot the data
library(ggplot2)
library(maps)
ggplot(rawdata, aes(x=Longitude, y = Lattitude, group = rawdata$State))+
  geom_polygon(aes(fill=FinalCount))+
  geom_path()+
  geom_text(data = rawdata, aes(x=Longitude, y = Lattitude, label = State))+
  scale_fill_gradientn(colours = rev(heat.colors(10)), na.value = "grey90")+
  coord_map()

这就是数据框头部所称的 rawdata 的样子,我希望通过 Count 列填充状态 - 1 最亮,10 为深红色:

 State        Count Group Lattitude  Longitude       Rev
 <chr>        <dbl> <dbl>     <dbl>      <dbl>     <dbl>
arizona        1.0     1  33.50000 -112.05000       0.0
arkansas       1.0     2  36.36000  -94.20000       0.0
georgia        1.0     3  33.82000  -84.32000       0.0
hawaii         1.0     4  21.30000 -157.85000       0.0
kansas         1.0     5  38.97167  -95.23525       0.0
maryland       1.0     6  38.98000  -77.08000       0.0
missouri       1.0     7  39.09000  -94.58000       0.0
oregon         1.0     8  45.51000 -122.68000       0.0
pennsylvania   1.0     9  40.43000  -79.97000       0.0
rhode island   1.0    10  41.82000  -71.41000       0.0
tennessee      1.0    11  35.10000  -90.00000       0.0
texas          1.0    12  29.76043  -95.36980       0.0
louisiana      2.1    13  30.44000  -91.12000  209250.0
indiana        2.3    14  38.30000  -85.72000  231605.9
oklahoma       2.7    15  35.22000  -97.34000  274377.9
michigan       3.8    16  42.73000  -84.48000  381528.5
florida        4.9    17  30.43826  -84.28073  498338.5
california     5.1    18  34.06000 -118.24000  511472.0
illinois       5.3    19  41.83000  -87.68000  537913.5
kentucky       5.7    20  38.22000  -85.74000  562077.0
new york       6.3    21  40.75000  -73.99000  630642.8
massachusetts  9.5    22  42.33038  -71.16619  908952.0
north carolina 10.0   23  36.07000  -79.82000 1571923.8

谁能帮我解决这个问题?

【问题讨论】:

    标签: r ggplot2 rstudio heatmap


    【解决方案1】:

    如果您尝试使用此数据生成等值线图,使用choroplethr 非常容易。首先,您需要将您的 states 列重命名为“region”并将 count 列重命名为“value”

    所以输入数据看起来像:

    df <- structure(list(region = structure(c(1L, 2L, 5L, 6L, 9L, 12L, 
    15L, 19L, 20L, 21L, 22L, 23L, 11L, 8L, 18L, 14L, 4L, 3L, 7L, 
    10L, 16L, 13L, 17L), .Label = c("arizona", "arkansas", "california", 
    "florida", "georgia", "hawaii", "illinois", "indiana", "kansas", 
    "kentucky", "louisiana", "maryland", "massachusetts", "michigan", 
    "missouri", "new york", "north carolina", "oklahoma", "oregon", 
    "pennsylvania", "rhode island", "tennessee", "texas"), class = "factor"), 
    value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.1, 2.3, 2.7, 
    3.8, 4.9, 5.1, 5.3, 5.7, 6.3, 9.5, 10), Group = 1:23), .Names = c("region", 
    "value", "Group"), class = "data.frame", row.names = c(NA, -23L
    ))
    

    脚本很简单:

    library(ggplot2)
    library(choroplethr)
    
    choro <- state_choropleth(df) + scale_fill_brewer(palette = "Reds")
    choro
    

    【讨论】:

    • 这太棒了,正是我想要的!太感谢了!我肯定会更多地使用这个包。
    • 一个问题 - 我不明白数据框结构部分中的“L”来自哪里。您是如何获得这些数字的?为什么它们的顺序是这样的?
    • @DanaOrinick 这是dput 函数的输出,我试图复制您发布的数据的重要部分。 L 后缀表示整数。出于脚本的目的,它们并不真正相关。您只需更改列名并确保状态为小写。见:cran.r-project.org/web/packages/choroplethr/vignettes/…
    【解决方案2】:

    以下代码将使用“原始数据”中的坐标和文本加载并绘制美国地图:

    # Load the data
    rawdata_path <- 'C:/data.xlsx'
    rawdata <- readxl::read_excel(rawdata_path, sheet = 1, col_names = TRUE)
    
    #clean up the data
    rawdata$Lattitude <- as.numeric(rawdata$Lattitude)
    rawdata$Longitude <- as.numeric(rawdata$Longitude)
    
    library(ggplot2)
    library(maps)
    # Load the map of the United State
    all_states <- map_data("state")
    
    ggplot() +
      geom_polygon( data=all_states, aes(x=long, y=lat, group = group), 
                colour="white", fill="blue" ) +
      geom_point(data=rawdata,
             aes(x=Longitude, y=Lattitude, colour='red', size=Count),
             alpha=I(0.5)) +
      geom_text(data = rawdata, aes(x=Longitude, y = Lattitude, label = State)) +
      scale_fill_gradientn(colours = rev(heat.colors(10)), na.value = "grey90")
    

    运行它会得到这张地图:

    【讨论】:

    • 谢谢!!我使用的数据是上面的数据框。这是我正在阅读的名为 heatdata 的电子表格。
    • 我复制了你的 Excel 文件并更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多