2

我试图在 ggplot2 轴上以粗体显示科学记数法,使用文字“Ax10^B”格式,而不是 ggplot2 默认的“AeB”格式。运行此代码时

library(tidyverse)
library(ggtext)
ggplot(mpg, aes(displ, hwy*10^9)) + geom_point()


#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
  # turn in to character string in scientific notation
  l <- format(l, scientific = TRUE)
  # quote the part before the exponent to keep all the digits
  l <- gsub("^(.*)e", "'\\1'e", l)
  # turn the 'e+' into plotmath format
  l <- gsub("e", "%*%10^", l)
  # return this as an expression
  parse(text=l)
}


ggplot(mpg, aes(displ, hwy*10^9)) + 
  theme_classic() +
  geom_point() + 
  scale_y_continuous(labels= fancy_scientific)  +
  theme(text = element_text(face = "bold"), 
        axis.text.y = element_markdown(face = "bold")) 


这是结果: 代码输出

我使用element_markdown()fromggtext是因为它允许像我在这里发现的那样转移粗体:如何使轴比例函数中的 ggplot2 自定义文本格式遵循主题()中设置的格式规范?

'\\1'我可以通过更改为\\1(删除单引号)来修复双引号。但我无法让乘号显示。我可以只使用小写字母x,但这很懒惰。

当我尝试$\times$按照此处的建议使用https://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.html时 出现错误。一个小插图ggtext似乎使用 html:https ://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.html但他们使用<sup>的标签似乎不利于在^此处制作指数,并且这些标签在我使用它们时不起作用,并且我搜索的“乘法登录 html”的所有资源都没有产生解决方案。所以我的问题是:我在哪里可以找到一个很好的资源来学习ggtext/ggplot2用于轴刻度标签的正确格式语言?也想知道我遇到的具体问题的解决方案。

4

2 回答 2

2

{ggtext} 使用 Markdown/HTML。您可以仅使用 unicode 字符或使用 HTML 实体插入特殊字符。在这里,您可能想要&times;.

另外,在使用 {ggtext} 时不要将字符串解析为表达式。

library(tidyverse)
library(ggtext)

#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
  # turn in to character string in scientific notation
  l <- format(l, scientific = TRUE)
  # quote the part before the exponent to keep all the digits
  l <- gsub("^(.*)e", "\\1e", l)
  # turn the 'e+' into plotmath format
  l <- gsub("e", "&times;10^", l)
  # return this as a string
  l
}


ggplot(mpg, aes(displ, hwy*10^9)) + 
  theme_classic() +
  geom_point() + 
  scale_y_continuous(labels= fancy_scientific)  +
  theme(text = element_text(face = "bold"), 
        axis.text.y = element_markdown(face = "bold")) 

reprex 包(v0.3.0)于 2020-08-20 创建

于 2020-08-20T14:32:03.090 回答
0

这是一个只有plotmath表达式的版本:

library(dplyr)
library(ggplot2)

fancy_scientific <- function(l) {
    l <- format(l, scientific = TRUE)
    parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)", 
        "bold('\\1') * bold(' * ') * bold('10')^bold('\\3')", l))
}

mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>% 
    ggplot(aes(displ, hwy)) + 
    theme_classic() +
    geom_point() + 
    scale_y_continuous(labels= fancy_scientific) +
    theme(text = element_text(face = "bold"))

...这是一个版本ggtext

library(dplyr)
library(ggplot2)
library(ggtext)

fancy_scientific <- function(l) {
    l <- format(l, scientific = TRUE)
    parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)", "\\1 * 10^(\\3)", l))

    ## this would also work, instead of the line above:
    # gsub("(.*)e(\\+?)(\\-?[0-9]+)", "**\\1 \\* 10<sup>\\3</sup>**", l)
}

mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>% 
    ggplot(aes(displ, hwy)) + 
    theme_classic() +
    geom_point() + 
    scale_y_continuous(labels= fancy_scientific)  +
    theme(text = element_text(face = "bold"), 
          axis.text.y = element_markdown(face = "bold"))

reprex 包(v0.3.0)于 2020-08-20 创建

于 2020-08-20T05:29:17.987 回答