0

I have a series of plots showing environmental pollution obtained with this code :

data$Nuc[grepl("Ag-|H-3|Mn-|Am-241|Co-|Cs-|Pu-|Tc-99|Sr-90", data$Nuclides)] <- 'Artificial nuclides'
data$Nuc[!grepl("Ag-|H-3|Mn-|Am-241|Co-|Cs-|Pu-|Tc-99|Sr-90", data$Nuclides)] <- 'Natural nuclides'
data_l <- split(data,list(data$Sample.type), drop=TRUE)
lapply(names(data_l), function(i){
  ggplot(data_l[[i]], aes(Date, Activity, group = Nuclides, col = as.factor(Nuclides)))+
    colorization +
    geom_line() + scale_x_date(labels = date_format("%Y"), date_breaks = "1 year", limits = c(min(data$Date), max(data$Date))) +
    geom_point() +
    facet_grid(cols = vars(Nuc), rows = vars(Locality.Name), scales = "fixed") +
    xlab("Date") +
    ylab(paste("Activity concentrations in", data_l[[ i ]]$Unit[ 1 ])) +
    theme(legend.title=element_blank(),
          strip.text.x = element_text(size = 12, colour = "black"),
          strip.text.y = element_text(size = 12, colour = "black"),
          legend.text = element_text(size = 12)) +
    guides(col=guide_legend(ncol=1)) +
    ggsave(paste(i, ".png", sep = ""),  dpi = 400, width = 30, height = 20, units = "cm")
})dev.off()

which gives :

enter image description here

The legend, alphabetically classified, is quite hard to follow for people who do not work in the field of radioactivity and do not know which radionuclide is of natural origin and which one is an anthropogenic one.

For clarity reason, I wish to divide the legend in 2 parts : "Natural nuclides" and "Artificial nuclides" in the same way the plot is currently divided in two columns with facet_grid

I read an explanation in ggplot2: Divide Legend into Two Columns, Each with Its Own Title but it did not work in my case.

Any ideas of how to "split" the legend ?

EDIT : To help you, I give you some data using head(data, 60)

> head(data, 60)
                  Sample.type                    Locality.Name Nuclides       Date    Activity Uncertainty  Unit                 Nuc
144  North Sea aquatic plants                         OOSTENDE   Ra-226 2010-07-01   1.3892770   1.0103840 Bq/kg    Natural nuclides
167            North Sea fish                        NORTH SEA   Th-228 2010-10-01   0.3324930   0.1899960 Bq/kg    Natural nuclides
379 Freshwater aquatic plants                         MOL-GEEL     Be-7 2010-11-02   6.8424231   2.8810202 Bq/kg    Natural nuclides
380 Freshwater aquatic plants                         MOL-GEEL   Cs-137 2010-11-02   0.8282933   0.3601275 Bq/kg Artificial nuclides
478           Oysters Scheldt         HOOFDPLAAT-KLOOSTERZANDE   Th-228 2010-03-03   0.4243410   0.1795290 Bq/kg    Natural nuclides
482  North Sea aquatic plants         HOOFDPLAAT-KLOOSTERZANDE     Be-7 2010-03-03   2.2304610   1.3940381 Bq/kg    Natural nuclides
483  North Sea aquatic plants         HOOFDPLAAT-KLOOSTERZANDE   Ra-228 2010-03-03   1.3940380   0.6970191 Bq/kg    Natural nuclides
484  North Sea aquatic plants         HOOFDPLAAT-KLOOSTERZANDE   Th-228 2010-03-03   0.5157940   0.2927480 Bq/kg    Natural nuclides
485  North Sea aquatic plants         HOOFDPLAAT-KLOOSTERZANDE   Ra-226 2010-03-03   1.7983090   0.2648672 Bq/kg    Natural nuclides
486  North Sea aquatic plants         HOOFDPLAAT-KLOOSTERZANDE   Am-241 2010-03-03   2.3838050   0.4879133 Bq/kg Artificial nuclides
519 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE     Be-7 2010-06-03  66.2000000  34.3000000 Bq/kg    Natural nuclides
522 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE    Co-60 2010-06-03   4.0900000   1.7400000 Bq/kg Artificial nuclides
523 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE    I-131 2010-06-03  20.9000000   4.9500000 Bq/kg Artificial nuclides
524 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE   Ra-226 2010-06-03   7.8200000   3.3800000 Bq/kg    Natural nuclides
525 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE   Ra-228 2010-06-03  22.1000000   5.5800000 Bq/kg    Natural nuclides
526 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE   Th-228 2010-06-03  11.1000000   4.8600000 Bq/kg    Natural nuclides
527 Freshwater aquatic mosses                  ANDENNE (GIVES)     Be-7 2010-06-03 193.0000000  63.7000000 Bq/kg    Natural nuclides
529 Freshwater aquatic mosses                  ANDENNE (GIVES)    I-131 2010-06-03  47.2000000  21.1000000 Bq/kg Artificial nuclides
530 Freshwater aquatic mosses                  ANDENNE (GIVES)   Ra-228 2010-06-03  63.0000000  19.7000000 Bq/kg    Natural nuclides
531 Freshwater aquatic mosses                  ANDENNE (GIVES)   Th-228 2010-06-03  18.9000000  12.1000000 Bq/kg    Natural nuclides
532 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE     Be-7 2010-06-03 208.0000000  35.5000000 Bq/kg    Natural nuclides
534 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE    I-131 2010-06-03  11.5000000   8.8600000 Bq/kg Artificial nuclides
535 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE   Ra-226 2010-06-03  17.7000000   6.2200000 Bq/kg    Natural nuclides
536 Freshwater aquatic mosses                  AMPSIN-FLÉMALLE   Th-228 2010-06-03  19.7000000   6.8800000 Bq/kg    Natural nuclides
537 Freshwater aquatic mosses FLORIFFOUX-MORNIMONT-SAMBREVILLE     Be-7 2010-06-03 130.0000000  44.7000000 Bq/kg    Natural nuclides
539 Freshwater aquatic mosses FLORIFFOUX-MORNIMONT-SAMBREVILLE   Ra-226 2010-06-03  34.2000000   6.8400000 Bq/kg    Natural nuclides
540 Freshwater aquatic mosses FLORIFFOUX-MORNIMONT-SAMBREVILLE   Th-228 2010-06-03  33.7000000   6.0800000 Bq/kg    Natural nuclides
541 Freshwater aquatic mosses                              HUY     Be-7 2010-06-03  97.5000000  18.7000000 Bq/kg    Natural nuclides
543 Freshwater aquatic mosses                              HUY    I-131 2010-06-03  11.2000000   4.1900000 Bq/kg Artificial nuclides
544 Freshwater aquatic mosses                              HUY   Cs-137 2010-06-03   3.3300000   0.9490000 Bq/kg Artificial nuclides
545 Freshwater aquatic mosses                              HUY   Ra-226 2010-06-03  19.4000000   2.4300000 Bq/kg    Natural nuclides
546 Freshwater aquatic mosses                              HUY   Ra-228 2010-06-03  31.0000000   4.0300000 Bq/kg    Natural nuclides
547 Freshwater aquatic mosses                              HUY   Th-228 2010-06-03  21.9000000   2.5600000 Bq/kg    Natural nuclides
548 Freshwater aquatic mosses                     VISÉ (LIXHE)     Be-7 2010-06-03 203.0000000  30.9000000 Bq/kg    Natural nuclides
550 Freshwater aquatic mosses                     VISÉ (LIXHE)    Co-60 2010-06-03   3.0100000   1.8100000 Bq/kg Artificial nuclides
551 Freshwater aquatic mosses                     VISÉ (LIXHE)    I-131 2010-06-03  22.5000000   6.8600000 Bq/kg Artificial nuclides
552 Freshwater aquatic mosses                     VISÉ (LIXHE)   Cs-137 2010-06-03   4.6700000   2.6400000 Bq/kg Artificial nuclides
553 Freshwater aquatic mosses                     VISÉ (LIXHE)   Ra-226 2010-06-03  23.7000000   4.0500000 Bq/kg    Natural nuclides
554 Freshwater aquatic mosses                     VISÉ (LIXHE)   Ra-228 2010-06-03  48.3000000   9.0800000 Bq/kg    Natural nuclides
555 Freshwater aquatic mosses                     VISÉ (LIXHE)   Th-228 2010-06-03  22.8000000   4.2500000 Bq/kg    Natural nuclides
556 Freshwater aquatic plants                  AMPSIN-FLÉMALLE     Be-7 2010-06-03  99.3000000   9.6400000 Bq/kg    Natural nuclides
558 Freshwater aquatic plants                  AMPSIN-FLÉMALLE   Cs-137 2010-06-03   0.7660000   0.3580000 Bq/kg Artificial nuclides
559 Freshwater aquatic plants                  ANDENNE (GIVES)     Be-7 2010-06-03  49.8000000  14.4000000 Bq/kg    Natural nuclides
561 Freshwater aquatic plants                  ANDENNE (GIVES)   Th-228 2010-06-03   5.3400000   3.5200000 Bq/kg    Natural nuclides
562 Freshwater aquatic plants                              HUY     Be-7 2010-06-03  60.7000000   9.7700000 Bq/kg    Natural nuclides
564 Freshwater aquatic plants                              HUY   Th-228 2010-06-03   1.6700000   1.5400000 Bq/kg    Natural nuclides
565 Freshwater aquatic plants                     VISÉ (LIXHE)     Be-7 2010-06-03 174.0000000  19.8000000 Bq/kg    Natural nuclides
601 Freshwater aquatic mosses                  ANDENNE (GIVES)     Be-7 2010-09-03  68.1000000  35.9000000 Bq/kg    Natural nuclides
603 Freshwater aquatic mosses                  ANDENNE (GIVES)   Cs-137 2010-09-03   3.2300000   1.6300000 Bq/kg Artificial nuclides
604 Freshwater aquatic mosses                  ANDENNE (GIVES)   Ra-226 2010-09-03  35.2000000   2.5400000 Bq/kg    Natural nuclides
605 Freshwater aquatic mosses                  ANDENNE (GIVES)   Ra-228 2010-09-03  34.4000000   4.4800000 Bq/kg    Natural nuclides
606 Freshwater aquatic mosses                  ANDENNE (GIVES)   Th-228 2010-09-03  33.8000000   3.0500000 Bq/kg    Natural nuclides
607 Freshwater aquatic mosses                              HUY     Be-7 2010-09-03 490.0000000  86.0000000 Bq/kg    Natural nuclides
609 Freshwater aquatic mosses                              HUY   Cs-137 2010-09-03  11.7000000   5.0400000 Bq/kg Artificial nuclides
610 Freshwater aquatic mosses                              HUY   Ra-226 2010-09-03  25.3000000   6.9000000 Bq/kg    Natural nuclides
611 Freshwater aquatic mosses                              HUY   Th-228 2010-09-03  15.0000000   9.7300000 Bq/kg    Natural nuclides
612 Freshwater aquatic plants                  AMPSIN-FLÉMALLE     Be-7 2010-09-03 343.0000000  33.0000000 Bq/kg    Natural nuclides
614 Freshwater aquatic plants                  AMPSIN-FLÉMALLE   Ra-226 2010-09-03   2.5700000   0.8600000 Bq/kg    Natural nuclides
615 Freshwater aquatic plants                  AMPSIN-FLÉMALLE      H-3 2010-09-03  83.0800000   8.9400000 Bq/kg Artificial nuclides
616 Freshwater aquatic plants                  ANDENNE (GIVES)     Be-7 2010-09-03  98.2000000  26.3000000 Bq/kg    Natural nuclides 
Sylvain
  • 133
  • 1
  • 10
  • 3
    Did you look at the second answer to that question (the one using `cowplot`? It seems like it should do what you need. It will be difficult to say much more than that without a reproducible example that matches your data structure though, so if you want a working solution it would be best to edit your question to include some data. Using `dput(data)` may help you with that. – Allan Cameron Nov 09 '21 at 17:43
  • Thank you for your help. I also read the explanation using cowplot but I actually do not clearly understand how to proceed for my case – Sylvain Nov 10 '21 at 09:56

0 Answers0