问题描述
在以下图表中,我想知道如何自定义标签(状态名称)以减少标签之间的距离? 这是我的代码的一部分,我认为需要对其进行修改。
ggrepel::geom_text_repel(aes(x =Year+1.6,y = Age.Adjusted.Rate,colour = State,label = State,fontface = 'bold'),data = d_filtered_top5_fe %>%
filter(Year == max(Year)),segment.color = 'transparent',direction = "y",size = 2.5)+
解决方法
尝试将box.padding
中的geom_text_repel
降低。
默认值为.25。
第二步,您可以使用force
参数减少重叠的文本标签之间的间隔。
df <- data.frame(y = rnorm(mean = 1,sd = .2,n = 50),name = rep(LETTERS[1:10],each = 5),x = rep(1:5,10))
df %>%
ggplot(aes(x = x,y = y,color = name)) +
geom_line() +
ggrepel::geom_text_repel(aes(x = x + .1,colour = name,label = name,fontface = 'bold'),data = df %>%
filter(x == max(x)),segment.color = 'transparent',direction = "y",size = 2.5,box.padding = .1,force = .8) +
scale_y_continuous(limits = c(0,2)) +
theme_classic() +
theme(legend.position = "none")