Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust specific text lables in ggplot to avoid overlap

Given a dummy data set:

V1<-c(1,10,30,22,22,20)
V2<-c(4,17,4,33,33.2,15)
V3<-c(20,20,15,34,33,30)
V4<-c("A","A","A","B","B","B")
DF<-data.frame(V1,V2,V3,V4)

If I were to plot a bubble plot like so:

symbols(DF$V1,DF$V2,circles=V3,inches=0.35,fg="darkblue",bg="red")
text(DF$V1,DF$V2,V4,cex=0.5)

I get some overlap with the 4th and 5th label text. Using the below code, I can remove this overlap by adjusting specific points

text(DF$V1,DF$V2+c( 0, 0, 0, 1, -1,0 ),V4,cex=0.5)

Which works fine, but I would like to work in ggplot. If I do a simple ggplot

ggplot(DF,aes(x=V1,y=V2,size=V3,label=V4),legend=FALSE)+geom_point(color='darkblue',fill="red", shape=21)+theme_bw()+geom_text(size=5)

I get the same overlap, but I don't know how to adjust the individual points to avoid the overlap. I've tried using "thigmophobe.labels", but that adjusts all points. I'd like to just slightly adjust the points that overlap as to make them readable.

like image 705
Vinterwoo Avatar asked Jan 11 '23 18:01

Vinterwoo


1 Answers

If the data value being overlapped is not very important, a quick and dirty solution is to specify:

+ geom_text(size = 5, check_overlap = T)

So that text overlap previous text will not be plotted.

like image 193
Kewei Qu Avatar answered Jan 17 '23 14:01

Kewei Qu