0

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

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

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

为了添加州缩写,我遵循了我在这个论坛上阅读的一些建议。特别是,我正在尝试遵循 2012 年 2 月 25 日题为“ggplot centered names on a map”的讨论中的建议。

对于如何将州缩写添加/覆盖到第一张地图,我将不胜感激。

# 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

示例数据文件如下所示 -</p>

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
4

1 回答 1

1

正如我在使用 R 时经常发生的那样,错误消息告诉你到底发生了什么(只需要一段时间才能弄清楚)。第二个数据集中的numer变量stannote是连续的(检查结构str(stannote)以查看此内容)。因此,您可以将该变量更改为一个因子。但请注意:当您cbind汇总使用时,我认为您强制将因子转换为数字变量,因此numerinstannote从 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_textusing中删除您为整体情节设置的美学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")
于 2013-10-18T15:53:27.600 回答