【问题标题】:ggplot2 coord_flip() with geom_textggplot2 coord_flip() 与 geom_text
【发布时间】:2017-03-03 17:57:33
【问题描述】:

我正在尝试找到一种方法来轻松翻转带有 geom_text() 注释的绘图。问题是如果我使用 coord_flip(),标签就不会翻转。

一个简单的例子,如果我像这样翻转情节:

df <- count(diamonds, cut)

ggplot(df, aes(x = cut, y = n, label = n)) +
geom_bar(stat = "identity") +
geom_text()

通过添加 coord_flip(),我得到:

我知道在某些情况下这可能是一种理想的行为,但我需要条形上的标签与条形保持对齐并且看起来更像:

有什么解决办法吗?

【问题讨论】:

  • 为了可读性,为什么不让文本保持水平呢?例如,您可以使用ggplot(df, aes(x=cut, y=n, label=format(n, big.mark=","))) + geom_bar(stat="identity") + geom_text(aes(y=0.5*n), colour="white") + coord_flip()

标签: r ggplot2


【解决方案1】:

您可以在geom_text 中使用angle 美学,将其设置为固定值。

ggplot(df, aes(x = cut, y = n, label = n)) +
    geom_bar(stat = "identity") +
    geom_text(angle = 270) +
    coord_flip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2018-11-02
    • 2011-12-01
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多