0

我使用 hjust 在我的条形图中添加了标签,但由于某种原因,对于具有更高值的条形,它们离它们更远。理想情况下,我希望它们都离每个酒吧都很近。

plot_snpEff_annotations <- ggplot(plot_METTL14_can_exome_annotations, aes(plot_METTL14_can_exome_annotations$snpEff_Annotation)) + 
  geom_bar(position = "dodge", color = "black", fill=my_palette) +
  theme_minimal() +
  coord_flip() +
  geom_text(aes(label = ..count.., hjust = -1), stat="count", size = 3) +
  labs(title = "Types of Variants in METTL14", subtitle = "snpEff Annotation", 
       x = "Count", y = "Variant Type")
plot_snpEff_annotations

图片1

4

1 回答 1

0

hjust通常在 0 和 1 之间变化,其中 0.5 是中心对齐的。我建议设置hjust = 0然后使用nudge调整器来调整数据坐标中的数量。例如:

ggplot(data.frame(a = letters[1:3], b = 1:3), aes(x = a, y = b)) +
  geom_col() +
  geom_text(aes(label = b^3), hjust = 0, nudge_y = 0.05) +
  coord_flip()

您的行为与预期不符的原因hjust是:文本“锚定”在数据坐标处。如果设置hjust = 0,它将左对齐,以该点为边界。使用hjust = 0.5,文本的中心将是该点,并将hjust = 1以该点作为边距右对齐。比例是线性的,较长的文本需要比较短的文本移动更远才能从左对齐到右对齐,并且当您超出 (0, 1) 间隔时,它会以相同的速率继续。

于 2020-03-30T20:47:19.183 回答