gpt4 book ai didi

r - 在传单 map 上注释数字变量

转载 作者:行者123 更新时间:2023-12-01 02:08:08 25 4
gpt4 key购买 nike

我做了这个情节

enter image description here

使用以下代码。

library(XML)
library(ggplot2)
library(scales)
library(plyr)
library(maps)

unemp <-
readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
colClasses = c('character', 'character', 'numeric'))[[2]]

names(unemp) <- c('rank', 'region', 'rate')
unemp$region <- tolower(unemp$region)

us_state_map <- map_data('state')
map_data <- merge(unemp, us_state_map, by = 'region')

map_data <- arrange(map_data, order)

states <- data.frame(state.center, state.abb)

p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = cut_number(rate, 5)))
p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
p1 <- p1 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette = 'Set1')
p1 <- p1 + coord_map()
p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p1 <- p1 + theme_bw()
p1

现在我想用 leaflet 重现相同的情节 R包裹。
library(leaflet)

leaflet(data = map_data) %>%
setView(lng = -77.0167, lat = 38.8833, zoom = 4) %>%
addTiles()

我如何注释 rate来自 map_data map 上的数据框 leafletgeom_polygon做在 ggplot2版本?

最佳答案

也许这里有一种方法作为起点:

mapStates = map("state", fill = TRUE, plot = FALSE)
rates <- cut_number(unemp$rate[match(sub("(.*?):.*", "\\1", mapStates$names), unemp$region)], 5)

leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = brewer_pal(palette = "Set1")(8)[as.numeric(rates)], stroke = FALSE) %>%
addLegend(colors = brewer_pal(palette = "Set1")(nlevels(rates)), labels = levels(rates), opacity = .2)

enter image description here

添加:

关于你的另一个问题:
library(raster)
pakistan.adm2.spdf <- getData("GADM", country = "Pakistan", level = 2)
rates <- cut_number(unemployment.df$unemployment[match(pakistan.adm2.spdf@data$NAME_2, unemployment.df$id)], 5)
leaflet(pakistan.adm2.spdf) %>% addTiles() %>%
addPolygons(fillColor = brewer_pal(palette = "PuRd")(nlevels(rates))[as.numeric(rates)], stroke = FALSE, fillOpacity = .6) %>%
addLegend(colors = brewer_pal(palette = "PuRd")(nlevels(rates)), labels = levels(rates), opacity = .6) %>%
setView(lng = 69.374268, lat = 30.028617, zoom = 5)

enter image description here

关于r - 在传单 map 上注释数字变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096907/

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