如何避免使用scatterplot / ggplot中的相同数据点重叠标签? [英] how to avoid overlapping labels with identical data points in scatterplot / ggplot?

查看:398
本文介绍了如何避免使用scatterplot / ggplot中的相同数据点重叠标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是相关数据的示例:

 结构(列表(牛皮=结构(c(5L,7L,24L,24L,23L,36L,
34L,38L,23L,6L,8L,38L,38L,23L,5L,7L,24L,24L,23L,标签= c(AFG,
ANG,AZE,BNG,BOS,b $ b 36L,34L,38L,23L,6L,8L,38L,38L,23L) ,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 波斯尼亚92-95,
柬埔寨70-91,黎巴嫩58-58 ,黎巴嫩75-89,利比里亚89-93,
SieLeo 91-96,Stafrica 83-91,苏丹63-72,利比里亚94-96,
布隆迪1993-2005,科特迪瓦2002-2007,苏丹达尔富尔2003-2010,
苏丹83-05,利比里亚1999-2003,波斯尼亚92- 95,柬埔寨70-91,
黎巴嫩58-58,黎巴嫩75-89,利比里亚ria 89-93,SieLeo 91-96,
Stafrica 83-91,苏丹63-72,利比里亚94-96,布隆迪1993-2005,
2002 - 2007年科特迪瓦,达尔富尔,苏丹2003-2010,苏丹83-05,
利比里亚1999-2003),总数= 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 (1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,3L,3L) 2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,2L).Label = c(strictvetos,lenientvetos
),class =factor ),强度= 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 ),class =data.frame,row.names = c(NA,
-28L))

这是我的代码:

  vetotype.plot<  -  ggplot(vetotype.x,aes(x = totalps ,y =强度,颜色=冲突))+ 
geom_point()+
实验室(x =共享电源区域的数量,y =强度)+
ggtitle(功率共享区域数量和Veto强度)+
geom_text(aes(label = conflict),hjust = 0,vjust = 0,size = 4)+
scale_x_continuous(limits = c(1,5))+
theme(legend.position =无)+
facet_wrap(〜vetotype,nrow = 2)

plot(vetotype.plot)

以下是我的图表。我手动突出显示那些重叠的数据点。

我在寻找的是一种'自动'的方式,让重叠数据点的标签以不重叠的方式显示。有这个功能吗?非常感谢!




解决方案

这不是一个完全一般的解决方案,但它似乎适用于您的情况。

  library(ggplot2)
#识别重复点
dupes < - 集合(conflict〜totalps +强度+ vetotype,vetotype.x,长度)
colnames(dupes)[4] =dupe
df< - merge(vetotype.x,dupes)#add dupe column
df $ vjust< - 0#default vertical偏移量为0
#根据投影数量计算垂直偏移量
for(i in 2:max(df $ dupe))df [df $ dupe == i,] $ vjust< -seq( - trunc(i / 2), - trunc(i / 2)+ i-1)
#渲染图
vetotype.plot < - ggplot(df,aes(x = totalps,y =强度,颜色=冲突))+
geom_point()+
实验室(x =功率共享区域数量,y =强度)+
ggtitle(共享区域和Veto强度)+
geom_text(ae (限制= c(1,5))+
scale_y_continuous(限制= c(1,6)s)(标签=冲突,vjust = vjust),hjust = 0,size = 4)+
scale_x_continuous )+
主题(legend.position =none)+
facet_wrap(〜vetotype,nrow = 2)

plot(vetotype.plot)


Is there any function etc which avoids overlapping data labels for identical data points in a scatter plot? I have checked the various questions/responses to textxy, direct.label, and geom_text(), but I haven't been successful. Maybe it's simply not possible.

Here's a sample of the relevant data:

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))

Here's my code:

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)

And below is my graph. I manually highlighted those data points which are overlapping.

What I am looking for is an 'automatic' way to get the labels of the overlapping data points displayed in way so that they don't overlap. Is there any function for this purpose? Many thanks!

解决方案

This is not a completely general solution, but it does seem to work in your case.

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)

这篇关于如何避免使用scatterplot / ggplot中的相同数据点重叠标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