Home > database >  insert a legend to a distinct graph
insert a legend to a distinct graph

Time:10-26

I have one phylogenetic tree as picture below.

enter image description here

I did make a fake dataframe to get the legend, and now I would like to insert that legend to the center of phylogenetic tree. I used grid.arrange but I could not move the legend position.

The fake legend that I created

df <- data.frame(name = c("ANA-GRADE", "ASTERIDS","BASAL EUDICOTS", "BASAL SUPERASTERIDS",
                          "CHOLORANTHALES", "MAGNOLIIDS", "MONOCOTS","ROSIDS",
                          "ANA-GRADE", "ASTERIDS","BASAL EUDICOTS", "BASAL SUPERASTERIDS",
                          "CHOLORANTHALES", "MAGNOLIIDS", "MONOCOTS","ROSIDS"),
                 colour = c("#EFF13A", "#428d34", "#84cf70", "#62a852", "#CDD100", 
                            "#8B5742", "#EEAD0E", "#2d7a21", "#EFF13A", "#428d34", 
                            "#84cf70", "#62a852", "#CDD100", "#8B5742", "#EEAD0E", "#2d7a21"),
                 x1 = runif(16, 5,100),
                 y1 = runif(16, 20,50))

fake <- df %>% ggplot(aes(x1,y1, colour = name))  
  geom_line(size = 1)  
  scale_colour_manual(values = c("#EFF13A", "#428d34", "#84cf70", "#62a852", "#CDD100", 
                       "#8B5742", "#EEAD0E", "#2d7a21"),
                      name = "Major clade")  
  theme_bw()  
  theme(legend.title = element_text(face = "bold",size = 12))

legend <- get_legend(fake)

# using grid.arrange

Does anyone has suggestion for me using grid.arrange?

CodePudding user response:

The new ggdraw function from cowplot provides one option.

It accepts any grob, not just ggplot objects.

library(tidyverse)
library(cowplot)

df <- data.frame(name = c("ANA-GRADE", "ASTERIDS","BASAL EUDICOTS", "BASAL SUPERASTERIDS",
                          "CHOLORANTHALES", "MAGNOLIIDS", "MONOCOTS","ROSIDS",
                          "ANA-GRADE", "ASTERIDS","BASAL EUDICOTS", "BASAL SUPERASTERIDS",
                          "CHOLORANTHALES", "MAGNOLIIDS", "MONOCOTS","ROSIDS"),
                 colour = c("#EFF13A", "#428d34", "#84cf70", "#62a852", "#CDD100", 
                            "#8B5742", "#EEAD0E", "#2d7a21", "#EFF13A", "#428d34", 
                            "#84cf70", "#62a852", "#CDD100", "#8B5742", "#EEAD0E", "#2d7a21"),
                 x1 = runif(16, 5,100),
                 y1 = runif(16, 20,50))

fake <- 
  df %>% ggplot(aes(x1,y1, colour = name))  
  geom_line(size = 1)  
  scale_colour_manual(values = c("#EFF13A", "#428d34", "#84cf70", "#62a852", "#CDD100", 
                                 "#8B5742", "#EEAD0E", "#2d7a21"),
                      name = "Major clade")  
  theme_bw()  
  theme(legend.title = element_text(face = "bold",size = 12))


sample_donut_plot <- 
  data.frame() %>%
  ggplot(aes(ymin = 0, ymax = 1, xmin = 3, xmax = 4), )   
  geom_rect()   
  coord_polar(theta="y")  
  xlim(c(0, 4))  
  theme_void()

legend <- cowplot::get_legend(fake)

cowplot::ggdraw(sample_donut_plot)  
  cowplot::draw_plot(legend, x = .25, y = .25, width = .5, height = .5) 

Created on 2021-10-25 by the reprex package (v2.0.1)

  • Related