我的 plot_grids 在 R 中使用 cowplot 和 ggplot2 缺少网格中的四个图之一?

My plot_grids using cowplot in R with ggplot2 are missing one of the four plots in the grid?

很抱歉我发布了多少代码,但我缺少使用 plot_grid 包 cowplot 的四个 ggplots 之一,网格中应该有四个,出于某种原因一个其中缺少了,有人知道为什么吗?

这是缺少四个图表之一的 plot_grid:

这是错误:

> plot_grid("ANN Models with 08 Learning Rate", plot3_08, plot10_08, plot20_08, plot50_08, labels = c("N3", "N10", "N20", "N50"), ncol = 2, nrow = 2)
Warning message:
In as_grob.default(plot) :
  Cannot convert object of class character into a grob.

这是可重现的代码:

library(Rcpp)
library(RSNNS)
library(ggplot2)
library(plotROC)
library(tidyr)
library(cowplot)

setwd("**set working directory**")
data <- read.csv("WDBC.csv", header=T)
data <- data[,1:4]
data <- scale(data)  # normalizes the data

numHneurons3 = 3
numHneurons10 = 10
numHneurons20 = 20
numHneurons50 = 50

DecTargets = decodeClassLabels(data[,4])

train.test3 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split
train.test10 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split
train.test20 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split
train.test50 <- splitForTrainingAndTest(data, DecTargets,ratio = 0.50) # split

model3_02 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain,  # build model3
           size = numHneurons3, learnFuncParams = c(0.02),maxit = 10000, 
           inputsTest = train.test3$inputsTest, 
           targetsTest = train.test3$targetsTest)
model3_08 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain,  # build model3
           size = numHneurons3, learnFuncParams = c(0.08),maxit = 10000, 
           inputsTest = train.test3$inputsTest, 
           targetsTest = train.test3$targetsTest)

model10_02 <- mlp(train.test10$inputsTrain, train.test10$targetsTrain, # build model10
           size = numHneurons10, learnFuncParams = c(0.02),maxit = 10000, 
           inputsTest = train.test10$inputsTest, 
           targetsTest = train.test10$targetsTest)

model10_08 <- mlp(train.test10$inputsTrain, train.test10$targetsTrain, # build model10
           size = numHneurons10, learnFuncParams = c(0.08),maxit = 10000, 
           inputsTest = train.test10$inputsTest, 
           targetsTest = train.test10$targetsTest)

model20_02 <- mlp(train.test20$inputsTrain, train.test20$targetsTrain, # build model20
           size = numHneurons20, learnFuncParams = c(0.02),maxit = 10000, 
           inputsTest = train.test20$inputsTest, 
           targetsTest = train.test20$targetsTest)

model20_08 <- mlp(train.test20$inputsTrain, train.test20$targetsTrain, # build model20
           size = numHneurons20, learnFuncParams = c(0.08),maxit = 10000, 
           inputsTest = train.test20$inputsTest, 
           targetsTest = train.test20$targetsTest)

model50_02 <- mlp(train.test50$inputsTrain, train.test50$targetsTrain, # build model50
           size = numHneurons50, learnFuncParams = c(0.02),maxit = 10000, 
           inputsTest = train.test50$inputsTest, 
           targetsTest = train.test50$targetsTest)

model50_08 <- mlp(train.test50$inputsTrain, train.test50$targetsTrain, # build model50
           size = numHneurons50, learnFuncParams = c(0.08),maxit = 10000, 
           inputsTest = train.test50$inputsTest, 
           targetsTest = train.test50$targetsTest)

trainFitTar3_02 <- cbind(fitted.values(model3_02), train.test3$targetsTrain)
predictions = predict(model3_02, train.test3$inputsTest)

trainFitTar3_08 <- cbind(fitted.values(model3_08), train.test3$targetsTrain)
predictions = predict(model3_08, train.test3$inputsTest)

trainFitTar10_02 <- cbind(fitted.values(model10_02), train.test10$targetsTrain)
predictions = predict(model10_02, train.test10$inputsTest)

trainFitTar10_08 <- cbind(fitted.values(model10_08), train.test10$targetsTrain)
predictions = predict(model10_08, train.test10$inputsTest)

trainFitTar20_02 <- cbind(fitted.values(model20_02), train.test20$targetsTrain)
predictions = predict(model20_02, train.test20$inputsTest)

trainFitTar20_08 <- cbind(fitted.values(model20_08), train.test20$targetsTrain)
predictions = predict(model20_08, train.test20$inputsTest)

trainFitTar50_02 <- cbind(fitted.values(model50_02), train.test50$targetsTrain)
predictions = predict(model50_02, train.test50$inputsTest)

trainFitTar50_08 <- cbind(fitted.values(model50_08), train.test50$targetsTrain)
predictions = predict(model50_08, train.test50$inputsTest)

#--------------------------------------
#     GGPlots of the Iterative Error:
#--------------------------------------

