Home > Software design >  Adding gridlines to a faceted graph using ggplot2
Adding gridlines to a faceted graph using ggplot2

Time:01-20

I am trying to create a more presentable graph for my research using R. I have created these faceted boxplots, but I would like them to have gridlines in the back. I am following a guide and keep running into errors.

My original code that works is:

cl.conc <- ggboxplot(
  leach.conc, x = "treatment", y = "cl_conc",
  fill = "days", palette = "RdBu",
  facet.by = "soil_type",
  xlab = "Treatment", ylab = "Chloride (mg/L)")

cl.conc

Which creates this graph: enter image description here

I am trying to add gridlines by adding this block of code:

cl.conc <- ggboxplot(
  leach.conc, x = "treatment", y = "cl_conc",
  fill = "days", palette = "RdBu",
  facet.by = "soil_type",
  xlab = "Treatment", ylab = "Chloride (mg/L)",
  theme(panel.grid.major = element_line(color = "black",
                                      size = 0.5,
                                      linetype = 2)))

cl.conc

And am recieving this error:

Error in !is.null(facet.by) | combine : 
  operations are possible only for numeric, logical or complex types

Any help is appreciated!

'''

Here is a sample block of my code:

   core_id sample_id soil_type treatment sample_period days rep year
1      MS1    MS1-0B      Silt     Solid             B    0   1    1
2      MS1     MS1-1      Silt     Solid             1    4   1    1
3      MS1     MS1-2      Silt     Solid             2   11   1    1
4      MS1     MS1-3      Silt     Solid             3   18   1    1
5      ML1    ML1-0B      Silt    Liquid             B    0   1    1
6      ML1     ML1-1      Silt    Liquid             1    4   1    1
7      ML1     ML1-2      Silt    Liquid             2   11   1    1
8      ML1     ML1-3      Silt    Liquid             3   18   1    1
9      MK1    MK1-0B      Silt       KCl             B    0   1    1
10     MK1     MK1-1      Silt       KCl             1    4   1    1
11     MK1     MK1-2      Silt       KCl             2   11   1    1
12     MK1     MK1-3      Silt       KCl             3   18   1    1
13     MC1    MC1-0B      Silt   Control             B    0   1    1
14     MC1     MC1-1      Silt   Control             1    4   1    1
15     MC1     MC1-2      Silt   Control             2   11   1    1
16     MC1     MC1-3      Silt   Control             3   18   1    1
17     FS1    FS1-0B     Clay      Solid             B    0   1    1
18     FS1     FS1-1     Clay      Solid             1    4   1    1
19     FS1     FS1-2     Clay      Solid             2   11   1    1
20     FS1     FS1-3     Clay      Solid             3   18   1    1
21     FL1    FL1-0B     Clay     Liquid             B    0   1    1
22     FL1     FL1-1     Clay     Liquid             1    4   1    1
23     FL1     FL1-2     Clay     Liquid             2   11   1    1
24     FL1     FL1-3     Clay     Liquid             3   18   1    1
25     FK1    FK1-0B     Clay        KCl             B    0   1    1
26     FK1     FK1-1     Clay        KCl             1    4   1    1
27     FK1     FK1-2     Clay        KCl             2   11   1    1
28     FK1     FK1-3     Clay        KCl             3   18   1    1
29     FC1    FC1-0B     Clay    Control             B    0   1    1
30     FC1     FC1-1     Clay    Control             1    4   1    1
31     FC1     FC1-2     Clay    Control             2   11   1    1
32     FC1     FC1-3     Clay    Control             3   18   1    1
33     MS2    MS2-0B      Silt     Solid             B    0   2    1
34     MS2     MS2-1      Silt     Solid             1    4   2    1
35     MS2     MS2-2      Silt     Solid             2   11   2    1
36     MS2     MS2-3      Silt     Solid             3   18   2    1
37     ML2    ML2-0B      Silt    Liquid             B    0   2    1
38     ML2     ML2-1      Silt    Liquid             1    4   2    1
39     ML2     ML2-2      Silt    Liquid             2   11   2    1
40     ML2     ML2-3      Silt    Liquid             3   18   2    1
41     MK2    MK2-0B      Silt       KCl             B    0   2    1
42     MK2     MK2-1      Silt       KCl             1    4   2    1
43     MK2     MK2-2      Silt       KCl             2   11   2    1
44     MK2     MK2-3      Silt       KCl             3   18   2    1
45     MC2    MC2-0B      Silt   Control             B    0   2    1
46     MC2     MC2-1      Silt   Control             1    4   2    1
47     MC2     MC2-2      Silt   Control             2   11   2    1
48     MC2     MC2-3      Silt   Control             3   18   2    1
49     FS2    FS2-0B     Clay      Solid             B    0   2    1
50     FS2     FS2-1     Clay      Solid             1    4   2    1
51     FS2     FS2-2     Clay      Solid             2   11   2    1
52     FS2     FS2-3     Clay      Solid             3   18   2    1
53     FL2    FL2-0B     Clay     Liquid             B    0   2    1
54     FL2     FL2-1     Clay     Liquid             1    4   2    1
55     FL2     FL2-2     Clay     Liquid             2   11   2    1
56     FL2     FL2-3     Clay     Liquid             3   18   2    1
57     FK2    FK2-0B     Clay        KCl             B    0   2    1
58     FK2     FK2-1     Clay        KCl             1    4   2    1
59     FK2     FK2-2     Clay        KCl             2   11   2    1
60     FK2     FK2-3     Clay        KCl             3   18   2    1
61     FC2    FC2-0B     Clay    Control             B    0   2    1
62     FC2     FC2-1     Clay    Control             1    4   2    1
63     FC2     FC2-2     Clay    Control             2   11   2    1
64     FC2     FC2-3     Clay    Control             3   18   2    1
65     MS3    MS3-0B      Silt     Solid             B    0   3    1
66     MS3     MS3-1      Silt     Solid             1    4   3    1
67     MS3     MS3-2      Silt     Solid             2   11   3    1
68     MS3     MS3-3      Silt     Solid             3   18   3    1
69     ML3    ML3-0B      Silt    Liquid             B    0   3    1
70     ML3     ML3-1      Silt    Liquid             1    4   3    1
71     ML3     ML3-2      Silt    Liquid             2   11   3    1
72     ML3     ML3-3      Silt    Liquid             3   18   3    1
73     MK3    MK3-0B      Silt       KCl             B    0   3    1
74     MK3     MK3-1      Silt       KCl             1    4   3    1
75     MK3     MK3-2      Silt       KCl             2   11   3    1
76     MK3     MK3-3      Silt       KCl             3   18   3    1
77     MC3    MC3-0B      Silt   Control             B    0   3    1
78     MC3     MC3-1      Silt   Control             1    4   3    1
79     MC3     MC3-2      Silt   Control             2   11   3    1
80     MC3     MC3-3      Silt   Control             3   18   3    1
81     FS3    FS3-0B     Clay      Solid             B    0   3    1
82     FS3     FS3-1     Clay      Solid             1    4   3    1
83     FS3     FS3-2     Clay      Solid             2   11   3    1
84     FS3     FS3-3     Clay      Solid             3   18   3    1
85     FL3    FL3-0B     Clay     Liquid             B    0   3    1
86     FL3     FL3-1     Clay     Liquid             1    4   3    1
87     FL3     FL3-2     Clay     Liquid             2   11   3    1
88     FL3     FL3-3     Clay     Liquid             3   18   3    1
89     FK3    FK3-0B     Clay        KCl             B    0   3    1
90     FK3     FK3-1     Clay        KCl             1    4   3    1
   vol_leached cl_conc  cl_load
1         2.00    3.60 0.007200
2         3.25   18.10 0.058825
3         3.00   18.10 0.054300
4         3.50   17.40 0.060900
5         2.00    3.80 0.007600
6         3.50   77.10 0.269850
7         3.25   81.40 0.264550
8         3.25   66.80 0.217100
9         2.00    4.60 0.009200
10        3.00   19.40 0.058200
11        3.00   22.30 0.066900
12        3.25   36.90 0.119925
13        2.00    4.10 0.008200
14        3.00    1.90 0.005700
15        3.00    1.20 0.003600
16        3.25    0.60 0.001950
17        2.00   18.20 0.036400
18        3.25   27.80 0.090350
19        3.00   28.30 0.084900
20        3.00   28.30 0.084900
21        2.00   15.00 0.030000
22        3.50  107.80 0.377300
23        3.00  150.30 0.450900
24        3.50   94.60 0.331100
25        2.00   12.30 0.024600
26        3.25   84.80 0.275600
27        3.00   53.40 0.160200
28        3.00   51.90 0.155700
29        2.00   17.30 0.034600
30        3.50    9.10 0.031850
31        2.50    4.25 0.010625
32        3.25    1.90 0.006175
33        1.00    3.40 0.003400
34        3.25   19.80 0.064350
35        3.50   20.70 0.072450
36        3.50   20.50 0.071750
37        1.50    2.30 0.003450
38        3.25  102.00 0.331500
39        3.50   56.70 0.198450
40        3.50   47.40 0.165900
41        1.50    2.60 0.003900
42        3.25   33.40 0.108550
43        3.50   15.30 0.053550
44        3.25   19.90 0.064675
45        1.25    2.90 0.003625
46        3.25    2.00 0.006500
47        3.25    1.20 0.003900
48        3.25    0.80 0.002600
49        1.50    8.10 0.012150
50        3.00   37.10 0.111300
51        3.50   39.80 0.139300
52        3.00   34.80 0.104400
53        1.50    4.90 0.007350
54        3.25   81.90 0.266175
55        2.75   67.50 0.185625
56        3.00   56.00 0.168000
57        1.50    5.90 0.008850
58        2.75   41.10 0.113025
59        2.75   38.30 0.105325
60        3.00   30.90 0.092700
61        1.50   17.50 0.026250
62        3.00   12.40 0.037200
63        3.00    6.00 0.018000
64        3.25    3.10 0.010075
65        2.50    2.40 0.006000
66        3.25   27.80 0.090350
67        3.25   27.80 0.090350
68        3.00   24.90 0.074700
69        2.50    2.60 0.006500
70        3.00   79.70 0.239100
71        3.50   65.50 0.229250
72        3.00   55.20 0.165600
73        2.25    2.10 0.004725
74        3.00   13.50 0.040500
75        3.25   20.40 0.066300
76        2.50   24.70 0.061750
77        2.75    2.00 0.005500
78        3.00    1.60 0.004800
79        3.00    1.20 0.003600
80        3.00    0.70 0.002100
81        3.00    8.40 0.025200
82        3.00   42.70 0.128100
83        3.25   40.50 0.131625
84        3.25   30.10 0.097825
85        3.00   10.30 0.030900
86        3.25  121.20 0.393900
87        3.25   73.60 0.239200
88        3.00   38.00 0.114000
89        2.50    9.40 0.023500
90        3.25   53.00 0.172250

CodePudding user response:

In ggpubr you could also use grids function to add grid lines. Here is a reproducible example using the ToothGrowth dataset:

library(ggpubr)
ggboxplot(ToothGrowth, 
          x = "dose", 
          y = "len", 
          facet.by = "supp",
          )  
  grids(axis = c("xy"), color = "black", size = 0.5, linetype = 2)

Created on 2023-01-19 with reprex v2.0.2

CodePudding user response:

And a second option would be to add your grid lines or any theme adjustments as an additional layer to your plot:

library(ggpubr)
#> Loading required package: ggplot2

df <- ToothGrowth

ggboxplot(
  df,
  x = "dose", y = "len",
  fill = "supp", palette = "RdBu",
  facet.by = "supp",
  xlab = "Treatment", ylab = "Chloride (mg/L)"
)  
  theme(panel.grid.major = element_line(
    color = "black",
    size = 0.5,
    linetype = 2
  ))
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.

  • Related