【问题标题】:Custom legend in ggpairs disappears after changing theme更改主题后,ggpairs 中的自定义图例消失
【发布时间】:2021-07-30 03:31:50
【问题描述】:

我遇到了 ggpairs 中消失的图例的问题。

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

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

# 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)

更换主题后传奇消失:

【问题讨论】:

    标签: r themes legend ggpairs


    【解决方案1】:

    我有类似的问题。我认为您需要使用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()
    

    【讨论】:

    • 谢谢 Sam_9090。这正是我想要的。我只是认为应该有一个更简单的答案,在 theme() 之前,图例出现在正确的位置。我想支持你的答案,但没有足够的声誉。
    【解决方案2】:

    在这里学习:Legend using 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)
    

    【讨论】:

    • 感谢 TarJae,我已经看到了这个解决方案,但问题是我不想要图例中的 alpha(但我想要它在图表中)并且我想要形状。这就是我使用自定义图例的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2020-04-14
    • 2019-02-06
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多