如何轻松地为两个直方图保持相同的轴刻度
How to easily maintain the same axis scale for two histograms
看到了一些类似的代码,但我很好奇 "gridExtra" 包是否可以做到这一点。我想在 x 轴和 y 轴的相同比例上显示这些。箱线图也需要这样做。使用 grid.arrange() 有更简单的方法吗?
library(tidyverse)
library(gridExtra)
#Subset of Data
za <- structure(list(sodium = c(1.77, 1.79, 1.63, 1.61, 1.64, 1.65,
1.58, 1.75, 1.71, 1.66), cal = c(4.93, 4.84, 4.95, 4.74, 4.67,
4.67, 4.63, 4.72, 4.93, 4.95)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))
#Histogram
sodHist <- ggplot(data = za, mapping = aes(x = sodium)) +
geom_histogram(fill = "royalblue1", color = "white") +
ggtitle("Sodium Distribution of Pizzas")
calHist <- ggplot(data = za, mapping = aes(x = cal)) +
geom_histogram(fill = "limegreen", color = "white") +
ggtitle("Calorie Distribution of Pizzas")
grid.arrange(sodHist,calHist, ncol = 2)
我会像r2evans建议的那样使用分面
za %>%
mutate(num = row_number()) %>% # add rownumbers to allow the pivoting
pivot_longer(-num, names_to = "atom", values_to = "val") %>%
ggplot(aes(x = val, col = atom, fill = atom)) +
geom_histogram() +
facet_wrap(~atom)
问题是您将两个 grobs 送入 grid.arrange
并且轴已经设置好。使 y 轴相等的最干净的方法可能是 "crack open" 底层 ggplots 并使它们的轴在内部相等:
grid.arrange.equal <- function(plotA, plotB, ...)
{
A <- ggplot_build(plotA)
B <- ggplot_build(plotB)
if(A$layout$panel_scales_y[[1]]$range$range[2] > B$layout$panel_scales_y[[1]]$range$range[2])
B$layout$coord$limits$y <- A$layout$panel_scales_y[[1]]$range$range
else
A$layout$coord$limits$y <- B$layout$panel_scales_y[[1]]$range$range
grid.arrange(A$plot, B$plot, ...)
}
所以现在你可以这样做:
grid.arrange.equal(calHist, sodHist, ncol=2)
您的示例中的数据有点短,无法提供漂亮的直方图,但您明白了:
具有更真实的虚拟数据:
看到了一些类似的代码,但我很好奇 "gridExtra" 包是否可以做到这一点。我想在 x 轴和 y 轴的相同比例上显示这些。箱线图也需要这样做。使用 grid.arrange() 有更简单的方法吗?
library(tidyverse)
library(gridExtra)
#Subset of Data
za <- structure(list(sodium = c(1.77, 1.79, 1.63, 1.61, 1.64, 1.65,
1.58, 1.75, 1.71, 1.66), cal = c(4.93, 4.84, 4.95, 4.74, 4.67,
4.67, 4.63, 4.72, 4.93, 4.95)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))
#Histogram
sodHist <- ggplot(data = za, mapping = aes(x = sodium)) +
geom_histogram(fill = "royalblue1", color = "white") +
ggtitle("Sodium Distribution of Pizzas")
calHist <- ggplot(data = za, mapping = aes(x = cal)) +
geom_histogram(fill = "limegreen", color = "white") +
ggtitle("Calorie Distribution of Pizzas")
grid.arrange(sodHist,calHist, ncol = 2)
我会像r2evans建议的那样使用分面
za %>%
mutate(num = row_number()) %>% # add rownumbers to allow the pivoting
pivot_longer(-num, names_to = "atom", values_to = "val") %>%
ggplot(aes(x = val, col = atom, fill = atom)) +
geom_histogram() +
facet_wrap(~atom)
问题是您将两个 grobs 送入 grid.arrange
并且轴已经设置好。使 y 轴相等的最干净的方法可能是 "crack open" 底层 ggplots 并使它们的轴在内部相等:
grid.arrange.equal <- function(plotA, plotB, ...)
{
A <- ggplot_build(plotA)
B <- ggplot_build(plotB)
if(A$layout$panel_scales_y[[1]]$range$range[2] > B$layout$panel_scales_y[[1]]$range$range[2])
B$layout$coord$limits$y <- A$layout$panel_scales_y[[1]]$range$range
else
A$layout$coord$limits$y <- B$layout$panel_scales_y[[1]]$range$range
grid.arrange(A$plot, B$plot, ...)
}
所以现在你可以这样做:
grid.arrange.equal(calHist, sodHist, ncol=2)
您的示例中的数据有点短,无法提供漂亮的直方图,但您明白了:
具有更真实的虚拟数据: