我正在尝试根据相关因素突出显示地图上的区域,如下所示:
我最初的问题是,由于边界重叠,区域会显示不正确,如下所示:
我通过黑客子化数据来解决这个问题,这导致了第一张图片。
#First Map With No Legend
p <- ggplot() +
geom_polygon(data = OpportunityAreas.df[OpportunityAreas.df$Opp_Area == 'Yes',],
aes(long,lat,group=group), fill = "#d2eef7", color = 'black') +
geom_polygon(data = OpportunityAreas.df[OpportunityAreas.df$Opp_Area == 'No',],
aes(long,lat,group=group), fill = "#d2eef7", color = 'orange') +
coord_map()
但现在我没有传说,也只是一个糟糕的解决方案。是否有通过相关因子为多边形边界着色的正确方法?
#Second Map With Improper Boundaries
p.improperly.drawn <- ggplot() +
geom_polygon(data = OpportunityAreas.df,
aes(long,lat,group=group, color = Opp_Area), fill = "#d2eef7") +
coord_map()
答案 0 :(得分:1)
您的第一个解决方案只需要进行一些小调整即可获得所需内容:将color
调用中的geom_polygon
个参数移至mapping = aes()
来电。对于第一个电话,mapping = aes(color = 'black')
和第二个geom_polygon
来电使用mapping = aes(color = 'orange')
。
我无法访问您示例中的OpportunityAreas.df
,因此我将在下面的示例中使用一些通用映射数据。
library(ggplot2)
library(mapdata)
counties <- map_data("county")
ca_county <- subset(counties, region == "california")
ca_county$color <- cut(ca_county$group, 2, labels = c("A", "B"))
ggplot() +
aes(x = long, y = lat, group = group) +
geom_polygon(data = subset(ca_county, color == "A"),
mapping = aes(color = "A"),
fill = NA) +
geom_polygon(data = subset(ca_county, color == "B"),
mapping = aes(color = "B"),
fill = NA)