使用geom_text_repel自定义标签,在行尾显示

问题描述

在以下图表中,我想知道如何自定义标签(状态名称)以减少标签间的距离? 这是我的代码的一部分,我认为需要对其进行修改

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)+

enter image description here

解决方法

尝试将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")
                 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...