test_error_m302 <- model3_02$IterativeTestError
train_error_m302 <- model3_02$IterativeFitError

test_error_m308 <- model3_08$IterativeTestError
train_error_m308 <- model3_08$IterativeFitError



test_error_m1002 <- model10_02$IterativeTestError
train_error_m1002 <- model10_02$IterativeFitError

test_error_m1008 <- model10_08$IterativeTestError
train_error_m1008 <- model10_08$IterativeFitError



test_error_m2002 <- model20_02$IterativeTestError
train_error_m2002 <- model20_02$IterativeFitError

test_error_m2008 <- model20_08$IterativeTestError
train_error_m2008 <- model20_08$IterativeFitError



test_error_m5002 <- model50_02$IterativeTestError
train_error_m5002 <- model50_02$IterativeFitError

test_error_m5008 <- model50_08$IterativeTestError
train_error_m5008 <- model50_08$IterativeFitError



error_df_m3_02 <- data.frame(iter = c(seq_along(test_error_m302),
                                      seq_along(train_error_m302)),
                             Error = c(test_error_m302, train_error_m302), 
                             type = c(rep("test", length(test_error_m302)),
                                      rep("train", length(train_error_m302))
                             ))


error_df_m3_08 <- data.frame(iter = c(seq_along(test_error_m308),
                                      seq_along(train_error_m308)),
                             Error = c(test_error_m308, train_error_m308), 
                             type = c(rep("test", length(test_error_m308)),
                                      rep("train", length(train_error_m308))
                             ))



error_df_m10_02 <- data.frame(iter = c(seq_along(test_error_m1002),
                                      seq_along(train_error_m1002)),
                             Error = c(test_error_m1002, train_error_m1002), 
                             type = c(rep("test", length(test_error_m1002)),
                                      rep("train", length(train_error_m1002))
                             ))


error_df_m10_08 <- data.frame(iter = c(seq_along(test_error_m1008),
                                      seq_along(train_error_m1008)),
                             Error = c(test_error_m1008, train_error_m1008), 
                             type = c(rep("test", length(test_error_m1008)),
                                      rep("train", length(train_error_m1008))
                             ))



error_df_m20_02 <- data.frame(iter = c(seq_along(test_error_m2002),
                                       seq_along(train_error_m2002)),
                              Error = c(test_error_m2002, train_error_m2002), 
                              type = c(rep("test", length(test_error_m2002)),
                                       rep("train", length(train_error_m2002))
                              ))


error_df_m20_08 <- data.frame(iter = c(seq_along(test_error_m2008),
                                       seq_along(train_error_m2008)),
                              Error = c(test_error_m2008, train_error_m2008), 
                              type = c(rep("test", length(test_error_m2008)),
                                       rep("train", length(train_error_m2008))
                              ))



error_df_m50_02 <- data.frame(iter = c(seq_along(test_error_m5002),
                                       seq_along(train_error_m5002)),
                              Error = c(test_error_m5002, train_error_m5002), 
                              type = c(rep("test", length(test_error_m5002)),
                                       rep("train", length(train_error_m5002))
                              ))


error_df_m50_08 <- data.frame(iter = c(seq_along(test_error_m5008),
                                       seq_along(train_error_m5008)),
                              Error = c(test_error_m5008, train_error_m5008), 
                              type = c(rep("test", length(test_error_m5008)),
                                       rep("train", length(train_error_m5008))
                              ))


plot3_02 <- ggplot(error_df_m3_02[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m302))) + geom_line() + ggtitle("Error Model 3 Neurons 02 Learning Rate")

plot3_08 <- ggplot(error_df_m3_08[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m308))) + geom_line() + ggtitle("Error Model 3 Neurons 08 Learning Rate")

plot10_02 <- ggplot(error_df_m10_02[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m1002))) + geom_line() + ggtitle("Error Model 10 Neurons 02 Learning Rate")

plot10_08 <- ggplot(error_df_m10_08[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m1008))) + geom_line() + ggtitle("Error Model 10 Neurons 08 Learning Rate")

plot20_02 <- ggplot(error_df_m20_08[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m2002))) + geom_line() + ggtitle("Error Model 20 Neurons 02 Learning Rate")

plot20_08 <- ggplot(error_df_m20_08[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m2008)))  + geom_line() + ggtitle("Error Model 20 Neurons 08 Learning Rate")

plot50_02 <- ggplot(error_df_m50_02[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m2002))) + geom_line() + ggtitle("Error Model 50 Neurons 02 Learning Rate")

plot50_08 <- ggplot(error_df_m50_08[c(5000:10000, 15000:20000),], 
       aes(iter, Error, color = type, 
           each = length(test_error_m2008)))  + geom_line() + ggtitle("Error Model 50 Neurons 08 Learning Rate")

plot_grid("ANN Models with 02 Learning Rate", plot3_02, plot10_02, plot20_02, plot50_02, labels = c("N3", "N10", "N20", "N50"), ncol = 2, nrow = 2)

