我需要根据我已经解析为R的某些商家的频率来绘制美国各州的地图。
当我绘制时会发生的情况是生成的地图只显示有业务的州(仅约15个),因此没有显示其他州。
这是我的代码:
aux <- latlong2state(data.frame(x=allDataBusiness$longitude,y=allDataBusiness$latitude))
allDataBusiness["region"] <- NA
allDataBusiness$region <- aux
all_states <- map_data("state")
temp<-as.data.frame(table(aux))
colnames(temp)<- c("region", "count")
p <- ggplot(temp, aes(map_id = region)) +
geom_map(aes(fill = count), map = all_states, color ="black") +
expand_limits(x = all_states$long, y = all_states$lat) +
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="white", high="darkred") +
guides(fill = guide_colorbar(barwidth = 20, barheight = .5)) +
ggtitle("Busines Frequency Choropleth")
allDataBusiness
有3列(经度,纬度和ID)。我创建了一个列,其中我将区域与相应的坐标相关联。
之后我创建一个DF来设置这些状态出现次数的频率然后我绘制数据。
我在此link生成的图片。
我知道它必须是如此基本的东西,但我只是在这里转圈。
任何帮助都表示赞赏,并提前致谢。
编辑:这是AllDataBusiness
的输出allBusinessData <-
structure(list(latitude = c(34.13729, 32.89915, 49.41999, 49.42169,
49.11919, 39.28675, 39.28679, 39.95398, 34.96329, 42.43932, 47.66441,
34.99269, 42.44433, 32.87958, 29.71918, 47.65743, 37.86849, 32.87974,
39.95299, 42.38919, 37.43942, 37.43924, 34.19189, 42.72999, 29.79988,
42.73461, 41.82826, 42.37319, 49.45195, 39.95469, 42.27551, 49.11629
), longitude = c(-118.13299, -117.24295, -86.89949, -86.99516,
-88.23939, -97.73632, -97.74489, -75.19538, -118.44193, -76.49249,
-122.39151, -117.79999, -76.51255, -117.23223, -95.49158, -122.31321,
-122.26199, -117.23613, -75.19229, -72.51929, -122.16229, -122.17419,
-117.71499, -73.69159, -95.49298, -73.68918, -71.49929, -71.11769,
-79.93327, -75.29999, -83.73795, -88.22139), region = structure(c(1L,
6L, 3L, 3L, 2L, 10L, 10L, 8L, 1L, 7L, 11L, 1L, 7L, 1L, 10L, 11L,
1L, 1L, 8L, 4L, 1L, 1L, 1L, 7L, 10L, 7L, 9L, 4L, 8L, 8L, 5L,
2L), .Label = c("california", "illinois", "indiana", "massachusetts",
"michigan", "NA", "new york", "pennsylvania", "rhode island",
"texas", "washington"), class = "factor")), .Names = c("latitude",
"longitude", "region"), row.names = c(NA, -32L), class = "data.frame")
答案 0 :(得分:3)
您只需要首先使用geom_map
绘制基础all_states
,然后覆盖已填充的基础{有一个替代方案,您只需填写&#34;缺少&#34;状态temp
数据框)
p <- ggplot(all_states, aes(map_id = region)) +
geom_map(fill="white", map=all_states, color="black") +
geom_map(data=temp, aes(fill = count), map = all_states, color ="black") +
expand_limits(x = all_states$long, y = all_states$lat) +
coord_map(projection="mercator") +
theme(legend.position = "bottom",
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()) +
scale_fill_gradient(low="white", high="darkred") +
guides(fill = guide_colorbar(barwidth = 20, barheight = .5)) +
ggtitle("Busines Frequency Choropleth")
p
我投入了一个coord_map
,以帮助您更轻松地调整大小并保持比例。