【发布时间】:2021-08-20 20:51:54
【问题描述】:
我在 R 中制作了一张地图,想知道如何适当地标记州代码(我的数据集中的变量)。使用简单的 geom_text 甚至 geom_text_repel 我得到了每个州的很多标签(我实际上可以理解为什么),正如我继续展示的那样:
我该如何解决它,以便每个州获得 1 个且只有 1 个文本缩写(这些州代码在我的数据集中作为变量命名为 State Codes)?提前致谢。
代码如下:
library(tidyverse)
library(maps)
library(wesanderson)
library(hrbrthemes)
ggplot(data = data,
mapping = aes(x = long,
y = lat,
group = group,
fill = black_percentage)) +
geom_polygon(col = "black") +
geom_text(aes(label = black_percentage)) +
theme_void() +
theme(legend.position = "bottom",
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5, family = "Times", face = "bold"),
plot.subtitle = element_text(hjust = 0.5, family = "Times", face = "italic"),
plot.caption = element_text(family = "Times", face = "italic"),
legend.key.height = unit(0.85, "cm"),
legend.key.width = unit(0.85, "cm")) +
scale_fill_gradient(low = "#E6A0C4",
high = "#7294D4") +
labs(title = "Percentage of Black People, US States 2018",
subtitle = "Pink colors represent lower percentages. Light-blue colors represents higer percentages") +
ggsave("failed_map.png")
【问题讨论】:
-
我在这里有点搞砸了,我说我想要将变量标记为“black_percentage”而不是“
State Codes”,但这张地图仅用于演示目的。正确标记后仍然无法正常工作。 -
你能提供/一些示例数据吗?
标签: r dictionary ggplot2