动态轴在绘图区域的顶部和底部刻度,循环时具有一致的间隔数和一致的宽度(ggplot2,grid.arrange)

Dynamic axis ticks at top and bottom of plot area with consistent number of intervals and consistent width when looping (ggplot2, grid.arrange)

我有一些数据

set.seed(123)

df <- data.frame(rbind(
  a = rnorm(4, 60, 30),
  b = rnorm(4, 30, 15),
  c = rnorm(4, 10, 5))) 

spe_a  = rnorm(3, mean=0.05, sd=0.003)
season = factor(c('Jan','Feb','Mar'))

colnames(df) <- c("spe_b","spe_c","spe_d","spec_e")
df <- cbind(season, spe_a, df) 
# Yes I am sure there is a better method! Please advise in comments?
# I was determine to recreate my problem 

使用以下我可以生成网格图:

plot_list = list()

library(ggplot2)

for(i in colnames(df[, 2:5])){
  #
  plot <- ggplot(data = df,
                 aes_string(x = df$season, y = i)) +
    geom_bar(stat = 'identity',
             fill = 'lightblue') + # For style
    ggtitle(i) + 
    theme(axis.title.y = element_blank()) +
    scale_y_continuous(expand = c(0,0))
  #
  plot_list[[i]] = plot
  #
}

library(gridExtra)

grid.arrange(grobs = plot_list, ncol = 2)

这给了我:

太棒了!但是它有一些问题(也见图 2)

  1. 我希望刻度(和标签)始终位于绘图区域的顶部和底部。
  2. 我希望刻度具有一致的间隔数(至少是 4/5)
  3. 地块需要具有相同的地块面积。

我知道这里有几个问题,但我认为它们可能是相关的。

我确实环顾四周了

使用 patchwork 尝试这种方法并使用 seq() 设置中断。这里的代码:

library(patchwork)
library(ggplot2)
#List
plot_list = list()
#Loop
for(i in colnames(df[, 2:5])){
  #
  plot <- ggplot(data = df,
                 aes_string(x = season, y = i)) +
    geom_bar(stat = 'identity',
             fill = 'lightblue') + # For style
    ggtitle(i) + 
    theme(axis.title.y = element_blank()) +
    scale_y_continuous(expand = c(0,0),
                       limits = c(0, max(df[,i])),
                       breaks = seq(0,max(df[,i]),length.out = 5),
                       labels = function(x) round(x,2))
  #
  plot_list[[i]] = plot
  #
}
#Plot
G <- wrap_plots(plot_list,ncol = 2)

输出: