更改主题后,ggpairs中的自定义图例将消失

更改主题后,ggpairs中的自定义图例将消失,r,themes,legend,ggpairs,R,Themes,Legend,Ggpairs,我对ggpairs中消失的图例有问题 我在下三角ggpairs图的顶部添加了一个图例,如下所示 首先,我创建了一个不带图例的ggpairs绘图,然后我从中提取我想要的图例和特殊图形,并将其放置在ggpairs中,用putPlot绘图。它工作得很好,直到我试图修改主题,使传奇消失 # 1 produce graph without legend library(GGally) library(ggplot2) plotwithoutlegend <-ggpairs( iris,

我对
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)
更改主题后,图例消失:

在此学习:

#首先运行代码,直到

图2我也有类似的问题。我认为您需要使用
库(网格)
。看看我的解决方案

# 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()
#不带图例的绘图

没有图例的绘图谢谢TarJae,我已经看到了这个解决方案,但问题是我不想在图例中使用alpha(但我想在图形中使用alpha),我想要形状。这就是我使用自定义图例的原因。谢谢Sam_9090。这正是我想要的。我只是觉得应该有一个更简单的答案,在主题()之前,传说出现在正确的地方。我想推荐你的答案,但名声不好。
# 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)
# 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()