将State缩写添加到使用ggplot2生成的map

时间:2013-10-17 21:31:56

标签: r ggplot2

我正在尝试将状态缩写添加到使用ggplot2生成的美国地图,并且遇到一些困难。我相信“fill =”选项会导致它,但我不确定。

下面我提供我正在使用的代码。最初,我按照我想要的方式生成地图,除了州名。接下来,我尝试在同一个地图上叠加州缩写。

不幸的是,这对我不起作用。如果我从第一张地图中注释掉“fill =”选项,我可以生成一个带有州缩写的地图。但该地图并未显示我打算展示的内容。我尝试了几种方法。我暂时只在代码中留下一个选项。

要添加州缩写,我正在遵循我在本论坛中阅读的一些建议。特别是,我试图按照2012年2月25日题为“地图上的ggplot居中名称”的讨论提出建议。

如果我能将州缩写添加/叠加到第一张地图,我将不胜感激。

# Master US location data
states <- map_data("state")

# Read in the data
rate <- read.csv("~/R/HealthCare/Data/Test_data.csv")

names(rate) <- tolower(names(rate))
rate$numer <- as.factor(rate$numer)
rate$region <- tolower(rate$statename)


# Create data for US mapping
tomap <- merge(states, rate, sort = FALSE, by = "region")
tomap <- tomap[order(tomap$order), ]


  ## US Map

 # 1. Target Map (w/o state abbr)
p <- qplot(long, lat, data = tomap, 
            group = group, 
            fill = numer,
            geom = "polygon")

p + scale_fill_brewer(palette = "Greens",
                    guide = guide_legend(reverse = TRUE),
                    labels = c("1st cat", "2nd cat", 
                               "3rd cat", "4th cat")) 



  # 2. Add State Abbreviations to Target Map
stannote <- aggregate(cbind(long, lat, group, numer) ~ stateabbr, data = tomap, 
                      FUN=function(x)mean(range(x)))


q <-  qplot(long, lat, data = tomap, 
            group = group,
            #fill = numer,
            fill = "red", #testing
            geom = "polygon") +
  geom_text(data=stannote, aes(long, lat, label = stateabbr), color = "blue", size=3) +
  coord_map()

q

示例数据文件如下所示 -

StateName,StateAbbr,Numer
Alabama,AL,0
Alaska,AK,0
Arizona,AZ,0
Arkansas,AR,0
California,CA,0
Colorado,CO,0
Connecticut,CT,0
Delaware,DE,0
District of Columbia,DC,1
Florida,FL,0
Georgia,GA,0
Hawaii,HI,0
Idaho,ID,1
Illinois,IL,0
Indiana,IN,0
Iowa,IA,1
Kansas,KS,0
Kentucky,KY,1
Louisiana,LA,1
Maine,ME,2
Maryland,MD,0
Massachusetts,MA,2
Michigan,MI,0
Minnesota,MN,1
Mississippi,MS,0
Missouri,MO,0
Montana,MT,0
Nebraska,NE,0
Nevada,NV,1
New Hampshire,NH,1
New Jersey,NJ,2
New Mexico,NM,1
New York,NY,3
North Carolina,NC,0
North Dakota,ND,1
Ohio,OH,0
Oklahoma,OK,0
Oregon,OR,2
Pennsylvania,PA,0
Rhode Island,RI,0
South Carolina,SC,0
South Dakota,SD,1
Tennessee,TN,0
Texas,TX,0
Utah,UT,1
Vermont,VT,2
Virginia,VA,0
Washington,WA,2
West Virginia,WV,0
Wisconsin,WI,0
Wyoming,WY,0

1 个答案:

答案 0 :(得分:1)

正如R经常发生的那样,事实证明错误信息正在告诉你究竟发生了什么(只需要一段时间才能弄明白)。您的第二个数据集numer中的stannote变量是连续的(请使用str(stannote)检查结构以查看此内容)。因此,您可以将该变量更改为一个因子。但请注意:当您使用cbind汇总时,我认为您强制将该因子转换为数字变量,因此numer中的stannote从1-4而不是0 3。

选项1:

stannote$numer = factor(stannote$numer, labels = c(0, 1, 2, 3))
qplot(long, lat, data = tomap, 
            group = group,
            fill = numer, #testing
            geom = "polygon") +
  geom_text(data=stannote, aes(long, lat, label = stateabbr),
                    color = "blue", size=3) + scale_fill_brewer(palette = "Greens")

或者,您可以使用fill从调用geom_text中移除您为整体情节设置的fill = NULL美学。实际上,您不需要填充文本,只需要填充多边形。如果您使用的是ggplot而不是qplot,则可以设置geom_polygon的填充美学。

选项2:

stannote$numer = as.numeric(stannote$numer)
qplot(long, lat, data = tomap, 
            group = group,
            fill = numer, #testing
            geom = "polygon") +
  geom_text(data=stannote, aes(long, lat, label = stateabbr, fill = NULL),
                    color = "blue", size=3) + scale_fill_brewer(palette = "Greens")