gpt4 book ai didi

r - 在 ggpairs 中加入独立的图例(采取 2)

转载 作者:行者123 更新时间:2023-12-04 12:46:24 27 4
gpt4 key购买 nike

tl;博士无法在 ggpairs 中获得独立的图例(描述整个情节的常用颜色)令我满意。

对不起,长度。

我正在尝试使用 GGally::ggpairs 绘制(下三角形)对图(用于绘制各种绘图矩阵的扩展包 ggplot2 )。这与 How to add an external legend to ggpairs()? 本质上是相同的问题。 ,但我对这个问题的美学答案不满意,所以我将其作为扩展发布(如果评论者建议/推荐,我将删除此问题并提供奖励)。特别是,我希望图例出现在子图框架之外,或者将它放在一个虚拟子图中但允许额外的宽度来容纳它,或者(理想情况下)将它放在一个单独的(空的)子图中。如下所示,我的两个部分解决方案都有问题。

假数据:

set.seed(101)
dd <- data.frame(x=rnorm(100),
y=rnorm(100),
z=rnorm(100),
f=sample(c("a","b"),size=100,replace=TRUE))
library(GGally)

基础绘图功能:
ggfun <- function(...) {
ggpairs(dd,mapping = ggplot2::aes(color = f),
columns=1:3,
lower=list(continuous="points"),
diag=list(continuous="blankDiag"),
upper=list(continuous="blank"),
...)
}

修剪顶部/右列的功能:
trim_gg <- function(gg) {
n <- gg$nrow
gg$nrow <- gg$ncol <- n-1
v <- 1:n^2
gg$plots <- gg$plots[v>n & v%%n!=0]
gg$xAxisLabels <- gg$xAxisLabels[-n]
gg$yAxisLabels <- gg$yAxisLabels[-1]
return(gg)
}

gg0 <- trim_gg(ggfun(legends=TRUE))

去掉左栏中的图例(如上面的链接问题):
library(ggplot2)  ## for theme()
for (i in 1:2) {
inner <- getPlot(gg0,i,1)
inner <- inner + theme(legend.position="none")
gg0 <- putPlot(gg0,inner,i,1)
}
inner <- getPlot(gg0,2,2)
inner <- inner + theme(legend.position="right")
gg0 <- putPlot(gg0,inner,2,2)

enter image description here

问题 :
  • 图例后面的空白面板实际上掩盖了一些点;我不知道为什么它不像往常一样不在面板之外,我认为这是 ggpairs正在做
  • 如果它在面板之外(在顶部或右侧),我想确保留出一些额外的空间,以便面板本身的大小相同。但是,ggmatrix/ggpairs对此看起来很不灵活。

  • 到目前为止,我能够尝试的唯一选择是关注 ggplot separate legend and plot通过提取图例并使用 gridExtra::grid.arrange() :
    g_legend <- function(a.gplot){
    tmp <- ggplot_gtable(ggplot_build(a.gplot))
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
    legend <- tmp$grobs[[leg]]
    return(legend)
    }

    library(gridExtra)
    grid.arrange(getPlot(gg0,1,1),
    g_legend(getPlot(gg0,2,2)),
    getPlot(gg0,2,1),
    getPlot(gg0,2,2)+theme(legend.position="none"),
    nrow=2)

    enter image description here

    问题 :
  • ggpairs 抑制的轴和标签回来了...

  • 我还考虑过创建一个带有仅包含图例的特殊情节的面板(即尝试使用 theme(SOMETHING=element.blank) 来隐藏情节本身,但不知道如何去做。

    作为最后的手段,我可​​以自己在适当的地方修整轴,但这实际上是在重新发明 ggpairs首先是在做...

    最佳答案

    对解决方案 1 稍作修改:首先,绘制没有图例的图矩阵(但仍然使用颜色映射)。其次,使用您的trim_gg删除对角线空格的函数。第三,对于左上角的情节,绘制其图例,但将其放置在右侧的空白处。

    data(state)
    dd <- data.frame(state.x77,
    State = state.name,
    Abbrev = state.abb,
    Region = state.region,
    Division = state.division)

    columns <- c(3, 5, 6, 7)
    colour <- "Region"

    library(GGally)
    library(ggplot2) ## for theme()

    # Base plot
    ggfun <- function(data = NULL, columns = NULL, colour = NULL, legends = FALSE) {
    ggpairs(data,
    columns = columns,
    mapping = ggplot2::aes_string(colour = colour),
    lower = list(continuous = "points"),
    diag = list(continuous = "blankDiag"),
    upper = list(continuous = "blank"),
    legends = legends)
    }

    # Remove the diagonal elements
    trim_gg <- function(gg) {
    n <- gg$nrow
    gg$nrow <- gg$ncol <- n-1
    v <- 1:n^2
    gg$plots <- gg$plots[v > n & v%%n != 0]
    gg$xAxisLabels <- gg$xAxisLabels[-n]
    gg$yAxisLabels <- gg$yAxisLabels[-1]
    return(gg)
    }

    # Get the plot
    gg0 <- trim_gg(ggfun(dd, columns, colour))

    # For plot in position (1,1), draw its legend in the empty panels to the right
    inner <- getPlot(gg0, 1, 1)

    inner <- inner +
    theme(legend.position = c(1.01, 0.5),
    legend.direction = "horizontal",
    legend.justification = "left") +
    guides(colour = guide_legend(title.position = "top"))

    gg0 <- putPlot(gg0, inner, 1, 1)
    gg0

    enter image description here

    关于r - 在 ggpairs 中加入独立的图例(采取 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127724/

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