plot_grid("ANN Models with 08 Learning Rate", plot3_08, plot10_08, plot20_08, plot50_08, labels = c("N3", "N10", "N20", "N50"), ncol = 2, nrow = 2)

这是数据和数据框:

> head(data, 10)
       PatientID     radius    texture   perimeter
 [1,] -0.2361973  1.0960995 -2.0715123  1.26881726
 [2,] -0.2361956  1.8282120 -0.3533215  1.68447255
 [3,]  0.4313615  1.5784992  0.4557859  1.56512598
 [4,]  0.4317407 -0.7682333  0.2535091 -0.59216612
 [5,]  0.4318215  1.7487579 -1.1508038  1.77501133
 [6,] -0.2361855 -0.4759559 -0.8346009 -0.38680772
 [7,] -0.2361809  1.1698783  0.1605082  1.13712450
 [8,]  0.4326197 -0.1184126  0.3581350 -0.07280278
 [9,] -0.2361759 -0.3198854  0.5883121 -0.18391855
[10,]  0.4329621 -0.4731182  1.1044669 -0.32919213

> head(error_df)     
  iter     Error type
1    1 6913.5938 test
2    2 2981.7415 test
3    3 1906.2921 test
4    4 1425.6680 test
5    5 1157.7373 test
6    6  988.3097 test

我尝试添加 title_theme 但出现错误 object 'title_theme1' not found:

title_theme1 <- ggdraw() +
draw_label("ANN Models with 02 Learning Rate", 
             fontfamily = theme_georgia()$text$family, 
             fontface = theme_georgia()$plot.title$face, x = 0.05, hjust = 0)
plot_grid(title_theme, gridded, ncol = 1, rel_heights = c(0.2, 1))

title_theme2 <- ggdraw() +
  draw_label("ANN Models with 08 Learning Rate", 
             fontfamily = theme_georgia()$text$family, 
             fontface = theme_georgia()$plot.title$face, x = 0.05, hjust = 0)
plot_grid(title_theme, gridded, ncol = 1, rel_heights = c(0.2, 1))


#plot_grid(plot3_02, plot10_02, plot20_02, plot50_02 + rremove("x.text"),
plot_grid(title_theme1, plot3_02, plot10_02, plot20_02, plot50_02, labels = c("N3", "N10", "N20", "N50"), ncol = 2, nrow = 2)
plot_grid(title_theme2, plot3_08, plot10_08, plot20_08, plot50_08, labels = c("N3", "N10", "N20", "N50"), ncol = 2, nrow = 2)

这是一个例子:

library(tidyverse)
library(palmerpenguins)
library(cowplot)

p1 <- penguins %>%
  na.omit() %>%
  ggplot(aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  theme_bw(base_size = 14) +
  labs(x = "Bill Length (mm)",
       y = "Body Mass (g)")

p2 <- penguins %>%
  na.omit() %>%
  ggplot(aes(x = factor(species), y = body_mass_g)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(color = species),
              show.legend = FALSE,
              width = 0.25,
              alpha = 0.4,
              size = 3) +
  theme_bw(base_size = 14) +
  labs(x = "Species",
       y = "Body Mass (g)")

p3 <- penguins %>%
  na.omit() %>%
  ggplot(aes(x = bill_depth_mm, y = body_mass_g)) +
  geom_hex(bins = 10, show.legend = FALSE) +
  scale_x_continuous(expand = c(0:1)) +
  scale_y_continuous(expand = c(0,2)) +
  labs(x = "Species",
       y = "Body Mass (g)") +
  theme_bw(base_size = 14) +
    theme(legend.position = "none")

p4 <- penguins %>%
  na.omit() %>%
  ggplot(aes(x = factor(sex), y = body_mass_g)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(color = sex),
              width = 0.25,
              alpha = 0.4,
              size = 3,
              show.legend = FALSE) +
  labs(x = "Sex",
       y = "Body Mass (g)") +
  theme_bw(base_size = 14)

plot_grid("title", p1, p2, p3, p4, labels = "AUTO", label_size = 16)
#> Warning in as_grob.default(plot): Cannot convert object of class character into
#> a grob.

plot_grid(p1, p2, p3, p4, labels = "AUTO", label_size = 16)

plots <- plot_grid(p1, p2, p3, p4, labels = "AUTO", label_size = 16)
title <- ggplot() + 
  labs(title = "ANN Models with 02 Learning Rate",
       subtitle = "Subtitle here") +
  theme_minimal()
plot_grid(title, plots, ncol = 1, rel_heights = c(0.1, 0.9))


title_theme1 <- ggdraw() +
  draw_label("ANN Models with 02 Learning Rate", 
             fontfamily = theme_bw()$text$family, 
             fontface = theme_bw()$plot.title$face,
             x = 0.05, hjust = 0)
plot_grid(title_theme1, plots, ncol = 1, rel_heights = c(0.1, 0.9))

reprex package (v2.0.1)

于 2021-11-25 创建

这是否解决了您的问题?