gpt4 book ai didi

r - ggplot2 在同一张 map 上以一种颜色绘制县边界,以另一种颜色绘制州边界

转载 作者:行者123 更新时间:2023-12-02 03:42:32 24 4
gpt4 key购买 nike

我正在创建带有灰色边框的分区统计县 map ,并且我还想将州边界显示为黑色。有谁知道我如何在现有的县 map 上添加第二层州 map ?

这是data set和我最终使用的代码:

#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 添加到您的代码中...

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

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

enter image description here

关于r - ggplot2 在同一张 map 上以一种颜色绘制县边界,以另一种颜色绘制州边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com