我在(x,y)位置使用ggplot
打印一组文本,其中只有一部分重叠。我想保持那些不完全重叠的地方,然后排斥重叠的那些(我知道哪些会做这些 - 例如新英格兰州的州名称重叠,而在西方没有重叠,我想保持西方国家的名字,但它们击退了新英格兰的那些国家。当我使用geom_text_repel
时,它会排斥所有文本。如果我选择了不重叠的子集并使用geom_text
打印它们而另一个使用geom_text_repel
,因为它们位于不同的层。有没有办法修复某些文本子集并使用geom_text_repel
排斥其余部分,或者我是否需要采用完全不同的解决方案?
以下是一个例子:
library(tidyverse)
library(ggrepel)
# state centers by fixing Alaska and Hawaii to look good in our maps
df = data.frame(x = state.center$x, y= state.center$y, z = state.abb)
overlaps = c('RI', 'DE', 'CT', 'MA')
df %>%
ggplot() +
geom_point(aes(x,y),
size = 1) +
# plot the ones I would like to keep where the are
# I want these right centered around the points
geom_text(aes(x,y,label=z),
data = df %>% filter(! z %in% overlaps),
size = 4) +
# plot the ones I would like to repel
geom_text_repel(aes(x,y,label=z),
data = df %>% filter(z %in% overlaps),
size = 4,
min.segment.length = unit(0, "npc")) +
coord_map() +
theme_minimal()
df %>%
ggplot() +
geom_point(aes(x,y),
size = 1) +
# if we repel all instead
geom_text_repel(aes(x,y,label=z),
size = 4,
min.segment.length = unit(0, "npc")) +
coord_map() +
theme_minimal()