R 如何避免散点图/ggplot中具有相同数据点的标签重叠?

R 如何避免散点图/ggplot中具有相同数据点的标签重叠?,r,ggplot2,label,scatter-plot,R,Ggplot2,Label,Scatter Plot,是否有任何功能可以避免散点图中相同数据点的重叠数据标签? 我已经检查了对textxy、direct.label和geom_text()的各种问题/回答,但没有成功。也许这根本不可能 以下是相关数据的样本: structure(list(cowc = structure(c(5L, 7L, 24L, 24L, 23L, 36L, 34L, 38L, 23L, 6L, 8L, 38L, 38L, 23L, 5L, 7L, 24L, 24L, 23L, 36L, 34L, 38L, 23L, 6L

是否有任何功能可以避免散点图中相同数据点的重叠数据标签? 我已经检查了对textxy、direct.label和geom_text()的各种问题/回答,但没有成功。也许这根本不可能

以下是相关数据的样本:

structure(list(cowc = structure(c(5L, 7L, 24L, 24L, 23L, 36L, 
34L, 38L, 23L, 6L, 8L, 38L, 38L, 23L, 5L, 7L, 24L, 24L, 23L, 
36L, 34L, 38L, 23L, 6L, 8L, 38L, 38L, 23L), .Label = c("AFG", 
"ANG", "AZE", "BNG", "BOS", "BUI", "CAM", "CDI", "CHA", "COL", 
"CRO", "DOM", "DRC", "ETH", "GNB", "GRG", "GUA", "IND", "INS", 
"IRQ", "KEN", "LAO", "LBR", "LEB", "MAL", "MLD", "MZM", "NEP", 
"NIC", "PHI", "PNG", "RUS", "RWA", "SAF", "SAL", "SIE", "SOM", 
"SUD", "TAJ", "UKG", "YAR", "ZIM"), class = "factor"), conflict = c("Bosnia 92-95", 
"Cambodia 70-91", "Lebanon 58-58", "Lebanon 75-89", "Liberia 89-93", 
"SieLeo 91-96", "Stafrica 83-91", "Sudan 63-72", "Liberia 94-96", 
"Burundi 1993-2005", "Cote d'Ivoire 2002-2007", "Darfur, Sudan 2003-2010", 
"Sudan 83-05", "Liberia 1999-2003", "Bosnia 92-95", "Cambodia 70-91", 
"Lebanon 58-58", "Lebanon 75-89", "Liberia 89-93", "SieLeo 91-96", 
"Stafrica 83-91", "Sudan 63-72", "Liberia 94-96", "Burundi 1993-2005", 
"Cote d'Ivoire 2002-2007", "Darfur, Sudan 2003-2010", "Sudan 83-05", 
"Liberia 1999-2003"), totalps = c(3L, 2L, 2L, 2L, 1L, 3L, 4L, 
3L, 1L, 3L, 3L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 4L, 3L, 1L, 
3L, 3L, 4L, 3L, 3L), vetotype = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("strictvetos", "lenientvetos"
), class = "factor"), intensity = c(3L, 4L, 2L, 5L, 2L, 2L, 2L, 
2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 4L, 2L, 6L, 2L, 2L, 4L, 2L, 2L, 
3L, 3L, 2L, 2L, 2L)), .Names = c("cowc", "conflict", "totalps", 
"vetotype", "intensity"), class = "data.frame", row.names = c(NA, 
-28L))
这是我的密码:

vetotype.plot <- ggplot(vetotype.x, aes(x=totalps, y=intensity, color=conflict))+
      geom_point() + 
      labs(x="number of power-sharing arenas", y="intensity") +
      ggtitle("Number of Power-Sharing areas and Veto intensity") +
      geom_text(aes(label=conflict),hjust=0, vjust=0, size=4)+
      scale_x_continuous(limits=c(1, 5))+
      theme(legend.position="none")+
      facet_wrap(~vetotype, nrow=2)

plot(vetotype.plot)

vetotype.plot这不是一个完全通用的解决方案,但在您的情况下似乎确实有效

library(ggplot2)
# identify duplicated points
dupes <- aggregate(conflict~totalps+intensity+vetotype,vetotype.x,length)
colnames(dupes)[4] = "dupe"
df <- merge(vetotype.x,dupes)   # add dupe column
df$vjust <- 0                   # default vertical offset is 0
# calculate vertical offsets based on number of dupes
for (i in 2:max(df$dupe)) df[df$dupe==i,]$vjust<-seq(-trunc(i/2),-trunc(i/2)+i-1)
# render the plot
vetotype.plot <- ggplot(df, aes(x=totalps, y=intensity, color=conflict))+
  geom_point() + 
  labs(x="number of power-sharing arenas", y="intensity") +
  ggtitle("Number of Power-Sharing areas and Veto intensity") +
  geom_text(aes(label=conflict,vjust=vjust), hjust=0,size=4)+
  scale_x_continuous(limits=c(1, 5))+
  scale_y_continuous(limits=c(1, 6))+
  theme(legend.position="none")+
  facet_wrap(~vetotype, nrow=2)

plot(vetotype.plot)
库(ggplot2)
#识别重复点

DUPEggrepel现在可以轻松做到这一点:


以下是您的绘图:


请查看
directlabels
软件包和此潜在的功能,非常感谢。我再次查看directlabels包。我认为,在生成ggplot绘图时,必须删除geom_文本(aes(label=conflict)、hjust=0、vjust=0、size=4)(因为后来通过direct.label(vetotype.plot)添加了标签)。我想,结果还可以,但不如下面jlhoward的解决方案好。非常感谢!对我来说,这是一个非常有创意的方法,效果非常好。我自己也无法解决这个问题。
library(ggrepel)

ggplot(vetotype.x, aes(x=totalps, y=intensity, color=conflict))+
  geom_point() + 
  labs(x="number of power-sharing arenas", y="intensity") +
  ggtitle("Number of Power-Sharing areas and Veto intensity") +
  geom_text_repel(
    aes(label=conflict), size=4, box.padding = unit(0.5, "lines")
  )+
  scale_x_continuous(limits=c(1, 5))+
  theme(legend.position="none")+
  facet_wrap(~vetotype, nrow=2)