wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Using richtext with annotate() #8

Closed krassowski closed 4 years ago

krassowski commented 4 years ago

I have tried using the geom_richtext with annotate in two ways, both of which failed:

annotate(geom='richtext', x=8, y=4, label='<i>potential outlier?</i>')

or

annotate(geom='geom_richtext', x=8, y=4, label='<i>potential outlier?</i>')

with: Error: Can't find `geom` called "(geom_)richtext"

and

annotate(geom=GeomRichText, x=8, y=4, label='<i>potential outlier?</i>')

with:

Error in `[.unit`(padding, c(1, 3)) : 
  index out of bounds ('unit' subsetting)
Calls: <Anonymous> ... convertHeight -> convertUnit -> is.unit -> [ -> [.unit

If this is just an unexplored corner case (and not something which would require a lot of work), this could be very useful to have. A full example:

library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa</i>",
      virginica = "<i style='color:#009E73'>I. virginica</i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset**  
    <span style='font-size:9pt'>Sepal width vs. sepal length for three *Iris*
    species</span>",
    x = "Sepal length (cm)", y = "Sepal width (cm)"
  ) +
  annotate(geom='richtext', x=8, y=4, label='<i>potential outlier?</i>') +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )
krassowski commented 4 years ago

Thank you!

clauswilke commented 4 years ago

Thanks, this should work now.

Going forward, please remove any unneeded code from your reprex, to focus on the key issue (see my example below). Also, I strongly suggest using the reprex package. It makes your life easier while making certain your examples are truly reproducible.

library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  annotate(geom='richtext', x=8, y=4, label='<i>potential outlier?</i>') +
  coord_cartesian(clip = "off")

Created on 2019-11-27 by the reprex package (v0.3.0)

krassowski commented 4 years ago

Sorry about that (I will limit the examples to minimum), but I cannot get reprex working for me (it was working in RStudio on my old PC, but because it was not updated for Ubuntu 19.10 and install fails with security issues I had to give up; then I was fighting with getting reprex to display the markdown instead of rendering in the browser, but could not - there are issues open for these problems already).

I am only saying that it is not so easy for everyone to make use of reprex as it may seem, despite an honest effort and an hour less of my time spend on fighting with it :(