【问题标题】:ggplot2 y-axis ticks not showing up on a log scaleggplot2 y轴刻度未显示在对数刻度上
【发布时间】:2012-03-11 02:28:47
【问题描述】:

我正在尝试使用 ggplot2 创建箱线图,但无法像 ggplot2 网站示例中那样显示刻度。

这里有一些水果味道的假数据:

apples <- data.frame(fruit=c(rep("apple", 30)), taste=runif(30, 30, 50)
banana <- data.frame(fruit=c(rep("banana", 30)), taste=runif(30, 300, 500))
orange <- data.frame(fruit=c(rep("orange", 30)), taste=runif(30, 3000, 5000))
fruits <- rbind(apples,banana,orange)

如果我在ggplot2 website example 中绘制,y 轴刻度应该类似于:

相反,我得到一个像这样的轴:

ggplot(fruits, aes(fruit, taste) ) +  geom_boxplot() + scale_y_log10()

如何获得科学计数法的 y 轴刻度?

【问题讨论】:

    标签: r ggplot2 graphing


    【解决方案1】:

    我假设您使用的是新的 0.9.0 版本的 ggplot2,它进行了大量更改。我相信这恰好是其中之一。

    如果我没记错的话,有足够多的人抱怨指数格式是默认格式,这已被更改。根据transition guide,您可以通过以下方式实现相同的效果:

    library(ggplot2)
    library(scales)
    ggplot(fruits, aes(fruit, taste) ) +  
        geom_boxplot() + 
        scale_y_log10(breaks = trans_breaks('log10', function(x) 10^x),
                      labels = trans_format('log10', math_format(10^.x)))
    

    【讨论】:

    • 现在似乎不适合我。如果一段时间,我将不得不玩。最重要的不是科学记数法,而是 y 轴上 0 到 1000 之间的巨大差距。当我更新我的操作系统时,我一定是错误地更新到了 0.8.9。
    • @Kev 很高兴它有帮助。何时需要显式加载以及何时不需要显式加载,我还没有完全理解。
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多