如何填补 r 中 choropleth 的空白? ggplot2 和地图
How to fill the blank in choropleth in r ? ggplot2 and maps
任何人都可以告诉我如何填写我的等值表中的空白字段?我已经附上了我 created.Thanks 很多的 choropleth 的照片!
我注意到两个表中的州名不匹配,但我无法更改原始数据中的州名。
library(ggplot2)
library(maps)
**#Data looks like this:**
aggrev2015<-data.frame(
statename=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
revenue=runif(50,min=100,max=9000))
state_df <- map_data("state")
colnames(state_df)<-c("long","lat","group","order","statename","subregion")
choropleth <- merge(aggrev2015,state_df, by = "statename")
ggplot(choropleth, aes(long, lat, group = statename)) +
geom_polygon(aes(fill = revenue))
乍一看,您的问题似乎与拥有岛屿的州有关。我不会调试它,而是会跟进@hrbrmstr 关于使用 choroplethr 的建议:
library(choroplethr)
library(choroplethrMaps)
data(df_pop_state)
state_choropleth(df_pop_state)
您还可以查看更多关于使用 choroplethr 包 here, and the package has an email course 创建状态等值线的文档。
对于州等值线,我认为包相对于自己做的主要优势在上面的小插图中得到了证明:它使探索性数据分析变得更加容易。例如,更改比例或缩放就像在函数上设置参数一样简单。该软件包还包括 AK 和 HI 以及使用合理的投影。在 ggplot2 中自己解决这些问题往往会让人头疼。请注意,我是 choroplethr 的作者,所以我是根据经验说的:)
另一件需要指出的事情是,您可能希望将等值线覆盖在参考地图上,例如来自 google 地图的地图。我最近将该功能添加到最新版本的 choroplethr。但我还没有机会更新包小插图来解释如何使用它。要获取默认参考地图,只需设置 reference_map = TRUE
:
data(continental_us_states)
state_choropleth(df_pop_state,
zoom = continental_us_states,
reference_map = TRUE)
如果您想使用不同的参考地图(例如卫星地图),您可以这样做。我只是在上面写了一个blog post。这是我几周前实施参考地图功能后遇到的最常见问题。
任何人都可以告诉我如何填写我的等值表中的空白字段?我已经附上了我 created.Thanks 很多的 choropleth 的照片!
我注意到两个表中的州名不匹配,但我无法更改原始数据中的州名。
library(ggplot2)
library(maps)
**#Data looks like this:**
aggrev2015<-data.frame(
statename=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
revenue=runif(50,min=100,max=9000))
state_df <- map_data("state")
colnames(state_df)<-c("long","lat","group","order","statename","subregion")
choropleth <- merge(aggrev2015,state_df, by = "statename")
ggplot(choropleth, aes(long, lat, group = statename)) +
geom_polygon(aes(fill = revenue))
乍一看,您的问题似乎与拥有岛屿的州有关。我不会调试它,而是会跟进@hrbrmstr 关于使用 choroplethr 的建议:
library(choroplethr)
library(choroplethrMaps)
data(df_pop_state)
state_choropleth(df_pop_state)
您还可以查看更多关于使用 choroplethr 包 here, and the package has an email course 创建状态等值线的文档。
对于州等值线,我认为包相对于自己做的主要优势在上面的小插图中得到了证明:它使探索性数据分析变得更加容易。例如,更改比例或缩放就像在函数上设置参数一样简单。该软件包还包括 AK 和 HI 以及使用合理的投影。在 ggplot2 中自己解决这些问题往往会让人头疼。请注意,我是 choroplethr 的作者,所以我是根据经验说的:)
另一件需要指出的事情是,您可能希望将等值线覆盖在参考地图上,例如来自 google 地图的地图。我最近将该功能添加到最新版本的 choroplethr。但我还没有机会更新包小插图来解释如何使用它。要获取默认参考地图,只需设置 reference_map = TRUE
:
data(continental_us_states)
state_choropleth(df_pop_state,
zoom = continental_us_states,
reference_map = TRUE)
如果您想使用不同的参考地图(例如卫星地图),您可以这样做。我只是在上面写了一个blog post。这是我几周前实施参考地图功能后遇到的最常见问题。