我尝试了geom_text()
的许多组合,但无法获得我需要的确切输出。
我希望标签保持在相同的y轴极限,除非它们相交如果它们相交,我需要它们在y方向上偏移,这样它们就不会相交。我希望它们高效,以便它们之间没有多少空间。
geom_text_repel()
不是修复,因为它会引发一些错误。有人可以解释我使用geom_text_repel()
这是我的数据和代码:
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),"%")))
错误是:
Error in .Call("_ggrepel_repel_boxes", PACKAGE = "ggrepel", data_points, :
Incorrect number of arguments (11), expecting 9 for '_ggrepel_repel_boxes'
答案 0 :(得分:1)
你必须使用geom_text_repel的参数,但这可能是一个起点:
library(ggrepel)
library(ggplot2)
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="")+
ggrepel::geom_text_repel(direction = "y",
segment.colour = "transparent",
nudge_x = 0.2, point.padding = 0.1, box.padding = 0.1)