0

I want to plot a picture like this and I have finished the first half of the picture. Now the problem is how to add the below text at bottom-middle (The part of the picture circled in red. The red rectangle is just an emphasis to you and I won't show it in real picture):

bst365体育投注网址大全For simplifying the question I used fake data:

library(tidyverse)
list <- split(mtcars, mtcars$cyl)

p <- list %>% imap(~ .x %>% ggplot(aes(x = mpg)) + geom_histogram())

plots <- ggarrange(p[[1]], p[[2]], p[[3]],
                   nrow = 1, ncol = 3)

text1 <- c('Category 1: Quarantinable diseases
            Category 4: Vectorborne diseases
            Category 7: Sexually transmitted diseases
                        and bloodborne infections')
text2 <- c('Category 2: Vaccine preventable diseases
            Category 5: Zoonotic infections')
text3 <- c('Category 3: Gastrointestinal or
                        enterovirus diseases
            Category 6: Bacterial infections')

text1.p <- ggparagraph(text = text1, face = "italic", size = 10, color = '#1075BC')
text2.p <- ggparagraph(text = text2, face = "italic", size = 10, color = '#EE332E')
text3.p <- ggparagraph(text = text3, face = "italic", size = 10, color = '#27B460')

Then, I combined plots with texts using ggarrange().

ggarrange(plots, 
          ggarrange(text1.p, text2.p, text3.p, ncol = 3, nrow = 2),
          ncol = 1, nrow = 2
)

The result was not what I wanted. The text was evenly distributed in the second row not at bottom-middle. To make both sides of the text blank, I add two NA in ggarrange() but failed.

ggarrange(plots, 
          NA,
          ggarrange(text1.p, text2.p, text3.p, ncol = 3, nrow = 2),
          NA,
          ncol = 1, nrow = 2
)

Also, the text didn't align as the picture I posted. For solving this I got an idea but don't know how to do it. I want to store the text into a datafram with different columns and then combine plots with the datafram. But I don't know how to do it.


text_df <- structure(list(group = c("Category 1:", "Category 4:", "Category 7:", 
                                    NA, "Category 2:", "Category 5:", "Category 3:", NA, "Category 6:"
), text = c("Quarantinable diseases", "Vectorborne diseases", 
            "Sexually transmitted diseases", "and bloodborne infections", 
            "Vaccine preventable diseases", "Zoonotic infections", "Gastrointestinal or", 
            "enterovirus diseases", "Bacterial infections"), color = c("#1075BC", 
                                                                       "#1075BC", "#1075BC", "#1075BC", "#EE332E", "#EE332E", "#27B460", 
                                                                       "#27B460", "#27B460")), row.names = c(NA, -9L), class = c("tbl_df", 
                                                                                                                                 "tbl", "data.frame"))

Any help will be highly appreciated! :)

  • 1
    the fastest way would be to save the plot as pdf or svg and add the text box using a vector graphics software such as incscape. If you need a R solution, I suggest to use cowplot for alignment of the plots and plot the text using function annotate on an empty ggplot layer. – Roman Jul 1 at 9:37
  • Alternatively, you can utilize the grid or gridExtra package, store your text data as a tableGrob, and then use custom_annotation() to display the table at a particular location. I think this or the method @Roman suggested should work. – chemdork123 Jul 1 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policybst365体育投注网址大全

Browse other questions tagged or ask your own question.