Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绘制宾夕法尼亚州R_R_Ggplot2 - Fatal编程技术网

绘制宾夕法尼亚州R

绘制宾夕法尼亚州R,r,ggplot2,R,Ggplot2,我有一个宾夕法尼亚州县(67个)的数据集和每个县的频率。现在,我想创建一个带有频率的热图。我正在使用R的地图库: require(maps) m<-map("county","Pennsylvania") 如何将其转换为彩色choropleth频率热图?库(tidyverse) library(tidyverse) library(readr) library(maps) LicenseListWithSecondaries <- read_csv("C:/Users/..../D

我有一个宾夕法尼亚州县(67个)的数据集和每个县的频率。现在,我想创建一个带有频率的热图。我正在使用R的地图库:

require(maps)
m<-map("county","Pennsylvania")
如何将其转换为彩色choropleth频率热图?

库(tidyverse)
library(tidyverse)
library(readr)
library(maps)
LicenseListWithSecondaries <- read_csv("C:/Users/..../Downloads/LicenseListWithSecondaries.csv")

License_final <- LicenseListWithSecondaries %>% 
                        filter(Status == 'Active') %>% 
                        mutate(County_join = tolower(str_remove_all(County, " County"))) %>% 
                        group_by(County_join) %>% summarise(Frequencies = n())

#m <- map("county","Pennsylvania")

Pennsylvania <- map_data("county","Pennsylvania")

Pennsylvania_final <- inner_join(Pennsylvania, License_final,by=c('subregion' = 'County_join'))

pen_base <- ggplot(data = Pennsylvania_final, mapping = aes(x = long, y = lat, group = subregion)) + 
coord_fixed(1.3) + 
geom_polygon(color = "black", fill = "gray")

ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)

pen_base + 
geom_polygon(aes(fill = Frequencies), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +
ditch_the_axes +
scale_fill_gradientn(colours = rev(rainbow(7)),
               breaks = c(2, 4, 10, 100, 1000, 10000),
               trans = "log10")
图书馆(readr) 图书馆(地图) LicenseListWithSecondary% 突变(县加入=降低(县移除所有(县,县))%>% 分组依据(县加入)%>%总结(频率=n() #m
库(tidyverse)
图书馆(readr)
图书馆(地图)
LicenseListWithSecondary%
突变(县加入=降低(县移除所有(县,县))%>%
分组依据(县加入)%>%总结(频率=n()
#m
require(地图)
要求(sf)
需要(dplyr)
#加载频率数据
df%filter(grepl('.\\b(\\d{5})\\b.*,LID))%>%
突变(名称=gsub(“县”,“县”),%>%
选择(-country)
#查找县频率
频率%
分组单位(名称)%>%
摘要(n=长度(名称))%>%
排列(-n)%>%
过滤器(名称!=“”)
#加入县信息统计频率
res%left\u join(df\u five%>%select(LID,NAME),by=“NAME”)
#定义颜色桶
颜色=c(“#F1EEF6”、“#D4B9DA”、“#C994C7”、“#DF65B0”、“#DD1C77”、“#980043”、“#4c0021”)
res$colorbackes
require(映射)
要求(sf)
需要(dplyr)
#加载频率数据
df%filter(grepl('.\\b(\\d{5})\\b.*,LID))%>%
突变(名称=gsub(“县”,“县”),%>%
选择(-country)
#查找县频率
频率%
分组单位(名称)%>%
摘要(n=长度(名称))%>%
排列(-n)%>%
过滤器(名称!=“”)
#加入县信息统计频率
res%left\u join(df\u five%>%select(LID,NAME),by=“NAME”)
#定义颜色桶
颜色=c(“#F1EEF6”、“#D4B9DA”、“#C994C7”、“#DF65B0”、“#DD1C77”、“#980043”、“#4c0021”)

res$ColorBucket您使用什么数据集?它是一个csv文件,包含宾夕法尼亚州各县的许可证信息。我想创建一个热图,显示拥有最多许可证和最少一个输入(df)数据样本的县,其中df是您的数据样本。但是它有什么帮助呢?你没有提供任何数据,所以我们怎么知道你想在这张地图上绘制什么频率变量。无论如何,你应该提供一些数据。你使用的数据集是什么?它是一个csv文件,包含宾夕法尼亚州各县的许可证信息。我想创建一个热图,显示拥有最多许可证和最少一个输入(df)数据样本的县,其中df是您的数据样本。但是它有什么帮助呢?你没有提供任何数据,所以我们怎么知道你想在这张地图上绘制什么频率变量。无论如何,你应该提供一些数据。哇!伟大的这很有帮助。哇!伟大的这很有帮助。帮了大忙!伟大的帮了大忙!
library(tidyverse)
library(readr)
library(maps)
LicenseListWithSecondaries <- read_csv("C:/Users/..../Downloads/LicenseListWithSecondaries.csv")

License_final <- LicenseListWithSecondaries %>% 
                        filter(Status == 'Active') %>% 
                        mutate(County_join = tolower(str_remove_all(County, " County"))) %>% 
                        group_by(County_join) %>% summarise(Frequencies = n())

#m <- map("county","Pennsylvania")

Pennsylvania <- map_data("county","Pennsylvania")

Pennsylvania_final <- inner_join(Pennsylvania, License_final,by=c('subregion' = 'County_join'))

pen_base <- ggplot(data = Pennsylvania_final, mapping = aes(x = long, y = lat, group = subregion)) + 
coord_fixed(1.3) + 
geom_polygon(color = "black", fill = "gray")

ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)

pen_base + 
geom_polygon(aes(fill = Frequencies), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +
ditch_the_axes +
scale_fill_gradientn(colours = rev(rainbow(7)),
               breaks = c(2, 4, 10, 100, 1000, 10000),
               trans = "log10")
require(maps)
require(sf)
require(dplyr)

# load freq data
df <- read.csv("D:/GoogleDrive/stack/df.csv", sep = ",")

# select administrative divisions with 5 digits code i.e. county level
df_five = df %>% filter(grepl(".*\\b(\\d{5})\\b.*", LID)) %>% 
                mutate(NAME = gsub(" County", "", County)) %>% 
                select(-County)

# find county frequencies
freq <- df_five %>% 
  group_by( NAME) %>% 
  summarise(n = length(NAME)) %>% 
  arrange(-n) %>% 
  filter(NAME != "")

# join county info to count frequency
res <- freq %>% left_join(df_five %>% select(LID, NAME), by="NAME")

# define color buckets
colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043", "#4c0021")
res$colorBuckets <- as.numeric(cut(res$n, c(0, 500, 1000, 1500, 2000, 2500, 3500)))
leg.txt <- c("< 500", "500-1000", "1000-1500", "1500-2000", "2000-2500", "2500-3500")

# draw map
map("county", "Pennsylvania", col = colors, fill = TRUE, resolution = 0,
    lty = 0, projection = "polyconic")
map("state", col = "white", fill = FALSE, add = TRUE, lty = 1, lwd = 0.2,
    projection="polyconic")
legend("topright", leg.txt, horiz = TRUE, fill = colors)