【问题标题】:Scientific notation in ggplotggplot中的科学记数法
【发布时间】:2020-12-30 03:45:43
【问题描述】:

谁能告诉我如何在我的绘图轴中使用科学记数法以这种方式与 ggplot 一起使用?

我想只在角落写指数部分,如图所示(该图是用 matplotlib 制作的)。

【问题讨论】:

    标签: r ggplot2 plot axis-labels scientific-notation


    【解决方案1】:

    我能想到的最好的办法是使用annotate() 函数并禁用坐标裁剪将文本放置在面板边界之外。您可能需要稍微调整一下确切的间距,表达式会引发警告。

    
    library(ggplot2)
    
    df <- data.frame(
      x = 1:10,
      y = 10:1
    )
    
    ggplot(df, aes(x, y)) +
      geom_point() +
      annotate("text", -Inf, Inf, label = expression(""%*%10^1),
               hjust = 0, vjust = -1) +
      annotate("text", Inf, -Inf, label = expression(""%*%10^-3),
               hjust = 1, vjust = 2) +
      coord_cartesian(clip = "off") +
      theme(plot.margin = margin(t = 30, b = 10, l = 10, r = 10))
    #> Warning in is.na(x): is.na() applied to non-(list or vector) of type
    #> 'expression'
    
    #> Warning in is.na(x): is.na() applied to non-(list or vector) of type
    #> 'expression'
    

    reprex package (v0.3.0) 于 2020-09-11 创建

    【讨论】:

    • 感谢您的回答。但是,我想自动编写指数,而不必手动传递它或编写额外的代码来计算它。
    • 我认为 ggplot2 中不存在特定的注释方式,但您可能可以在 ggplot2 的轴系统上编写扩展来执行此操作。然而,这不是一项微不足道的任务,而且肯定需要额外的代码才能弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    相关资源
    最近更新 更多