Home > Software design >  How to create a new row in tag_facets()?
How to create a new row in tag_facets()?

Time:07-02

In the first facet of the graph, I have a caption in the top corner that says, "this is interesting what now" but I would like it to show up as:

this is interesting
what now

Every time I put in \n I would like a new line, but I'm not sure how to do it in the context of tag_facets().

data(iris)
library(ggplot2)
# devtools::install_github("eliocamp/tagger")
library(tagger)


ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))   geom_point(aes(color=Species, shape=Species))  
  xlab("Sepal Length")    ylab("Sepal Width")  
  ggtitle("Sepal Length-Width")  
  tag_facets(
    tag_pool = c("this is interesting\nwhat now", "for what:\nnow", "hey:\nhow are you?"),
    tag_suffix = "",
    position = "tr")  
  theme(tagger.panel.tag.background = element_rect(fill = "white"))   facet_wrap(~Species)

Any guidance would be appreciated!

CodePudding user response:

The issue is that tagger uses gridtext::richtext_grob under the hood which allows for formatting via HTML, CSS and markdown but requires to use <br> instead of \n to add line breaks:

library(ggplot2)
library(tagger)

tag_pool <- c("this is interesting\nwhat now", "for what:\nnow", "hey:\nhow are you?")
tag_pool <- gsub("\\n", "<br>", tag_pool)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))  
  geom_point(aes(color = Species, shape = Species))  
  xlab("Sepal Length")  
  ylab("Sepal Width")  
  ggtitle("Sepal Length-Width")  
  tag_facets(
    tag_pool = tag_pool,
    tag_suffix = "",
    position = "tr"
  )  
  theme(tagger.panel.tag.background = element_rect(fill = "white"))  
  facet_wrap(~Species)

CodePudding user response:

Is this what you want?

data(iris)
library(ggplot2)

ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))   
  geom_point(aes(color=Species, shape=Species))  
  xlab("Sepal Length")    ylab("Sepal Width")  
  ggtitle("Sepal Length-Width")  
  facet_wrap(~Species)  
  geom_label(data = data.frame(x = 6, y = 5, Species = "setosa", 
                              label = "this is interesting\nwhat now"), 
                                  aes(x = x, y = y, label = label), size = 4)

enter image description here

  • Related