ggplot 美国州地图;颜色很好,多边形呈锯齿状 - r

ggplot US state map; colors are fine, polygons jagged - r

我正在尝试绘制一张美国地图,其中每个州都用它拥有的计数来表示。我已经让阴影工作得很好。然而,我 运行 遇到的问题是多边形看起来参差不齐(我假设在我尝试将 map_data('state') 与我的数据框合并时发生了什么每个州的计数)。合并前我的数据框有 49 行(内华达州在我的集合中缺少数据),合并后有更多行(各州的 long/lat 项预期)但数据似乎已正确复制每个 lat/long 对,所以我不确定为什么多边形如此参差不齐。

代码:

ggplot() +
  geom_polygon(data=try1, aes(x=long, y=lat, group = group, fill= COUNT)) +
  scale_fill_continuous(low='thistle2', high='darkred', guide='colorbar') +
  theme_bw() + labs(fill="State Map Try Title1", title='Title2', x='', y='') +
  scale_y_continuous(breaks=c()) +
  scale_x_continuous(breaks=c()) +
  theme(panel.border = element_blank())

任何帮助将不胜感激(显然,如果有更好的方法,我愿意接受建议!)。

您不需要进行合并。您可以使用 geom_map 并将数据与形状分开。这是一个使用内置 USArrests 数据的示例(使用 dplyr 重新格式化):

library(ggplot2)
library(dplyr)

us <- map_data("state")

arr <- USArrests %>% 
  add_rownames("region") %>% 
  mutate(region=tolower(region))

gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
                    aes(x=long, y=lat, map_id=region),
                    fill="#ffffff", color="#ffffff", size=0.15)
gg <- gg + geom_map(data=arr, map=us,
                    aes(fill=Murder, map_id=region),
                    color="#ffffff", size=0.15)
gg <- gg + scale_fill_continuous(low='thistle2', high='darkred', 
                                 guide='colorbar')
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + coord_map("albers", lat0 = 39, lat1 = 45) 
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.background = element_blank())
gg <- gg + theme(axis.ticks = element_blank())
gg <- gg + theme(axis.text = element_blank())
gg

我在使用ggplot2的时候遇到了类似的问题。作为@hrbrmstr 's solution, I found reloading the ggplot2 package (as well as rgdal/`maptools') 的替代方案,我解决了这个问题。