【问题标题】:geom_text does not appear using scale_y_log10geom_text 不使用 scale_y_log10 出现
【发布时间】:2015-03-07 17:37:27
【问题描述】:

我可以使用 geom_text 将文本添加到 ggplot 中(使用汽车数据库的示例):

ggplot(cars, aes(speed, dist), color='blue') + 
geom_point(size=0.8) + 
geom_text(y=25, x=5, aes(label=paste("sigma == 1"), size=1.5), 
    hjust=0, parse=TRUE, color='blue')

但是当我将 y 比例更改为对数时,我无法让文本出现在图表上:

ggplot(cars, aes(speed, dist), color='blue') + 
geom_point(size=0.8) + 
geom_text(y=25, x=5, aes(label=paste("sigma == 1"), size=1.5), 
    hjust=0, parse=TRUE, color='blue') + 
scale_y_log10()

我尝试过改变文本大小和位置,但似乎无法做到。

【问题讨论】:

    标签: r text ggplot2 geom-text


    【解决方案1】:

    这对我有用:

    require(ggplot2)
    g <- ggplot(cars, aes(speed, dist), color='blue')
    g <- g + geom_point(size=0.8)
    g <- g + geom_text(aes(y=25, x=5, label=paste("sigma == 1"), size=1.5), hjust=0, parse=TRUE, color='blue')
    g <- g + scale_y_log10()
    g
    

    http://www.r-fiddle.org/#/fiddle?id=GVWg1dDO

    【讨论】:

    • 有趣又奇怪。这对我行得通。现在,当我像上面那样重新发出命令时,它也可以正常工作。很奇怪。感谢您的帮助。
    • 那么只是 y=25 和 x=5 在 aes 之外给 ggplot 带来了麻烦......
    【解决方案2】:

    这样效果更好:

    ggplot(cars, aes(speed, dist), color='blue') + 
        geom_point(size=0.8) + 
        geom_text(y=2, x=5, aes(label=paste("sigma == 1"), size=1.5), 
            hjust=0, parse=TRUE, color='blue') + 
        scale_y_log10()
    

    enter image description here

    正常对数刻度:100 -> 2; 1000 -> 3; 0.1 -> -1; 0.01 -> -2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2014-05-29
      • 2015-05-20
      • 1970-01-01
      • 2014-08-28
      相关资源
      最近更新 更多