所以我有这个ggplot,其中标签由geom_label_repel()方法定位。它相当不错,但是我喜欢标签,所有这些都在geom_line()之上,而不是在下面,这在某些情况下会发生。
这是生成图表的代码:
ggplot(data,
aes(x=daydelta,y=day1ret)
) +
geom_line(color='steelblue',size=2) +
geom_label_repel(aes(label = paste0(round(day1ret*100,2),"%")),box.padding = unit(0.6, "lines"),label.size = 0.1)+
scale_x_continuous(limits = c(1,15),breaks=1:15,minor_breaks = 1:15) +
scale_y_continuous(limits = c(0,1),breaks=seq(0,1,0.1),minor_breaks = seq(0,1,0.1),labels = paste0(seq(0,1,0.1)*100,"%")) +
ggtitle("RETENTION BY USERID") +
theme(plot.title = element_text(hjust = 0.5))+
xlab("Days from Register\n (baseline: Day 1)")+
ylab("Retention as % of returning users from Day 1")+
geom_segment(mapping=aes(x=daydelta,y=0,xend=daydelta,yend=day1ret),size=0.5,color="red",linetype=2)
答案 0 :(得分:1)
为什么不将标签放在实际的x和y值上,而不是使标签偏离数据线而使图形混乱?这在视觉上更清晰,并且不会通过将标签放置在它们所代表的数据值之外的位置来分散或误导观看者。例如:
library(ggplot2)
library(scales)
dat = data.frame(x=1:10, y=c(1, seq(.3,.1,length=9)))
ggplot(dat, aes(x,y)) +
geom_line() +
geom_label(aes(label=paste0(sprintf("%1.1f",y*100),"%")), size=3, label.padding=unit(2,"pt")) +
scale_y_continuous(labels=percent, limits=c(0,1)) +
scale_x_continuous(breaks=dat$x) +
theme_classic()