I've tried many combinations of geom_text()
but couldn't get the exact output I need.
I want the labels to remain at same y-axis limit unless they intersect If they do intersect, I need them to offset in the y-direction so that they will not intersect. I want them efficient so that not much space is left between them.
geom_text_repel()
is not a fix because it throws some error. can someone explain why there is an error when I use geom_text_repel()
This is my Data and code:
library(ggplot2)
library(ggrepel)
Data=data.frame(Group=c("Group 2","Group 1","Group 3","Group 2","Group 5","Group 4","Group 6",
"Group 7","Group 4","Group 3","Group 1","Group 5","Group 6","Group 7",
"Group 2","Group 4","Group 6","Group 7","Group 3","Group 1"),
Fruit=c("apple","apple","apple","mango","apple","apple","apple","apple","mango","mango",
"mango","mango","mango","mango","orange","orange","orange","orange","orange",
"orange"),
Percentage=c(68.46846847,77.35849057,72.72727273,26.12612613,76.31578947,62.79069767,
71.05263158,69.23076923,30.23255814,25,20.75471698,23.68421053,23.68421053,
23.07692308,5.405405405,6.976744186,5.263157895,7.692307692,2.272727273,
1.886792453))
ggplot(Data,aes(x=reorder(Fruit,Percentage),y=Percentage,color=Group,label=paste0(round(Percentage),"%")))+geom_point(size=4)+coord_flip()+scale_y_continuous(limits=c(0,100))+
theme(panel.grid.major.y=element_line(color="gray90",size = 0.7),panel.background=element_blank(),strip.background=element_blank(),panel.border=element_rect(color="black",fill=NA,size=1))+
labs(x="",y="% of people",color="")+geom_text_repel(aes(label=paste0(round(Percentage),"%")))
The error is:
Error in .Call("_ggrepel_repel_boxes", PACKAGE = "ggrepel", data_points, :
Incorrect number of arguments (11), expecting 9 for '_ggrepel_repel_boxes'