回答问题:如何访问直方图的最大计数?
为了创建scale_y_continuous 命令,您在每个绘图上缺少的信息是最大计数。创建ggplot 对象后,有一个很好的方法可以访问此信息,即使用来自ggplot2 的内置ggplot_build() 函数。对于给定的绘图myPlot,以下将为您提供用于绘图中每一层的数据框列表:
ggplot_build(myPlot)$data
在您的示例中,您可以访问第一个数据框的 count 列(因为您只有一个直方图几何图层)。以下是您如何编写函数来执行您需要它执行的操作。我将使用一个可以向您展示结果的示例数据集。请注意,我还更改了您的 scale_x_continuous 行,以便能够使用 min()、max() 以及 ceiling() 和 floor() 函数的组合来容纳正数和负数:
set.seed(1234)
df <- data.frame(
y1=rnorm(100,10,1),
y2=rnorm(100,12,3),
y3=rnorm(100,5,4),
y4=rnorm(100,13,5))
for (i in 1:ncol(df)) {
p <- ggplot(df, aes(df[,i])) +
geom_histogram(alpha=0.5, color='black', fill='red', binwidth=1) +
scale_x_continuous(breaks=seq(floor(min(df[,i])),ceiling(max(df[,i])))) +
ggtitle(names(df)[i])
# get max counts
max_count <- max(ggplot_build(p)$data[[1]]$count)
p <- p + scale_y_continuous(breaks=seq(0,max_count,1))
print(p)
}
有没有更好的办法?
虽然这可以满足您的需求,但通常很难迭代地处理输出到图形设备的多个绘图。我建议将上述代码重新格式化为函数,然后使用lapply() 并使用cowplot 中的plot_grid() 之类的东西来显示输出。这个建议的方法在下面的代码中有详细说明:
myPlots <- function(data, column, fill_color) {
# column = character name of column
p <- ggplot(data, aes_string(x=column)) +
geom_histogram(fill='red', binwidth=1, alpha=0.5, color='black') +
scale_x_continuous(breaks=seq(floor(min(data[column])), ceiling(max(data[column])),1)) +
ggtitle(column)
max_count <- max(ggplot_build(p)$data[[1]]$count)
p <- p + scale_y_continuous(breaks=seq(0,max_count,1))
return(p)
}
library(cowplot)
plotList <- lapply(names(df), myPlots, data=df)
plot_grid(plotlist = plotList)