gpt4 book ai didi

r - 具有不同规范的 ylim 的 ggplot 分面

转载 作者:行者123 更新时间:2023-12-04 11:16:45 25 4
gpt4 key购买 nike

我想在 R 中创建一个包含六个面板的 ggplot 图。前五个面应该代表条形图中数据的五个不同子集,最后一个面应该代表整个数据。我还希望在前五个方面有一个固定的 y 轴比例,但在最后一个方面有不同的比例。我知道目前无法在 ggplot 功能 ( https://github.com/hadley/ggplot2/issues/187 ) 中为每个方面指定单独的 ylim,但我想知道我是否可以使用 grid 和可能的 gtable 做类似的事情 包,目前我对它们都不是很熟悉。

以下是我的尝试。我将最后一个面替换为另一个图中的一个面。

library("ggplot2")
library("dplyr")
library("grid")

# create data
set.seed(1)
d1 <- data_frame(
value = rnorm(3 * 5, mean = 30, sd = 10),
f = rep(LETTERS[1:3], 5),
p = rep(paste("Panel", 1:5), each = 3)
)
d2 <- d1 %>%
mutate(p = "Total") %>%
rbind(d1)

# make initial figures
plot1 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p) +
coord_cartesian(ylim = c(0, 50))
plot2 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p, scales = "free_y")

# extract their grobs
g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)

# replace the final facet of plot1 with the final facet of plot2
g1[["grobs"]][[7]] <- g2[["grobs"]][[7]]
g1[["grobs"]][[19]] <- g2[["grobs"]][[19]]
g1[["grobs"]][[25]] <- g2[["grobs"]][[25]]

# draw the figure
grid.newpage()
grid.draw(g1)

这就是我得到的。 enter image description here

但是,可以看出,最后一个面的 y 轴标签与前面的面重叠。有谁知道避免重叠的方法,例如,通过缩小最后一个面?

最佳答案

一种方法是从“g2”中提取“Total”图,然后将其插入“g1”,但首先从“g1”中删除“Total”图。但是您会注意到,x 轴刻度标记标签并未跨小平面对齐。

# Load packages
library(ggplot2)
library(dplyr)
library(gtable)
library(grid)

# create data
set.seed(1)
d1 <- data.frame(
value = rnorm(3 * 5, mean = 30, sd = 10),
f = rep(LETTERS[1:3], 5),
p = rep(paste("Panel", 1:5), each = 3)
)
d2 <- d1 %>%
mutate(p = "Total") %>%
rbind(d1)

# make initial figures
plot1 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p) +
coord_cartesian(ylim = c(0, 50))
plot2 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p, scales = "free_y")

# Get the ggplot grobs
g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)

# Extract "Total" plot from g2
keep = g2$layout$name %in% c("panel-3-2", "axis-b-3-2", "axis-l-2-3", "strip-t-3-2")
pos = subset(g2$layout, keep, c(t,l,b,r))
g2 = g2[c(min(pos$t):max(pos$b)), c(min(pos$l):max(pos$r))]

# Remove "Total" plot from g1
keep = !g1$layout$name %in% c("panel-3-2", "axis-b-3-2", "strip-t-3-2")
pos = subset(g1$layout, !keep, c(t,l,b,r))
g1$grobs <- g1$grobs[keep]
g1$layout <- g1$layout[keep, ]

# Insert g2 into g1
g1 = gtable_add_grob(g1, g2, t=min(pos$t), b=max(pos$b), l=min(pos$l), r=max(pos$r))

# Draw it
grid.newpage()
grid.draw(g1)

enter image description here

另一种方法是像以前一样从“g2”中提取“总计”图,但将其 y 轴移动到图的右侧(使用从 here 借用的代码。(我调整了你的“plot2”以便刻度标记标签在最终图中更好地对齐。)这样,“总计”面板占用与其他面板一样多的空间,因此 x 轴刻度标记标签对齐,但 y 轴用于“总计”面板突出显示在右侧。

# Make initial figures
plot1 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p) +
coord_cartesian(ylim = c(0, 50))
plot2 <- ggplot(d2, aes(f, value)) +
geom_bar(stat = "identity") +
facet_wrap(~ p, scales = "free_y") +
theme(axis.text.y = element_text(hjust = 0)) ## For better formatting of labels

# extract their grobs
g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)

# Extract "Total" plot from g2
keep = g2$layout$name %in% c("panel-3-2", "axis-b-3-2", "axis-l-2-3", "strip-t-3-2")
pos = subset(g2$layout, keep, c(t,l,b,r))
g2 = g2[c(min(pos$t):max(pos$b)), c(min(pos$l):max(pos$r))]


# Get the position of the panel in the layout
panel <- c(subset(g2$layout, grepl("panel", g2$layout$name), se = t:r))

# Get the row number of the y-axis in the layout
rn <- which(grepl("axis-l", g2$layout$name))

# Extract the axis (tick marks and axis text from the gtable)
axis.grob <- g2$grobs[[rn]]
axisl <- axis.grob$children[[2]] # Two children - get the second
axisl # Note: two grobs - tick marks and text

# Reverse the grobs and the widths
axisl$widths <- rev(axisl$widths)
axisl$grobs <- rev(axisl$grobs)

axisl$grobs[[1]]$x <- axisl$grobs[[1]]$x - unit(1, "npc") + unit(2.75, "pt")

axisl$grobs[[2]]$children[[1]]$x = unit(.15, "npc")

# Remove the column containing the left axis
g2 <- g2[, -(panel$r-1)]

## remove empty panels
keep = !g1$layout$name %in% c("panel-3-2", "axis-b-3-2", "strip-t-3-2")
pos = subset(g1$layout, !keep, c(t,l,b,r))
g1$grobs <- g1$grobs[keep]
g1$layout <- g1$layout[keep, ]

# Insert g2 into g1
g1 = gtable_add_grob(g1, g2, t = min(pos$t), b = max(pos$b), l = min(pos$l), r = max(pos$r))

# Add a new column to g1, and add the revised axisl grob to the new column.
pos = subset(g1$layout, grepl("panel", g1$layout$name), c(t,l,b,r)) # position of bottom right panel
g1 <- gtable_add_cols(g1, axisl$widths, max(pos$r))
g1 <- gtable_add_grob(g1, axisl, t = max(pos$b), l = max(pos$r)+1, r = max(pos$r)+2)

# Draw it
grid.newpage()
grid.draw(g1)

enter image description here

关于r - 具有不同规范的 ylim 的 ggplot 分面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966994/

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