【发布时间】:2021-07-30 03:31:50
【问题描述】:
我遇到了 ggpairs 中消失的图例的问题。
我在下三角 ggpairs 图的顶部添加了一个图例,如下所示。
首先,我创建一个没有图例的 ggpairs 图,然后我从临时图表中删除我想要的图例,并将其放置在带有 putPlot 的 ggpairs 图中。在我尝试修改使图例消失的主题之前,它运行良好。
# 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)
更换主题后传奇消失:
【问题讨论】: