簡體   English   中英

如何使用多個繪圖增加plot.gam 中的邊界線寬度

[英]How to increase the border lines width in plot.gam with multiple plots

親愛的 StackOverflow 用戶,

我需要增加我的多個 GAM 圖的邊界線寬度。 我嘗試使用box(lwd=3) ,但它只適用於最后一個數字。 所以我的問題是如何增加每個圖形的邊框線寬。

這是我的代碼

Gam_AF_species <- gam(Species.AF ~ s(SST) + s(SBT, k=3) + s(Salinity) + 
                      s(Primary.productivity), data = Data)
par(mfrow = c(1,4))
plot.gam(Gam_AF_species, ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5, 
         cex.main = 3, lwd = 3, lwd.tick = 3)
box(lwd=3)

GAM 圖

我也試過不使用par(mfrow=c(1,4) ,然后數字會一一出來,我確實成功增加了每個數字的邊框線寬。但是,我有超過30個數字需要繪制,所以這種方式效率不高,希望大家給點建議,謝謝。

您需要分別繪制每個面板; 您對box(lwd = 3)調用僅生成最終圖之后才出現,因此它只能影響最終圖。

要單獨繪制各個面板,我將使用plot.gam()select參數來選擇我想要繪制的平滑。

這最容易通過創建一個快速包裝函數來完成,該函數結合了您對plot.gam()的調用box()的調用。

包裝函數可能如下所示

my_plot_fun <- function(smooth) {
  plot(m, select = smooth,
       ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5, 
       cex.main = 3, lwd = 3)
  box(lwd = 3)
}

其中所有選項都是硬編碼的,甚至是要繪制的模型,並且我們作為參數傳入的唯一想法是通過我們在plot.gam()調用中傳遞給select的參數來繪制smooth

然后我會使用一個簡單的for循環來調用包裝函數來依次繪制每個平滑。

以下是如何執行此操作的完整示例:

library('mgcv')

# simulate some data
set.seed(1)
df <- gamSim(1)

# fit the GAM
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")

# wrapper function for the plot you want
my_plot_fun <- function(smooth) {
  plot(m, select = smooth,
       ylab="", xlab="", cex.lab = 1.5, cex.axis= 1.5, 
       cex.main = 3, lwd = 3)
  box(lwd = 3)
}

# set up the plot to have 1 row and 4 columns
layout(matrix(1:4, ncol = 4))

# loop over the indices 1, 2, 3, 4 to plot each smooth in turn
for (i in seq_len(4)) {
  my_plot_fun(i)
}

# reset the device
layout(1)

您可以用par(mfrow)調用替換最后一位,如

# set up the plot to have 1 row and 4 columns
op <- par(mfrow = c(1,4))

# loop over the indices 1, 2, 3, 4 to plot each smooth in turn
for (i in seq_len(4)) {
  my_plot_fun(i)
}

# reset the device
par(op)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM