【问题标题】:Generating compact scientific notation with ggplot2 on R在 R 上使用 ggplot2 生成紧凑的科学记数法
【发布时间】:2021-03-01 04:46:23
【问题描述】:

我用 ggplot2 生成的图看起来像左边的图,在 y 轴的每个刻度上都有完整的科学记数法。我怎样才能让它看起来更紧凑,就像右边的图一样,在角落里用红色圆圈标记了科学记数法?

我没有在 ggplot2 包文档或堆栈溢出中看到这一点。有人有解决方法吗?

two formats for scientific notation

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    从类似的情节开始:

    ggplot(mtcars, aes(wt, mpg * 1E-8)) +
      geom_point()
    

    如果我们知道要使用的比例,我们可以定义它,然后我们可以在输入的过程中对数据进行缩放,或者更改 y 轴上的标签,除了 y 轴标签外,看起来都一样(我们可以随意重命名):

    divisor = 1E-8
    
    ggplot(mtcars, aes(wt, mpg * 1E-8 / divisor)) +
      geom_point() +
      labs(title = formatC(divisor, format = "e", digits = 0))
    

    ggplot(mtcars, aes(wt, mpg * 1E-8)) +
      geom_point() +
      scale_y_continuous(labels = function(x) x / divisor) +
      labs(title = formatC(divisor, format = "e", digits = 0))
    


    编辑:

    如果您还想要标题,也可以使用annotate 在绘图区域外写下文本,然后将标题向上移动:

    ggplot(mtcars, aes(wt, mpg * 1E-8)) +
      geom_point() +
      scale_y_continuous(labels = function(x) x / divisor) +
      annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = -0.5,
               label = formatC(divisor, format = "e", digits = 0)) +
      coord_cartesian(clip = "off") +
      labs(title = "Title here") +
      theme(plot.title = element_text(margin = margin(0,0,20,0)))
    

    【讨论】:

    • 感谢@Jon Spring 的详细回复。这是否也允许我放置标题?看起来除数取代了它。
    • 添加了另一种方法,可以保留添加单独标题的功能。
    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多