【问题标题】:Using nudge_x and nudge_y with ggplot2将 nudge_x 和 nudge_y 与 ggplot2 一起使用
【发布时间】:2016-08-27 00:51:20
【问题描述】:

我开始学习 ggplot2 已经 3 天了。我正在阅读http://docs.ggplot2.org/current/geom_text.html 的手册,以了解如何使文本与底层几何图形不重叠。

这就是我所做的:

df <- data.frame(trt = c("a", "b", "c"), resp = c(2, 3, 4))
     ggplot(df, aes(resp, trt)) +
       geom_point() +
       geom_text(aes(label = resp , nudge_y = 2, nudge_x = 2))

我尝试使用 nudge_x 和 nudge_y,但似乎没有任何反应。

这是输出。如我们所见,文本与点重叠。 :

无论如何我们可以解决这个问题吗?另外,nudge_x 和 nudge_y 有什么用?我不是从手册中得到它。

【问题讨论】:

  • 将它们移出aesthetic,并选择合理的值..geom_text(aes(label = resp) , nudge_y = 0.5, nudge_x = 0.25)

标签: r ggplot2


【解决方案1】:

问题是nudge_xnudge_y 应该超出审美范围。

df <- data.frame(trt = c("a", "b", "c"), resp = c(2, 3, 4))

ggplot(df, aes(resp, trt)) +
       geom_point() +
       geom_text(aes(label = resp), nudge_y = -0.1)

这解决了这个问题。我减少了nudge_y 的值。

这是图表:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多