首页 文章

ggplot2轴:设置间隔,对数刻度和指数而不是科学

提问于
浏览
0

首先,我对编程和R(一周)完全不熟悉,所以请提前道歉 .

我将如何使用ggplot2以下列方式格式化y轴?:

  • 我想要的间隔数 . (例如,10,视觉上等距的间隔)

  • 对数刻度

  • 指数代替科学(我想要10¹,10²,10³而不是1e 01,1e 02,1e 03)

我可以找到其中一些问题的答案,但它们不能同时发挥作用 .

这是我的图表 . 我不知道这是否有帮助 .

ggplot(dfm,aes(Strain,value))geom_bar(aes(fill = variable),stat =“identity”,position =“dodge”)

底线是:目前y轴是:1e 02,1e 05,1e 08我希望它是:10¹,10²,10³,10⁴,10⁵,10⁶,10⁷,10⁸,10⁹,10¹⁰

1 回答

  • 0

    以下是一些指导(我正在使用 tidyr::population 数据集)

    1 .

    library(ggplot2)
    library(scales)
    library(tidyr)
    ggplot(population, aes(country, population)) + 
      geom_bar(aes(fill=year),stat="identity",position="dodge") +
      scale_y_continuous(breaks = pretty_breaks(n = 10))
    

    2 .

    library(ggplot2)
    library(scales)
    library(tidyr)
    ggplot(population, aes(country, population)) + 
      geom_bar(aes(fill=year),stat="identity",position="dodge") +
      scale_y_log10()
    

    把它们放在一起:

    library(ggplot2)
    library(scales)
    library(tidyr)
    ggplot(population, aes(country, population)) + 
      geom_bar(aes(fill=year),stat="identity",position="dodge") +
      scale_y_log10(labels= trans_format(log10, math_format(10^.x)), 
                    breaks =trans_breaks(log10, function(x) 10^x, 10))
    

相关问题