我正在尝试使用ggplot2制作地图。点图可以正常工作,但Choropleth图只是空白。
library(broom)
library(rgdal)
library(ggplot2)
philly_df <- tidy(philly)
head(philly_df)
philly$polyID <- sapply(slot(philly, "polygons"), function(x) slot(x, "ID"))
philly_df <- merge(philly_df, philly, by.x = "id", by.y="polyID")
head(philly_df)
# because my polygon number grows from 72 to up to 6000 with repeated entries,
# use distinct to have them back to 72
distinct(philly_df, id, .keep_all = TRUE)
# Generate the data to plot
philly_df <- merge(philly_df, Death_U5, by.x = "DIS_NAME", by.y="District")
head(philly_df)
# when I plot the map, it gives error:
# "Error: Don't know how to add o to a plot"
# and shows only title and legend but no map
ggplot() +
geom_polygon(data = philly_df,
aes(x = long, y = lat, group = group, fill = cut_number(deathU5_Q1, 5))) +
scale_fill_brewer("deaths", palette = "OrRd") +
ggtitle("under 5 malaria deaths for Q1 Philly") +
theme(line = element_blank(),
axis.text=element_blank(),
axis.title=element_blank(),
panel.background = element_blank())
coord_equal()