ggplot2在同一张地图上用一种颜色映射县界,用另一种颜色映射州界 [英] ggplot2 mapping county boundries in one color and state boundries in another on the same map

查看:50
本文介绍了ggplot2在同一张地图上用一种颜色映射县界,用另一种颜色映射州界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有灰色边框的Choropleth县地图,并且我还希望将州边界设为黑色.有谁知道我要如何在现有的县地图上添加第二层州地图?

I am creating a choropleth county map with grey borders, and I also want to include the state boundries in black. Does anyone know how I go about adding a second layer of state mapping to an existing county map?

这是

Here's the data set and code I ended up using:

#load libraries
    library(ggplot2)
    library(ggmap)
    library(maps)
    library(plyr)

#get wif file
wip <- read.csv("wip.csv") 

#get map data for US counties and states
county_map <- map_data("county")
state_map <- map_data("state")

#merge wip and county_map
wip_map <- merge(county_map, wip, by.x=c("region", "subregion"), 
    by.y=c("region","subregion"), all.x=TRUE)

#resort merged data
wip_map <- arrange(wip_map, group, order)

#relpace NA with 0's
wip_map[is.na(wip_map)] <- 0

#generate a disctrete color pallette    
pal <- c("#F7FCF5","#74C476","#41AB5D","#238B45","#006D2C","#00441B")


theme_clean <- function(base_size = 12) {
    require(grid)
    theme_grey(base_size) %+replace%
    theme(
        axis.title      =   element_blank(),
        axis.text       =   element_blank(),
        panel.background    =   element_blank(),
        panel.grid      =   element_blank(),
        axis.ticks.length   =   unit(0,"cm"),
        axis.ticks.margin   =   unit(0,"cm"),
        panel.margin    =   unit(0,"lines"),
        plot.margin     =   unit(c(0,0,0,0),"lines"),
        complete = TRUE
        )
    }

final_map <- ggplot(wip_map, aes(x=long, y=lat, group=group, fill=factor(CATEGORY))) +
            geom_polygon(colour="grey", aes(fill=factor(CATEGORY))) +
            scale_fill_manual(values=pal) +
            expand_limits(x = wip_map$long, y = wip_map$lat) +
            coord_map("polyconic") + 
            labs(fill="Number Per\nCounty") + 
                theme_clean()

 final_map + geom_path( data = state_map , colour = "red")

谢谢!

推荐答案

只需在您的代码中添加 geom_path ...

Just add a geom_path to your code...

我用红色突出显示了边界,但您可以轻松地将其设置为黑色.

I used red to highlight the boundaries but you can easily just set it to black.

    ggplot( wip_map, aes( x = long , y = lat , group=group ) ) +
        geom_polygon( colour = "grey" , aes( fill = factor( CATEGORY ) ) ) +
        scale_fill_manual( values = pal ) +
        expand_limits( x = wip_map$long, y = wip_map$lat ) +
        coord_map( "polyconic" ) + 
        labs(fill="Number Per\nCounty") + 
        theme_clean( ) +
        geom_path( data = state_map , colour = "red")

这篇关于ggplot2在同一张地图上用一种颜色映射县界,用另一种颜色映射州界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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