ggtextはテキストのフォーマットにどのフォーマット言語を使用しますか?

Aug 19 2020

ggplot2のデフォルトである「AeB」形式ではなく、文字通りの「Ax10 ^ B」形式で、ggplot2軸に科学的記数法を太字で表示しようとしています。このコードが実行されるとき

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()fromを使用しggtextます。軸スケール関数からggplot2カスタムテキストフォーマットをtheme()で設定されたフォーマット仕様に従うにはどうすればよいですか?

私は変更することで、二重引用符を修正することができます'\\1'\\1(単一引用符を削除します)。しかし、乗算記号を表示するのに問題があります。小文字を使用することもできますxが、それは怠惰です。

$\times$ここで提案されているように使用しようとするとhttps://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.htmlエラーが発生します。のビネットはggtexthtmlを使用しているようです:https://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.htmlしかし、ここで指数を作成するために<sup>使用することに反しているように見えるタグを使用し^ており、タグを使用すると機能しません。また、検索した「乗算サインインhtml」のすべてのリソースで解決策が得られませんでした。 。だから私の質問は:軸目盛りラベルにggtext/ggplot2使用する適切なフォーマット言語を学ぶための良いリソースをどこで見つけることができますか?また、私が抱えている特定の問題の解決策を知りたいです。

回答

1 ClausWilke Aug 20 2020 at 14:32

{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に作成されました

user12728748 Aug 20 2020 at 05:29

これは、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に作成されました