如何垂直对齐条形图以获得具有不同 x 数的每个条形的相同宽度
How to align bar plots vertically to get same width for each bar with different numbers of x
我正在使用 cowplot 包来创建绘图网格。当我想垂直绘制两个宽度不同的图时,我的问题就来了。这是一个例子:
library(dplyr)
library(ggplot2)
library(cowplot)
plot1 = iris %>%
ggplot(aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_col()
plot2 = iris %>%
filter(Species != 'virginica') %>%
ggplot(aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_col()
w1 = max(layer_data(plot1, 1)$x)
w2 = max(layer_data(plot2, 1)$x)
plot_grid(plot1, plot2, align = 'v', ncol = 1, rel_widths = c(w1, w2), axis = 'l')
正如您在代码中看到的那样,我使用 layer_data() 函数来提取图中有多少列,因为我想 运行 它递归地,有时,一些组被删除,所以我确保列数。因此,目标是从不同的地块垂直对齐列。在前面的代码中,rel_width 参数没有效果。
我试过这样的方法:
plot_grid(plot1,
plot_grid(plot2, NA, align = 'h', ncol = 2, rel_widths = c(w2, w1-w2)),
align = 'v', ncol = 1, axis = 'lr')
但它没有按预期工作并且取决于 w1 > w2。一些帮助将不胜感激
已编辑:
可能因为之前的代码有点混乱,我添加了一个新代码,它创建了两个不同的数据帧来绘制。目标是对齐两个图中的 x 轴。不需要图例对齐,只需要 x 轴。
library(ggplot2)
library(cowplot)
d1 = data.frame(length = c('large', 'medium', 'small'),
meters = c(100, 50, 30))
d2 = data.frame(speed = c('high', 'slow'),
value =c(200, 45))
p1 = ggplot(d1, aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d()
p2 = ggplot(d2, aes(x = speed, y = value, fill = speed)) +
geom_col()
p_ls = list(p1, p2)
n_x = sapply(p_ls, function(p) {
max(layer_data(p, 1)$x)
})
plot_grid(plotlist = p_ls, align = 'v', ncol = 1, rel_widths = n_x)
来自?plot_grid
:
rel_widths
(optional) Numerical vector of relative columns widths. For example, in a two-column grid, rel_widths = c(2, 1) would make the first column twice as wide as the second column.
参数 rel_widths
在单列绘图网格中不执行任何操作。
您可能需要使用适当的尺寸手动调用 cowplot::draw_plot
以将绘图放在您想要的位置。
首先,我认为如果不进行一些严肃的黑客攻击,这是不可能的。我认为通过一些变通办法你会过得更好。
我的第一个答案(这里现在是第二个选项)是创建假因子水平。这当然会带来类别的完美对齐。
另一个选项(这里现在是选项 1)是使用 expand 参数。下面是一种编程方法。
我添加了一个矩形,让它看起来好像没有进一步的情节。这可以通过主题的相应背景填充来完成。
但最后,我仍然认为您可以通过刻面获得更好、更容易的结果。
一个选项
library(ggplot2)
library(cowplot)
d1 = data.frame(length = c('large', 'medium', 'small'), meters = c(100, 50, 30))
d2 = data.frame(speed = c('high', 'slow'), value =c(200, 45))
d3 = data.frame(key = c('high', 'slow', 'veryslow', 'superslow'), value = 1:4)
n_unq1 <- length(d1$length)
n_unq2 <- length(d2$speed)
n_unq3 <- length(d3$key)
n_x <- max(n_unq1, n_unq2, n_unq3)
#p1 =
expand_n <- function(n_unq){
if((n_x - n_unq)==0 ){
waiver()
} else {
expansion(add = c(0.6, (n_x-n_unq+0.56)))
}
}
p1 <-
ggplot(d1, aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq1)) +
annotate(geom = 'rect', xmin = n_unq1+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p2 <-
ggplot(d2, aes(x = speed, y = value, fill = speed)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq2)) +
annotate(geom = 'rect', xmin = n_unq2+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p3 <-
ggplot(d3, aes(x = key, y = value, fill = key)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq3)) +
annotate(geom = 'rect', xmin = n_unq3+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p_ls = list(p1, p2,p3)
plot_grid(plotlist = p_ls, align = 'v', ncol = 1)
由 reprex package (v0.3.0)
于 2020-04-24 创建
选项 2,创建 n 个假因子水平直到图的最大水平,然后使用 drop = FALSE
。这是一种编程方法
library(tidyverse)
library(cowplot)
n_unq1 <- length(d1$length)
n_unq2 <- length(d2$speed)
n_unq3 <- length(d3$key)
n_x <- max(n_unq1, n_unq2, n_unq3)
make_levels <- function(x, value) {
x[[value]] <- as.character(x[[value]])
l <- length(unique(x[[value]]))
add_lev <- n_x - l
if (add_lev == 0) {
x[[value]] <- as.factor(x[[value]])
x
} else {
dummy_lev <- map_chr(1:add_lev, function(i) paste(rep(" ", i), collapse = ""))
x[[value]] <- factor(x[[value]], levels = c(unique(x[[value]]), dummy_lev))
x
}
}
list_df <- list(d1, d2, d3)
list_val <- c("length", "speed", "key")
fac_list <- purrr::pmap(.l = list(list_df, list_val), function(x, y) make_levels(x = x, value = y))
p1 <-
ggplot(fac_list[[1]], aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq1 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p2 <-
ggplot(fac_list[[2]], aes(x = speed, y = value, fill = speed)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq2 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p3 <-
ggplot(fac_list[[3]], aes(x = key, y = value, fill = key)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq3 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p_ls <- list(p1, p2, p3)
plot_grid(plotlist = p_ls, align = "v", ncol = 1)
由 reprex package (v0.3.0)
于 2020-04-24 创建
我正在使用 cowplot 包来创建绘图网格。当我想垂直绘制两个宽度不同的图时,我的问题就来了。这是一个例子:
library(dplyr)
library(ggplot2)
library(cowplot)
plot1 = iris %>%
ggplot(aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_col()
plot2 = iris %>%
filter(Species != 'virginica') %>%
ggplot(aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_col()
w1 = max(layer_data(plot1, 1)$x)
w2 = max(layer_data(plot2, 1)$x)
plot_grid(plot1, plot2, align = 'v', ncol = 1, rel_widths = c(w1, w2), axis = 'l')
正如您在代码中看到的那样,我使用 layer_data() 函数来提取图中有多少列,因为我想 运行 它递归地,有时,一些组被删除,所以我确保列数。因此,目标是从不同的地块垂直对齐列。在前面的代码中,rel_width 参数没有效果。
我试过这样的方法:
plot_grid(plot1,
plot_grid(plot2, NA, align = 'h', ncol = 2, rel_widths = c(w2, w1-w2)),
align = 'v', ncol = 1, axis = 'lr')
但它没有按预期工作并且取决于 w1 > w2。一些帮助将不胜感激
已编辑:
可能因为之前的代码有点混乱,我添加了一个新代码,它创建了两个不同的数据帧来绘制。目标是对齐两个图中的 x 轴。不需要图例对齐,只需要 x 轴。
library(ggplot2)
library(cowplot)
d1 = data.frame(length = c('large', 'medium', 'small'),
meters = c(100, 50, 30))
d2 = data.frame(speed = c('high', 'slow'),
value =c(200, 45))
p1 = ggplot(d1, aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d()
p2 = ggplot(d2, aes(x = speed, y = value, fill = speed)) +
geom_col()
p_ls = list(p1, p2)
n_x = sapply(p_ls, function(p) {
max(layer_data(p, 1)$x)
})
plot_grid(plotlist = p_ls, align = 'v', ncol = 1, rel_widths = n_x)
来自?plot_grid
:
rel_widths
(optional) Numerical vector of relative columns widths. For example, in a two-column grid, rel_widths = c(2, 1) would make the first column twice as wide as the second column.
参数 rel_widths
在单列绘图网格中不执行任何操作。
您可能需要使用适当的尺寸手动调用 cowplot::draw_plot
以将绘图放在您想要的位置。
首先,我认为如果不进行一些严肃的黑客攻击,这是不可能的。我认为通过一些变通办法你会过得更好。
我的第一个答案(这里现在是第二个选项)是创建假因子水平。这当然会带来类别的完美对齐。
另一个选项(这里现在是选项 1)是使用 expand 参数。下面是一种编程方法。
我添加了一个矩形,让它看起来好像没有进一步的情节。这可以通过主题的相应背景填充来完成。
但最后,我仍然认为您可以通过刻面获得更好、更容易的结果。
一个选项
library(ggplot2)
library(cowplot)
d1 = data.frame(length = c('large', 'medium', 'small'), meters = c(100, 50, 30))
d2 = data.frame(speed = c('high', 'slow'), value =c(200, 45))
d3 = data.frame(key = c('high', 'slow', 'veryslow', 'superslow'), value = 1:4)
n_unq1 <- length(d1$length)
n_unq2 <- length(d2$speed)
n_unq3 <- length(d3$key)
n_x <- max(n_unq1, n_unq2, n_unq3)
#p1 =
expand_n <- function(n_unq){
if((n_x - n_unq)==0 ){
waiver()
} else {
expansion(add = c(0.6, (n_x-n_unq+0.56)))
}
}
p1 <-
ggplot(d1, aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq1)) +
annotate(geom = 'rect', xmin = n_unq1+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p2 <-
ggplot(d2, aes(x = speed, y = value, fill = speed)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq2)) +
annotate(geom = 'rect', xmin = n_unq2+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p3 <-
ggplot(d3, aes(x = key, y = value, fill = key)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(expand= expand_n(n_unq3)) +
annotate(geom = 'rect', xmin = n_unq3+0.5, xmax = Inf, ymin = -Inf, ymax = Inf, fill = 'white')
p_ls = list(p1, p2,p3)
plot_grid(plotlist = p_ls, align = 'v', ncol = 1)
由 reprex package (v0.3.0)
于 2020-04-24 创建选项 2,创建 n 个假因子水平直到图的最大水平,然后使用 drop = FALSE
。这是一种编程方法
library(tidyverse)
library(cowplot)
n_unq1 <- length(d1$length)
n_unq2 <- length(d2$speed)
n_unq3 <- length(d3$key)
n_x <- max(n_unq1, n_unq2, n_unq3)
make_levels <- function(x, value) {
x[[value]] <- as.character(x[[value]])
l <- length(unique(x[[value]]))
add_lev <- n_x - l
if (add_lev == 0) {
x[[value]] <- as.factor(x[[value]])
x
} else {
dummy_lev <- map_chr(1:add_lev, function(i) paste(rep(" ", i), collapse = ""))
x[[value]] <- factor(x[[value]], levels = c(unique(x[[value]]), dummy_lev))
x
}
}
list_df <- list(d1, d2, d3)
list_val <- c("length", "speed", "key")
fac_list <- purrr::pmap(.l = list(list_df, list_val), function(x, y) make_levels(x = x, value = y))
p1 <-
ggplot(fac_list[[1]], aes(x = length, y = meters, fill = length)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq1 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p2 <-
ggplot(fac_list[[2]], aes(x = speed, y = value, fill = speed)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq2 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p3 <-
ggplot(fac_list[[3]], aes(x = key, y = value, fill = key)) +
geom_col() +
scale_fill_viridis_d() +
scale_x_discrete(drop = FALSE) +
annotate(geom = "rect", xmin = n_unq3 + 0.56, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "white") +
theme(axis.ticks.x = element_blank())
p_ls <- list(p1, p2, p3)
plot_grid(plotlist = p_ls, align = "v", ncol = 1)
由 reprex package (v0.3.0)
于 2020-04-24 创建