r - 如何抖动/删除 geom_text 标签的重叠

标签 r ggplot2 geom-text

Figure

在图中,是否可以稍微抖动状态缩写标签以使它们不重叠?如果我使用 check_overlap = TRUE ,那么它会删除一些重叠的观察结果,但我不希望这样。我也不想要 geom_label_repel,因为它的标签突出并穿过我包含的 45 度线(我不希望发生这种情况)

这是我的代码的相关部分供引用:

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) +
  geom_abline(intercept = 0) +
  geom_text(fontface = "bold")

最佳答案

您尝试过position=position_jitter()吗?您可以根据自己的选择调整宽度高度

ggplot(df, aes(x = huff_margin_dem, y = margin16dem_state, label = abbrev)) +
  geom_abline(intercept = 0) +
  geom_text(fontface = "bold",position=position_jitter(width=1,height=1))

编辑 仅操作某个标签的示例

+geom_text(fontface = "bold",
position=position_jitter(width=ifelse(df$abbrev=='KS',1,0),
      height=ifelse(df$abbrev=='KS',1,0)))

或多个标签

df$jit<-with(df, ifelse(abbrev == "KS" | abbrev == "LA", 1, 2))

+geom_text(fontface = "bold",
    position=position_jitter(width=df$jit,height=df$jit))

关于r - 如何抖动/删除 geom_text 标签的重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542685/

相关文章:

使用 R 中的plotly包再现等值区域图

r - 使用 st_join 进行使用最大交叉点的空间连接

r - 我希望主要/次要网格线的 ggplot 网格线厚度不同

r - ggplot dotplot 为每个人显示一个点

r - FUN(X[[i]], ...) 错误 : object not found when adding geom_text with facet

r - Xgboost,最大核心数

regex - 按空格拆分字符串,除非包含在引号中

使用 facet_wrap 和 geom_segment 删除不必要的 y 轴点

R ggplot2 : geom_text error (object not found). .. aes() 冲突?

r - 如何使用 geom_text 在堆叠条形图的比例尺上添加频率或数字?