生成包含共享图例的ggplot时删除背景

时间:2017-07-20 00:42:31

标签: r ggplot2 legend

我正在尝试生成一个ggplot,该ggplots#generate the list of plots: set.seed(1) plot.list <- vector(mode="list",2) plot.list[[1]] <- ggplot(data.frame(group=rep(LETTERS[1:3],10),val=rnorm(30)),aes(val,color=group))+geom_density(adjust=1)+theme_minimal() set.seed(10) plot.list[[2]] <- ggplot(data.frame(group=rep(LETTERS[1:3],10),val=rnorm(30)),aes(val,color=group))+geom_density(adjust=1)+theme_minimal() 列表共享图例。

这是我的代码:

plot.list[[1]]

所以:

plot.list[[2]]

enter image description here

g <- ggplotGrob(plot.list[[1]]+theme_minimal()+theme(legend.position="right"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
plot(legend)
lheight <- sum(legend$height)
lwidth <- sum(legend$width)
gl <- lapply(plot.list,function(x) x+theme_minimal()+theme(legend.position="none"))
gl <- c(gl,nrow=1,ncol=2)

combined <- switch(position,"bottom"=arrangeGrob(do.call(arrangeGrob,gl),
                                                 legend,
                                                 ncol=1,
                                                 heights = unit.c(unit(1, "npc") - lheight, lheight)),
                   "right" = arrangeGrob(do.call(arrangeGrob,gl),
                                         legend,
                                         ncol=2,
                                         widths=unit.c(unit(1,"npc")-lwidth,lwidth)))

enter image description here

现在,制作一个分享他们传奇的情节:

plot(combined)

但是(combined)会生成一个灰色背景的情节:

enter image description here

如何让def button (msg, x, y, w, h, ic, ac, action=None ): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if (x+w > mouse[0] > x) and (y+h > mouse[1] > y): pygame.draw.rect(watercycle, CYAN, (x, y, w, h)) if (click[0] == 1 and action != None): if (action == "Start"): game_loop() elif (action == "Load"): ##Function that makes the loading of the saved file## elif (action == "Exit"): pygame.quit() else: pygame.draw.rect(watercycle, BLUE, (x, y, w, h)) smallText = pygame.font.Font("freesansbold.ttf", 20) textSurf, textRect = text_objects(msg, smallText) textRect.center = ( (x+(w/2)), (y+(h/2)) ) watercycle.blit(textSurf, textRect) 地块与单个地块具有相同的背景?

1 个答案:

答案 0 :(得分:1)

也许你最好合并数据框并使用facet_wrap()

set.seed(1)
df1<-data.frame(group=rep(LETTERS[1:3],10),val=rnorm(30),dfz=1)

set.seed(10)
df2<-data.frame(group=rep(LETTERS[1:3],10),val=rnorm(30),dfz=2)

df<-rbind(df1,df2)#combine dataframes 

ggplot(df,aes(val,color=group))+geom_density(adjust=1)+theme_minimal()+
  facet_wrap(~dfz,scales='free') #facet_wrap by distinguishing column

enter image description here