Closed ixxmu closed 11 months ago
❝本节通过一个案例来介绍如何使用ggplot2来绘制二维密度地图,整个过程仅参考。希望对各位观众老爷能有所帮助。「数据代码已经整合上传到2023VIP交流群」,加群的观众老爷可自行下载,有需要的朋友可关注文末介绍加入VIP交流群。
❞
❝给予长期支持我们的忠实读者们一个特别待遇:凡是购买过小编2022年或2023年VIP会员文档的朋友们,「将自动获得2024年及以后更新的绘图文档代码,无需额外付费。」目前这两年的会员文档已累计卖出1500+,质量方面各位无需担忧。简要概括就是只要购买任意1年的会员内容,2024及后期公众号所更新的绘图文档均会在已经加入的会员群内分享。
❞
library(tidyverse)
install.packages("mapdata")
install.packages("stopwords")
library(mapdata)
library(ggtext)
library(stopwords)
library(tidytext)
library(ggrepel)
library(tidyverse)
historical_markers <- read_csv('historical_markers.csv')
no_markers <- read_csv('no_markers.csv')
combined <- historical_markers %>%
filter(!(state_or_prov %in% c("Alaska", "Hawaii", "Puerto Rico"))) %>% # 筛选出除阿拉斯加、夏威夷和波多黎各外的州或省
group_by(state_or_prov) %>%
summarize(text = paste(title, collapse = " ")) # 按州或省分组,并将每组的标题合并成一个字符串
combined$words <- str_replace_all(combined$text, "[[:punct:]]", "") # 移除所有的标点符号
state_info <- map_data("state") # 加载美国各州的地图数据
state_labels <- state_info %>%
group_by(region) %>%
summarise(min_long = min(long),max_long = max(long),
min_lat = min(lat),max_lat = max(lat),
range_long = max_long - min_long,
range_lat = max_lat - min_lat,
long = min_long + range_long/2,
lat = min_lat + range_lat/2) %>%
mutate(long= case_when(region %in% c("michigan", "florida") ~ long + 2,region == "idaho" ~ long -1,
region == "virginia" ~ long + 1,RUE ~ long)) %>%
mutate(lat = case_when(region == "maryland" ~ lat + 0.5,TRUE ~ lat)) %>%
select(region, long, lat) %>%
right_join(word_by_state, by = c("region" = "state_or_prov")) # 计算每个州的地理中心位置,并将其与词汇数据合并
historical_markers %>%
filter(!(state_or_prov %in% c("Alaska", "Hawaii", "Puerto Rico"))) %>%
ggplot()+
geom_polygon(aes(x=long, y=lat, group = group), data = state_info, fill = NA, color = "black", linewidth = 0.15)+
coord_fixed(ratio = 1.3) +
geom_density2d_filled(aes(x=longitude_minus_w, y=latitude_minus_s), show.legend = FALSE, alpha=0.4, bins=7)+
scale_fill_manual(values = c("white", "#CEE9e9", "#84BBD8", "#F8F2BE", "#FEC376", "#F88A51", "#A50026"))+
geom_text(data = state_labels %>% filter(!(region %in% c("massachusetts", "connecticut", "new jersey", "delaware",
"maryland", "district of columbia", "new hampshire"))),
aes(x = long, y = lat, label = word), size =2, inherit.aes = FALSE)+
geom_text_repel(data = state_labels %>% filter(region %in% c("massachusetts", "connecticut", "new jersey", "delaware",
"maryland", "district of columbia", "new hampshire")),
aes(x=long, y=lat, label = word), nudge_x = c(5, 3, 5, 4, 5, 4, 4),
nudge_y = c(0, 0, -3, 0, 0, 0, 0), size = 2, min.segment.length = 0.2)+
theme_classic()+
theme(axis.ticks = element_blank(),
axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.background = element_blank(),
panel.background =element_blank())
❝本节介绍到此结束,有需要学习数据可视化的朋友,欢迎到小编的「淘宝店铺」 「R语言数据分析指南」下单购买,内容主要包括各种「高分论文的图表分析复现以及一些个性化图表的绘制」均包含数据+代码;
❞
购买后微信发小编订单截图即邀请进新的会员交流群,无论购买那一年的会员文档都将获取「2024年及以后更新的绘图文档」,早买早享受,需要了解更多信息的朋友欢迎淘宝店铺交流咨询。
https://mp.weixin.qq.com/s/7ETkHXuPe9Pftk-7CbhLFg