2

我遇到了一个正在消失的传说的问题ggpairs

我在下三角图的顶部添加了一个图例ggpairs,如下所示。

首先,我创建一个ggpairs没有图例的图,然后我从临时图表中删除我想要的图例,并将ggpairs它放在图中putPlot。在我尝试修改使图例消失的主题之前,它运行良好。

# 1 produce graph without legend
library(GGally)
library(ggplot2)

plotwithoutlegend <-ggpairs(
    iris,
    columns=1:4,
    switch="both",
    upper="blank",
    mapping=aes(color = Species,
                shape= Species,
                fill=Species, 
                alpha=0.5)
)

#2 grab the legend from a graph with the legend I want (without alpha).

auxplot <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width, 
                      color=Species, 
                      shape=Species,
                      fill=Species)) + geom_point()
mylegend <- grab_legend(auxplot)

# 3 place the legend in the ggpairs grid with putPlot

graph1 <- putPlot(plotwithoutlegend,mylegend,3,4)
show(graph1)

这会在所需位置生成带有图例的图表。

ggpairs更改主题前带有图例的图表:

在此处输入图像描述

但是,如果我改变主题的某些方面,传说就会消失。

graph2 <- graph1 +theme(strip.background =element_blank(), strip.placement = "outside")
show(graph2)

更换主题后传奇消失:

在此处输入图像描述

4

2 回答 2

2

我有类似的问题。我认为你需要使用library(grid). 看我的解决方案。

# plotwithoutlegend
plotwithoutlegend <- ggpairs(
  iris,
  columns=1:4,
  switch="both",
  upper="blank",
  mapping=aes(color = Species,
              shape= Species,
              fill=Species, 
              alpha=0.5)
)+
  theme(strip.background =element_blank(), strip.placement = "outside")


#2 grab the legend from a graph with the legend I want (without alpha).

auxplot <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width, 
                            color=Species, 
                            shape=Species,
                            fill=Species)) + geom_point()


mylegend <- grab_legend(auxplot)


##### plot legend with plot
grid.newpage()
grid.draw(plotwithoutlegend)
vp = viewport(x=.9, y=.75, width=.35, height=.3) ## control legend position
pushViewport(vp)
grid.draw(mylegend)
upViewport()
于 2021-05-07T21:14:15.117 回答
0

在这里学习:使用 ggpairs 的图例

# first run your code until
graph2 <- graph1 +theme(strip.background =element_blank(), strip.placement = "outside")

# then run this code

colidx <- c(3,5,6,7)
for (i in 1:length(colidx)) {
  
  # Address only the diagonal elements
  # Get plot out of plot-matrix
  inner <- getPlot(graph2, i, i);
  
  # Add ggplot2 settings (here we remove gridlines)
  inner <- inner + theme(panel.grid = element_blank()) +
    theme(axis.text.x = element_blank())
  
  # Put it back into the plot-matrix
  graph2 <- putPlot(graph2, inner, i, i)
  
  for (j in 1:length(colidx)){
    if((i==1 & j==1)){
      
      # Move the upper-left legend to the far right of the plot
      inner <- getPlot(graph2, i, j)
      inner <- inner + theme(legend.position=c(length(colidx)-0.25,0.50)) 
      graph2 <- putPlot(graph2, inner, i, j)
    }
    else{
      
      # Delete the other legends
      inner <- getPlot(graph2, i, j)
      inner <- inner + theme(legend.position="none")
      graph2 <- putPlot(graph2, inner, i, j)
    }
  }
}

# then run this code
show(graph2)

在此处输入图像描述

于 2021-05-07T21:13:03.703 回答