r - 使用arrangeGrob添加子图作为图例

标签 r ggplot2 gridextra grob

我正在尝试将两个图绘制在一起,其中一个是主图,另一个是我希望位于主图图例区域中的子图。

此代码生成此图:

gLegend <- 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)
} 

set.seed(1)
main.df <- data.frame(group=rep(LETTERS[1:4],3),
                     y=rnorm(12),x=c(rep(1,4),rep(2,4),rep(3,4)),col=rep(c("gray","blue","red","magenta"),3))
main.df$group <- factor(main.df$group,levels=LETTERS[1:4])
sub.df <- data.frame(group=c("B","C","D"),x=1:3,effect=runif(3,0,1),col=c("blue","red","magenta"))
sub.df$group <- factor(sub.df$group,levels=LETTERS[2:4])

main.plot <- ggplot(main.df,aes(x=x,y=y,color=factor(group)))+geom_point(size=3)+facet_wrap(~group,ncol=4)+scale_fill_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+scale_colour_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+scale_x_continuous(breaks=unique(main.df$x))
sub.plot <- ggplot(sub.df,aes(x=x,y=effect,color=factor(group)))+geom_point(size=2)+scale_fill_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+scale_colour_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+labs(x="group",y="effect")+ggtitle("effect summary")+scale_x_continuous(breaks=unique(sub.df$x),labels=c("B","C","D"))

sub.plot.grob <- ggplotGrob(sub.plot)
combined.plot <- arrangeGrob(main.plot+theme(legend.position="none"),widths=c(0.75,0.25),arrangeGrob(20,sub.plot.grob),ncol=2)

enter image description here

我想做的是去掉主线剧情的传说。

如果我将 guide=FALSE 添加到 scale_fill_manual scale_colour_manual gLegend 会引发错误:

Error in tmp$grobs[[leg]] : 
  attempt to select less than one element in get1index

显然是因为它无法精细“guide-box”

知道如何解决这个问题吗?

最佳答案

enter image description here这是使用cowplot

的替代解决方案
library(ggplot2)
library(cowplot)
theme_set(theme_bw()) #cowplot automatically sets theme_classic

set.seed(1)
main.df <- data.frame(group=rep(LETTERS[1:4],3),
                      y=rnorm(12),x=c(rep(1,4),rep(2,4),rep(3,4)),col=rep(c("gray","blue","red","magenta"),3))
main.df$group <- factor(main.df$group,levels=LETTERS[1:4])
sub.df <-     data.frame(group=c("B","C","D"),x=1:3,effect=runif(3,0,1),col=c("blue","red","magenta"))
sub.df$group <- factor(sub.df$group,levels=LETTERS[2:4])

sub.plot <- ggplot(sub.df,aes(x=x,y=effect,color=factor(group)))+
  geom_point(size=2)+
  scale_fill_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+
  scale_colour_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+
  labs(x="group",y="effect")+
  ggtitle("effect summary")+
  scale_x_continuous(breaks=unique(sub.df$x),labels=c("B","C","D"))


main.plot <- ggplot(main.df,aes(x=x,y=y,color=factor(group)))+
  geom_point(size=3)+facet_wrap(~group,ncol=4)+
  scale_fill_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+
  scale_colour_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+
  scale_x_continuous(breaks=unique(main.df$x)) +
  theme(legend.position = "none")

ggdraw() +
  draw_plot(main.plot, x = 0, y = 0, width = 0.75, height = 1) +
  draw_plot(sub.plot, 0.75, 0, .25, .5)

关于r - 使用arrangeGrob添加子图作为图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38559645/

相关文章:

r - 更改 ggplot2 中 strip 文本背景的高度无法按预期工作

r - 向 R 中的刻面图添加刻度线

r - ggplot2:具有单个图例的单行中有多个图

r - 如何在 R 中为打印函数编写这个特定方法?

r - 如何使矩阵的元素可调用函数

r - 连续时间数据的分位数

r - 具有独立轴 R 的背靠背条形图

r - 在 ggplot 中使用四个图在图表下方添加标题

r - R中多个变量的高值和低值并排显示geom_boxplot?

r - Y-break 与 R 的比例变化