0

I have the following data:

df4 <- structure(list(Domain = c("Archaea", "Archaea", "Archaea", "Archaea", 
                             "Archaea", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", 
                             "Bacteria", "Bacteria", "Bacteria", "Eukaryota", "Eukaryota", 
                             "Eukaryota"), Phylum = c("Crenarchaeota", "Euryarchaeota", "Korarchaeota", 
                                                      "Nanoarchaeota", "Thaumarchaeota", "Acidobacteria", "Aquificae", 
                                                      "Candidatus Poribacteria", "Chlamydiae", "Chlorobi", "Chloroflexi", 
                                                      "Chrysiogenetes", "Cyanobacteria", "Apicomplexa", "Arthropoda", 
                                                      "Ascomycota"), Alignment_Length = c(3573, 34060, 257, 22, 525, 
                                                                                          85973, 8825, 1273, 5962, 41954, 169450, 1783, 117562, 1514, 5848, 
                                                                                          12221), ymax = c(3573, 37633, 37890, 37912, 38437, 124410, 133235, 
                                                                                                           134508, 140470, 182424, 351874, 353657, 471219, 472733, 478581, 
                                                                                                           490802), ymin = c(0, 3573, 37633, 37890, 37912, 38437, 124410, 
                                                                                                                             133235, 134508, 140470, 182424, 351874, 353657, 471219, 472733, 
                                                                                                                             478581)), row.names = c(NA, -16L), class = c("tbl_df", "tbl", 
                                                                                                                                                                          "data.frame"))

I made the following 2-tiered pie chart:

ggplot(df4) +
  geom_rect(aes(fill=Phylum, ymax=ymax, ymin=ymin, xmax=4, xmin=2)) +
  geom_rect(aes(fill=Domain, ymax=ymax, ymin=ymin, xmax=2, xmin=0)) +
  xlim(c(0, 4)) +
  theme(aspect.ratio=1) +
  coord_polar(theta="y") +
  theme_void()

enter image description here

The inner tier represents domain and the outer tier represents phylum. The legend treats them both the same, though - it lumps all of the domains and phyla together. I would like to have two side-by-side legends with one for domain and one for phylum. Both legends should have their own title (i.e., either "Domain" or "Phylum"). Is it possible to do this? Thank you for your help.

David Moore
  • 670
  • 3
  • 15

1 Answers1

1

using relayer package you can get the desired output.

df4 <- df4a[1:7,]

fill_group1 <- unique(df4$Phylum)
fill_group2 <- unique(df4$Domain)

col_values <- c(Crenarchaeota="red", Euryarchaeota="blue", Korarchaeota="green", Nanoarchaeota="orange", Thaumarchaeota="brown",
                Acidobacteria="purple", Aquificae="yellow", Archaea="darkgreen", Bacteria="cyan" )

col_black <- rep("black", length(col_values)) 

library(relayer)

ggplot(df4) +
  geom_rect(aes(fill=Phylum, ymax=ymax, ymin=ymin, xmax=4, xmin=2)) +
  scale_fill_manual(aesthetics = "fill", values = col_values,
                    breaks =  fill_group1 , name = "Phylum") +
  #new_scale_fill() +  ### from ggnewscale
  geom_rect(aes(fill2=Domain, ymax=ymax, ymin=ymin, xmax=2, xmin=0)) %>% rename_geom_aes(new_aes = c(fill = "fill2")) +
  
  scale_fill_manual(aesthetics = "fill2", 
                      values = col_values,
                      breaks = fill_group2, name = "Domain") +
    xlim(c(0, 4)) +
    theme(aspect.ratio=1) +
    coord_polar(theta="y") +
    theme_void()

output

YBS
  • 19,324
  • 2
  • 9
  • 27
  • This gets at what I'm trying to do, but now all of the different segments in the inner pie chart are filled with the same color, and they have lines going through them where they shouldn't (i.e., where the outer pie slices are segmented). I do like how the legend is split up, though, even though I don't fully understand how the coding works for that. – David Moore Sep 23 '20 at 15:25
  • @DavidMoore, Please check the updated code. Sorry, I only did the first seven records. You can assign colors differently. – YBS Sep 23 '20 at 21:38
  • This is great. Thank you very much. Can you tell me how I'd have to change the code if I wanted to use the 'ggnewscale' package instead of the 'relayer' package? – David Moore Sep 24 '20 at 00:13
  • I have not used it myself, but you can see the example here : https://stackoverflow.com/questions/27803710/ggplot2-divide-legend-into-two-columns-each-with-its-own-title/62058690#62058690. – YBS Sep 24 '20 at 00:46