1

我有一组要ggplot在 (x,y) 位置打印的文本,其中只有一部分重叠。我想保持那些不完全重叠的地方,然后排斥那些重叠的(我知道哪些做这些 - 例如新英格兰的州名重叠,而在西部没有重叠,我想保留西部各州的名称,但排斥新英格兰的名称)。当我使用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()
4

0 回答 0