【发布时间】:2018-05-21 10:27:30
【问题描述】:
我尝试了许多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'
【问题讨论】:
-
使用
ggrepel::geom_text_repel()而不是geom_text(position=position_jitter())。 -
试过了,它没有给出想要的结果,我需要它们沿着 y 轴在同一条线上,但是当它们相交时偏移,它们应该偏移最小值,这样标签几乎不会接触和偏移仅在正 y 轴上。
-
您可以指定参数
direction = "x"(“调整标签位置的方向”)。 -
还是同样的错误
标签: r plot ggplot2 graph plotly