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()에서 ggtext그것이 내가 여기 발견으로 굵은면이 전송 될 수 있기 때문에 : 나는 축 스케일 기능에서 ggplot2 사용자 정의 텍스트 형식을 어떻게 테마 설정 형식 스펙에 따라 ()?

나는 변경하여 따옴표를 해결할 수 '\\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을 사용합니다. 유니 코드 문자 만 사용하거나 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")) 

2020-08-20에 reprex 패키지 (v0.3.0)로 생성됨

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"))

2020-08-20에 reprex 패키지 (v0.3.0)로 생성됨