ggplot不会填充美国的地图

时间:2016-10-19 15:49:31

标签: r ggplot2 rstudio heatmap

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

这是我的代码:     #read在我的数据中     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

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

如果您尝试使用此数据生成等值区域图,则使用choroplethr非常容易。首先,您需要将州列重命名为“region”,并将列计为“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

enter image description here

答案 1 :(得分:1)

以下代码将使用&quot; rawdata&#39;中的坐标和文字加载并绘制美国地图:

# 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