gpt4 book ai didi

r - 在 facet_wrap 图中添加 "floating"轴标签

转载 作者:行者123 更新时间:2023-12-01 06:40:22 25 4
gpt4 key购买 nike

我和 this 有同样的问题用户 - 我有一个“锯齿状”分面图,其中底行的面板比其他行少,我希望每列底部都有 x 轴刻度。

该问题的建议解决方案是设置 scales="free_x" . (在 ggplot 0.9.2.1 中;我相信我正在寻找的行为在早期版本中是默认的。)在我的情况下这是一个糟糕的解决方案:我的实际轴标签会很长,所以把它们放在每一行下面会占用太多房间。结果是这样的:

 x <- gl(3, 1, 15, labels=paste("this is a very long axis label ", letters[1:5]))
y <- rnorm(length(x))
l <- gl(5, 3, 15)
d <- data.frame(x=x, y=y, l=l)
ggplot(d, aes(x=x, y=y)) + geom_point() + facet_wrap(~l, scales="free_x") +
theme(axis.text.x=element_text(angle=90, hjust=1))

enter image description here

在评论中 here , Andrie 建议可以在 grid 中手动完成但我不知道如何开始。

最佳答案

如果我没记错的话,有关于如何将所有标签添加到最后一列下的同一行以及如何将这些最后一个标签提升到下一行的问题。所以这里是两种情况的函数:

编辑:因为这就像 print.ggplot 的替代品(请参阅 getAnywhere(print.ggplot) )我从中添加了一些行以保留功能。

编辑 2:我对其进行了更多改进:无需指定 nrowncol再有,所有面板的图也可以打印。

library(grid)
# pos - where to add new labels
# newpage, vp - see ?print.ggplot
facetAdjust <- function(x, pos = c("up", "down"),
newpage = is.null(vp), vp = NULL)
{
# part of print.ggplot
ggplot2:::set_last_plot(x)
if(newpage)
grid.newpage()
pos <- match.arg(pos)
p <- ggplot_build(x)
gtable <- ggplot_gtable(p)
# finding dimensions
dims <- apply(p$panel$layout[2:3], 2, max)
nrow <- dims[1]
ncol <- dims[2]
# number of panels in the plot
panels <- sum(grepl("panel", names(gtable$grobs)))
space <- ncol * nrow
# missing panels
n <- space - panels
# checking whether modifications are needed
if(panels != space){
# indices of panels to fix
idx <- (space - ncol - n + 1):(space - ncol)
# copying x-axis of the last existing panel to the chosen panels
# in the row above
gtable$grobs[paste0("axis_b",idx)] <- list(gtable$grobs[[paste0("axis_b",panels)]])
if(pos == "down"){
# if pos == down then shifting labels down to the same level as
# the x-axis of last panel
rows <- grep(paste0("axis_b\\-[", idx[1], "-", idx[n], "]"),
gtable$layout$name)
lastAxis <- grep(paste0("axis_b\\-", panels), gtable$layout$name)
gtable$layout[rows, c("t","b")] <- gtable$layout[lastAxis, c("t")]
}
}
# again part of print.ggplot, plotting adjusted version
if(is.null(vp)){
grid.draw(gtable)
}
else{
if (is.character(vp))
seekViewport(vp)
else pushViewport(vp)
grid.draw(gtable)
upViewport()
}
invisible(p)
}

这是它的外观
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) +
facet_wrap(~ color)
facetAdjust(d)

enter image description here
facetAdjust(d, "down")

enter image description here

编辑 3:

这是另一种解决方案,上面的也很好。

使用 ggsave时遇到一些问题连同 facetAdjust . ggplot类的图需要,因为 ggsave的源代码中有两部分: print(plot)default_name(plot)如果没有手动提供文件名(根据 ?ggsave 似乎它不应该工作,但)。因此,给定文件名,有一种解决方法(在某些情况下可能会产生副作用):

首先,让我们考虑实现 float 轴主要效果的单独函数。通常,它会返回 gtable对象,但是我们使用 class(gtable) <- c("facetAdjust", "gtable", "ggplot") .这样就允许使用 ggsaveprint(plot)按要求工作(见下文 print.facetAdjust)
facetAdjust <- function(x, pos = c("up", "down"))
{
pos <- match.arg(pos)
p <- ggplot_build(x)
gtable <- ggplot_gtable(p); dev.off()
dims <- apply(p$panel$layout[2:3], 2, max)
nrow <- dims[1]
ncol <- dims[2]
panels <- sum(grepl("panel", names(gtable$grobs)))
space <- ncol * nrow
n <- space - panels
if(panels != space){
idx <- (space - ncol - n + 1):(space - ncol)
gtable$grobs[paste0("axis_b",idx)] <- list(gtable$grobs[[paste0("axis_b",panels)]])
if(pos == "down"){
rows <- grep(paste0("axis_b\\-[", idx[1], "-", idx[n], "]"),
gtable$layout$name)
lastAxis <- grep(paste0("axis_b\\-", panels), gtable$layout$name)
gtable$layout[rows, c("t","b")] <- gtable$layout[lastAxis, c("t")]
}
}
class(gtable) <- c("facetAdjust", "gtable", "ggplot"); gtable
}

ggplot2:::print.ggplot仅有几行不同的打印功能:
print.facetAdjust <- function(x, newpage = is.null(vp), vp = NULL) {
if(newpage)
grid.newpage()
if(is.null(vp)){
grid.draw(x)
} else {
if (is.character(vp))
seekViewport(vp)
else pushViewport(vp)
grid.draw(x)
upViewport()
}
invisible(x)
}

例子:
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) +
facet_wrap(~ color)
p <- facetAdjust(d) # No output
print(p) # The same output as with the old version of facetAdjust()
ggsave("name.pdf", p) # Works, a filename is necessary

关于r - 在 facet_wrap 图中添加 "floating"轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11163400/

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