I have a categorical random forest model with 3 classes of species assemblages and 18 variables, and I would like to build partial dependence plots for each class and each variable. I have the following lines of code for building four plots (how each class interacts with that variable "depth")
pdp1.1 <- partial(RFmodel, pred.var = "depth", plot=TRUE, which.class=1, train=train.df, plot.engine= "ggplot")
pdp1.2 <- partial(model.rf.rf, pred.var = "a_bathym", plot=TRUE, which.class=2, train=train.df, plot.engine= "ggplot")
pdp1.3 <- partial(model.rf.rf, pred.var = "a_bathym", plot=TRUE, which.class=3, train=train.df, plot.engine= "ggplot")
Is there a way to make it so the results from each partial dependence plot can be combined into a single graph with the same x / y axis?
I'm happy to provide any additional information/code if it helps!
CodePudding user response:
Here is a solution for the basic ggplot fig objects, I don't if it will work for you. Meanwhile, I will try to make some PDP models and update this.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggpubr)
p1 <- iris %>% ggplot(aes(Sepal.Length, Petal.Length)) geom_point()
p2 <- iris %>% ggplot(aes(Sepal.Length, Petal.Length)) geom_point()
fig <- ggarrange(p1 rremove("xlab") rremove("ylab"),
p2 rremove("xlab") rremove("ylab"),
common.legend = T)
fig %>% annotate_figure(left = text_grob("your y-axis", rot = 90),
bottom = text_grob("your x-axis"))
Created on 2022-02-19 by the reprex package (v2.0.1)