R 如何在ggplot上添加透明的美国州地图?
我正在使用ggplot创建一张2004年美国各州及其药物中毒率的地图。我想在这张地图的上方添加一张透明地图,显示美国各州的黑色边界(而不是县…填写县后,我正在寻找一种在各州之间创建不同边界的方法)。这是到目前为止我的代码。问题从最后几行开始,以##为标记: 但我得到一个错误,说 as_映射程序(.f,…)中出错:缺少参数“.f”,没有默认值 你好,OP-?了解如何使用R 如何在ggplot上添加透明的美国州地图?,r,dictionary,ggplot2,border,R,Dictionary,Ggplot2,Border,我正在使用ggplot创建一张2004年美国各州及其药物中毒率的地图。我想在这张地图的上方添加一张透明地图,显示美国各州的黑色边界(而不是县…填写县后,我正在寻找一种在各州之间创建不同边界的方法)。这是到目前为止我的代码。问题从最后几行开始,以##为标记: 但我得到一个错误,说 as_映射程序(.f,…)中出错:缺少参数“.f”,没有默认值 你好,OP-?了解如何使用ggplot绘制地图非常有帮助map()是一个基本的R命令,因此不能直接使用ggplot。出现错误的原因是maps::map()已
ggplot
绘制地图非常有帮助map()
是一个基本的R命令,因此不能直接使用ggplot。出现错误的原因是maps::map()
已被purr::map
-tidyverse的一部分掩盖case\u when
可能比您的长ifelse部分更好(可能是更好的解决方案)Hi OP-?了解如何使用ggplot
绘制地图非常有帮助map()
是一个基本的R命令,因此不能直接使用ggplot。出现错误的原因是maps::map()
已被purr::map
-tidyverse的一部分掩盖case\u when
可能比长的ifelse部分更好(可能是更好的解决方案)
library(readr)
drugs <- read_csv("drugs.csv")
View(drugs)
summary(drugs)
library(maps)
library(mapdata)
library(ggplot2)
library(tidyverse)
library(ggthemes)
library(mapproj)
data(county.fips)
colnames(drugs)[7] <- "deaths"
sort(table(drugs$deaths))
drugs$death_rate <- 0
drugs$death_rate <- ifelse(drugs$deaths == "<2", 1,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "2-3.9", 2,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "4-5.9", 3,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "6-7.9", 4,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "8-9.9", 5,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "10-11.9", 6,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "12-13.9", 7,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "14-15.9", 8,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "16-17.9", 9,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "18-19.9", 10,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "20-21.9", 11,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "22-23.9", 12,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "24-25.9", 13,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "26-27.9", 14,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "28-29.9", 15,
drugs$death_rate)
drugs$death_rate <- ifelse(drugs$deaths == "30+", 16,
drugs$death_rate)
drugdeaths <- merge(drugs, county.fips, by.x = "FIPS", by.y = "fips")
counties <- map_data("county")
head(counties)
ggcounties <- mutate(counties, polyname = paste(region, subregion, sep = ","))
head(ggcounties)
county_coords <- left_join(ggcounties, drugdeaths, by = "polyname")
head(county_coords)
ggmap <- county_coords[order(county_coords$death_rate), ]
ggplot(ggmap, aes(x=long, y=lat, group=group, fill=death_rate)) +
geom_polygon() + coord_map() + theme_map()
##PROBLEM STARTS BELOW:
drugdeaths2004 <- subset(ggmap, ggmap$Year=="2004")
ggplot(drugdeaths2004, aes(x=long, y=lat, group=group, fill=death_rate)) +
geom_polygon() + coord_map() + theme_map() +
scale_fill_continuous(guide = guide_legend()) + labs(fill =
"Death Rate
Per 100k People") +
scale_fill_distiller(palette = "Purples", direction = 1)
map("state", col = "black", fill = FALSE, resolution = 0, lty = 0, projection = "mercator", add = TRUE)