2014-02-05 1 views
5

분산 형 플롯의 동일한 데이터 요소에 대한 데이터 레이블이 겹치지 않도록하는 기능이 있습니까? textxy, direct.label 및 geom_text()에 대한 다양한 질문/응답을 확인했지만 성공하지 못했습니다. 어쩌면 불가능할 수도 있습니다.scatterplot/ggplot에서 동일한 데이터 요소와 중복되는 레이블을 피하는 방법은 무엇입니까?

다음은 관련 데이터의 샘플입니다 :

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) 

그리고 아래에있는 내 그래프는 다음과 같습니다

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

여기 내 코드입니다. 중복되는 데이터 포인트를 수동으로 강조 표시했습니다.

내가 찾고있는 것은 중첩되지 않는 데이터 포인트의 라벨이 겹치지 않도록 표시되는 '자동'방법입니다. 이 목적을 위해 어떤 기능이 있습니까? 많은 감사합니다!

enter image description here

+1

'directlabels' 패키지와이 [관련 질문] (http://stats.stackexchange.com/questions/16057/how-do-i-avoid-overlapping-labels-in-an-r)을 살펴보십시오. -plot) – Justin

+0

잠재력 [중복 질문] (http://stackoverflow.com/questions/11197554/how-to-jitter-text-to-avoid-overlap-in-a-ggplot2-scatterplot) – BrodieG

+0

많은 감사. 나는 directlabels 꾸러미를 다시 보았다. ggplot-plot을 생성 할 때 geom_text (aes (label = conflict), hjust = 0, vjust = 0, size = 4)를 꺼내야한다고 생각했습니다 (레이블은 나중에 direct.label (vetotype.plot) 그 결과는 ok입니다.하지만 아래의 jlhoward의 솔루션만큼 좋지는 않습니다. – zoowalker

답변

4

이 완전히 일반적인 해결책이 아니다,하지만 귀하의 경우 제대로 작동 않습니다.

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) 

+0

많은 감사합니다! 훌륭한 결과를 보여주는 창의적인 접근 방식입니다. 직접 알아낼 수 없었을 것입니다. – zoowalker

1

여기 플롯이 ggrepel와 같은 모습입니다 :

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) 

enter image description here