0
votes

I am playing with the ggforce library and I have difficulties with displaying long text. When the text to display is a bit longer, it disappears from the plot (see example #2). Is there a way to fix that?

library(tidyverse)
library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_circle(aes(color = Species, label = "Small sentence"),
    expand = unit(0.2, "mm")
  ) +
  geom_point() +
  theme(legend.position = "none") +
  theme_minimal()

This does not work

ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_circle(aes(color = Species, label = "This is a very long text to display"),
                   expand = unit(0.2, "mm")
  ) +
  geom_point() +
  theme(legend.position = "none") +
  theme_minimal()

Created on 2019-07-25 by the reprex package (v0.3.0)

1
If you are using RStudio, try clicking on zoom. It should show the text - LocoGris
Thank you it works. However, it is still missing when I ggsave to a pdf or png. - Philippe Massicotte
How about this: p <-ggplot(iris, aes(Petal.Length, Petal.Width)) + geom_mark_circle(aes(color = Species, label = "This is a very long text to display"), expand = unit(0.2, "mm") ) + geom_point() + theme(legend.position = "none") + theme_minimal() ggsave("prueba.pdf",p, width = 10, height = 10) - LocoGris
This work for this specific example. But in mine with real data I really have to increase the width and the height of the pdf to see the annotation, even though there is plenty of space to print it. - Philippe Massicotte

1 Answers

1
votes

You can use stringr::str_wrap() in the tidyverse

ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_circle(
    aes(color = Species, label = str_wrap("This is a very long text to display", 20)),
    expand = unit(0.2, "mm")
  ) +
  geom_point() +
  theme(legend.position = "none") +
  theme_minimal()

enter image description here