Formattazione del testo nell'annotazione di ggplot
Aug 23 2020
È possibile annotare con codice html? Sto cercando di colorare solo poche parole e non l'intero testo.
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.0.2
mtcars %>%
ggplot(aes(x = hp, y = mpg)) +
geom_point() +
annotate(geom = "text", label = "I'm <span style='color: red;'>red</span> \n and i'm <span style='color: orange;'>orange</span>",
x = 250, y = 25)
Creato il 22-08-2020 dal pacchetto reprex (v0.3.0)
Risposte
5 PedroAphalo Aug 23 2020 at 02:36
Puoi usare il pacchetto "ggtext". È abbastanza nuovo. L'unico cambiamento necessario per il tuo esempio è sostituire il geom: using "richtext"
invece di "text"
.
library(tidyverse)
library(ggtext)
#> Warning: package 'ggplot2' was built under R version 4.0.2
mtcars %>%
ggplot(aes(x = hp, y = mpg)) +
geom_point() +
annotate(geom = "richtext", label = "I'm <span style='color: red;'>red</span> \n and i'm <span style='color: orange;'>orange</span>",
x = 250, y = 25)

È possibile utilizzare fill = NA
per rimuovere lo sfondo. Per rimuovere la linea di confine label.color = NA
può essere utilizzato.
library(tidyverse)
library(ggtext)
mtcars %>%
ggplot(aes(x = hp, y = mpg)) +
geom_point() +
annotate(geom = "richtext", label = "I'm <span style='color: red;'>red</span>\n and i'm <span style='color: orange;'>orange</span>",
x = 250, y = 25, fill = NA, label.color = NA)
