Is it possible to split the text in my hoover into multiple lines?

Viewed 25

I have a graph with a hoover. In this I show a list of names. Sometimes this list is too long and I would like my hoover to split the names into several lines.

So the input for my hoover is this: all_names = paste(unique(names_x), collapse = ', ')

And in 'text' I put this variable. How do I adjust this format to insert a max length of the line or something like that?

p <- ggplot(dataframe, aes(x=reorder(f_names,n_totaal), y=n,
                                    fill=jaar %>% as.factor(),
                                    text=c(paste0(n,": ",all_names))))+
               geom_bar(stat="identity",
               position=position_stack(reverse = TRUE)) +
               coord_flip()+
               reverse = TRUE,
               zero_axis = "x")+
               scale_y_continuous(breaks= pretty_breaks())

To clarify; the hoover is now 'Anna, Lisa, Bert, Otto, Max, Andreas, Kay, Michaella, Joost, Benny, Howard'. That is very lang and I would like the hoover to break automaticcaly after length X. Then the hoover would be:

'Anna, Lisa, Bert, Otto, Max, Andreas, Kay, Michaella, Joost, Benny, Howard'

1 Answers

I recommend stringr package and combining two things:

  1. use stringr::str_wrap to split long text into chunks of desirable length
  2. use stringr::str_replace_all to replace defualt \n with <br> tag

So assuming you have column called long_text in your dataframe which contains long text you may do this:

dataframe$new_text <- str_wrap(string = dataframe$long_text, width = desired_length)
dataframe$new_text <- str_replace_all(string = dataframe$new_text, pattern = "\\n", replacement = "<br>")

and pass this new column as one to hover.

Related