ggplot美国国家地图;颜色很好,多边形锯齿 - r [英] ggplot US state map; colors are fine, polygons jagged - r

查看:536
本文介绍了ggplot美国国家地图;颜色很好,多边形锯齿 - r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一张美国地图,其中每个国家都有它的计数。我已经得到了阴影,工作得很好。然而,我遇到的问题是多边形看起来非常粗糙(我假设当我试图将map_data('state')与我每个状态的计数数据帧合并时发生了一些事情)。我合并前的数据框有49行(内华达州在我的设置中缺少数据),并且在合并之后有更多的行(预期用于状态的长/宽项目),但数据看起来似乎被正确复制用于每个经纬度对,所以我不确定为什么poly的锯齿状。



代码:

  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())

任何帮助都将不胜感激(显然,如果有更好的方法来做到这一点,我愿意接受建议!)。 p>

解决方案

您不需要进行合并。您可以使用 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,$ b $ 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


I'm trying to plot a US map where each state is shaded by the count that it has. I've gotten the shading to work just fine. The problem I'm running into, however, is that the polygons look very jagged (I'm assuming something happened when I tried to merge the map_data('state') with my data frame of counts per state). My data frame before merging has 49 rows (Nevada was missing data in my set), and after merging has many more rows (expected for the long/lat items for the states) but the data appears to be copied correctly for each lat/long pair, so I'm unsure why the poly's are so jagged.

Code:

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())

Any help would be greatly appreciated (and obviously, if there is a better way to do it, I'm open to suggestions!).

解决方案

You don't need to do the merge. You can use geom_map and keep the data separate from the shapes. Here's an example using the built-in USArrests data (reformatted with 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

这篇关于ggplot美国国家地图;颜色很好,多边形锯齿 - r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