gpt4 book ai didi

r - 在ggplot中保留不同数量的面板的面板大小

转载 作者:行者123 更新时间:2023-12-04 11:02:10 26 4
gpt4 key购买 nike

我想从不同的数据框中绘制两个图形,然后将它们组合成一个图形(最终图形更复杂)。每个图都显示了两个分类变量(比如“Sex”和“Round”)的数据子集。两个图中绘制的数据类型相同。每个数据集在这些分类变量的级别数上有所不同。

例如,这是绘制在 3 x 2 网格中的模拟数据:

require(ggplot2)
set.seed(10)

# Mock data
N <- 10
rounds <- c("A", "B", "C")
NROUNDS <- length(rounds)

df1 <- data.frame(
Age = 1:N,
Response = rnorm(N*2*NROUNDS),
Sex = rep(c("M", "F"), each = N),
Round = rep(rounds, each = N*2)
)

# Dimension parameters
panel_width <- 2.5
panel_height <- 1.5
ylims <- c(-4, 4)
units <- "in"
panel_spacing <- 0.1
plot_mar <- 0.25

total_x_margin <- panel_spacing + plot_mar*2
total_y_margin <- panel_spacing*(NROUNDS-1) + plot_mar*2

# Plot the figure
six_panel_plot <- ggplot(df1, aes(x = Age, y = Response)) +
geom_line(lwd = 2, color = "#CC79A7") +
facet_grid(rows = vars(Round), cols = vars(Sex)) +
ylim(ylims) +
theme(
panel.spacing.x = unit(panel_spacing, units),
panel.spacing.y = unit(panel_spacing, units),
plot.margin = margin(plot_mar, plot_mar, plot_mar, plot_mar, units)
) + theme_bw()

# Save the figure
ggsave("six_panel_plot.png", six_panel_plot,
width = total_x_margin + panel_width*2,
height = total_y_margin + panel_height*NROUNDS)

six_panel_plot

我试图根据每个维度的面板数量和边距大小调整图形大小。但是,如果在 4 x 2 网格上创建一个类似的图形,则各个面板的尺寸与之前的图形并不完全相同。
# Mock data
N <- 10
rounds <- c("A", "B", "C", "D")
NROUNDS <- length(rounds)

df2 <- data.frame(
Age = 1:N,
Response = rnorm(N*2*NROUNDS),
Sex = rep(c("M", "F"), each = N),
Round = rep(rounds, each = N*2)
)


# Dimension parameters
panel_width <- 2.5
panel_height <- 1.5
ylims <- c(-4, 4)
units <- "in"
panel_spacing <- 0.1
plot_mar <- 0.25

total_x_margin <- panel_spacing + plot_mar*2
total_y_margin <- panel_spacing*(NROUNDS-1) + plot_mar*2


eight_panel_plot <- ggplot(df2, aes(x = Age, y = Response)) +
geom_line(lwd = 2, color = "#CC79A7") +
facet_grid(rows = vars(Round), cols = vars(Sex)) +
ylim(ylims) +
theme(
panel.spacing.x = unit(panel_spacing, units),
panel.spacing.y = unit(panel_spacing, units),
plot.margin = margin(plot_mar, plot_mar, plot_mar, plot_mar, units)
) + theme_bw()

ggsave("eight_panel_plot.png", eight_panel_plot,
width = total_x_margin + panel_width*2,
height = total_y_margin + panel_height*NROUNDS)

eight_panel_plot

如果我在其他软件(Inkscape、Illustrator 等)中对齐这些图形,则面板的尺寸不同。

如何保持个体大小 面板 跨数字(而不是简单地强制第一个数字有四行)?我想避免使用额外的包,这可能只用 ggplot2 吗?

最佳答案

我没有仅 ggplot 的解决方案,但我将使用 ggplot 本身依赖的一些包(grid & gtable),因此不需要额外的下载。我假设所需的单位以英寸为单位定义,但很容易更改。

library(grid)

# As per your post
panel_width <- 2.5
panel_height <- 1.5

# Convert plot to gtables
plots <- list(p8 = ggplotGrob(eight_panel_plot),
p6 = ggplotGrob(six_panel_plot))

plots <- lapply(plots, function(gt) {
# Find the positions of panels
panel_x <- unique(panel_cols(gt)$l)
panel_y <- unique(panel_rows(gt)$t)
# Change the sizes of these positions
gt$widths[panel_x] <- unit(panel_width, "inch")
gt$heights[panel_y] <- unit(panel_height, "inch")
gt
})

ggsave("test1.png", plot = plots[[1]])
ggsave("test2.png", plot = plots[[2]])

测试1.png:

enter image description here

测试2.png:

enter image description here

关于r - 在ggplot中保留不同数量的面板的面板大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com