2016-10-19 86 views
1

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

这里是我的代码: #阅读在我的数据 rawdata_path < - C( “〜/ R/heatdata.xlsx”)

# 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 

谁能帮助我在这?

回答

1

下面的代码将加载并绘制地图,美国从“RAWDATA”的坐标和文本:

# 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") 

运行它给出了这样的地图:

enter image description here

+0

谢谢!我使用的数据是上面的数据框。这是我正在阅读的名为heatdata的电子表格。 –

+0

我复制了你的Excel文件并更新了答案。 –

2

如果你正试图用这些数据生成一张等值线地图,这很容易与choroplethr。首先,你需要你的状态列重命名为“区域”和计数列作为“价值”

所以输入数据的样子:

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 

enter image description here

+0

这真是太好了,正是我在找的东西!非常感谢!我一定会利用更多的这个包。 –

+0

有一个问题 - 我不明白“L”来自数据帧的结构部分。你是如何得到这些数字的,他们为什么会这样呢? –

+0

@DanaOrinick这是'dput'函数的输出,我试图复制您发布的数据的重要部分。 L后缀表示一个整数。为了脚本的目的,它们并不真正相关。您只需更改列名称并确保状态为小写。请参阅:https://cran.r-project.org/web/packages/choroplethr/vignettes/b-state-choropleth.html – mpschramm