Quale linguaggio di formattazione utilizza ggtext per formattare il testo?
Sto cercando di visualizzare la notazione scientifica su un asse ggplot2 in grassetto, con il formato letterale "Ax10 ^ B", non il formato "AeB" che è l'impostazione predefinita di ggplot2. Quando viene eseguito questo codice
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"))
questo è il risultato:

Uso element_markdown()
from ggtext
perché consente di trasferire il grassetto come ho scoperto qui: Come faccio a fare in modo che i formati di testo personalizzati ggplot2 dalle funzioni di scala dell'asse seguano le specifiche di formato impostate in theme ()?
Posso correggere le virgolette doppie passando '\\1'
a \\1
(eliminando le virgolette singole). Ma ho problemi a visualizzare il segno di moltiplicazione. Potrei usare solo una minuscola x
ma è pigro.
Quando provo a usare $\times$
come suggerito quihttps://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.htmlOttengo un errore. Una vignetta per ggtext
sembra usare html:https://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.htmlma usano <sup>
tag che sembrano andare contro l'uso di ^
per fare un esponente qui, e i tag non funzionano quando li uso, e tutte le risorse per "segno di moltiplicazione in html" che ho cercato non hanno dato una soluzione . Quindi la mia domanda è: dove posso trovare una buona risorsa per imparare il linguaggio di formattazione corretto che ggtext
/ ggplot2
utilizza per le etichette delle tacche degli assi? Vorrei anche conoscere la soluzione ai problemi specifici che sto avendo.
Risposte
{ggtext} utilizza Markdown / HTML. È possibile inserire caratteri speciali utilizzando semplicemente caratteri Unicode o utilizzando entità HTML. Qui, probabilmente vuoi ×
.
Inoltre, non analizzare le stringhe in espressioni quando si lavora con {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", "×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"))
Creato il 20-08-2020 dal pacchetto reprex (v0.3.0)
Ecco una versione con solo plotmath
espressioni:
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"))
... ed ecco una versione con 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"))
Creato il 20-08-2020 dal pacchetto reprex (v0.3.0)