我已经使用EM算法根据一些经济变量对加利福尼亚州的县进行了聚类。现在,我想生成一张带有各县的加利福尼亚地图,从而使一个群集中的各县具有相同的颜色。总共有2个簇,在datafit $ classification列中以1或2为特征。 不幸的是,在按各自的群集对县进行映射时,存在一个我无法解决的错误,即:错误:美学的长度必须为1或与数据相同(2977):fill
我已经看到类似的问题,但是我找不到适合我的解决方案。我没有遗漏我的数据或拼错了一些内容。
#this generates a map of the US
states <- map_data("state")
ca_df <- subset(states, region == "california")
counties <- map_data("county")
ca_county <- subset(counties, region == "california")
ca_base <- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")
ca_base + theme_nothing() +
geom_polygon(data = ca_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA)
#merge map data with my clustered data(called newdata)
cacopa <- inner_join(ca_county, newdata, by = "subregion")
ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)
final_map <- ca_base +
geom_polygon(data = cacopa, aes(fill = datafit$classification), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +
ditch_the_axes
final_map
我希望各县的颜色取决于datafit $ classification的值,但这还行不通。关于如何使其工作有任何建议吗?