同一种美学的多种传说 [英] Multiple legends for the same aesthetic

查看:88
本文介绍了同一种美学的多种传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 facet_grid facet_wrap 连同 geom_raster code>。但是,在每个面板中, z 审美都是不同的。例如,

  ##问题结束时的数据
ggplot(dd,aes(x,y))+
geom_raster(aes(fill = z))+
facet_grid(type〜var)

给出



然而,由于C和D的平均值分别在0和100左右,所以我们失去了很多分辨率。您也可以尝试:

  ##将C更改为D以获得其他面板
ggplot(subset(dd,var ==C),aes(x,y))+
geom_raster(aes(fill = z))+
facet_grid(type〜var)+
theme(legend.position = 底部)

给出







但我现在有两个y条。



问题


  1. 试图为 fill 审美提供两个图例

  2. 或者,如果我做了两个单独的图,我可以删除y条其中一块地块让我把它们压在一起 - 搞乱了主题,表明这是不可能的。

数据



重现图表的数据

  dd = expand.grid(x = 1:10,y = 1:10)
dd = data.frame(dd,type = rep(LETTERS [1:2],each = 100),
var = rep(c(C,D),each = 200))
dd $ z = rnorm(400,rep(c(0,100),each = 200))


解决方案

这个怎么样:

  library(gridExtra)
p1 < - ggplot(subset(dd,var ==C),aes(x,y))+
geom_raster(aes(fill = z))+ facet_grid(键入〜 var)+
主题(legend.position =bottom,plot.margin = unit(c(1,-1,1,0.2),line))
p2 < - ggplot(子集(dd,var ==D),aes(x,y))+
geom_raster(aes(fill = z))+ facet_grid(type〜var)+
theme(legend.position =bottom,plot.margin = unit(c(1,1,1,-0.8),line),
axis.text.y = element_blank(),axis.ticks.y = element_blank ))+ ylab()
grid.arrange(arrangeGrob(p1,p2,nrow = 1))

你也可能想要玩 plot.margin 。看起来您对第一个问题的否定回答可以在这里找到


I'm trying to use facet_grid or facet_wrap in conjunction with geom_raster. However, in each panel, the z aesthetic is on a different scale. For example,

##Data at end of question
ggplot(dd, aes(x,y)) +
    geom_raster(aes(fill=z)) +
    facet_grid(type ~ var)

gives

.

However, since the average values of C and D are around 0 and 100 respectively, we lose a lot of resolution. You could also try:

##Change C to D to get other panel
ggplot(subset(dd, var=="C"), aes(x,y))+
    geom_raster(aes(fill=z)) + 
    facet_grid(type ~ var) + 
    theme(legend.position="bottom") 

which gives

and

but I now have two y strips.

Question

  1. Can I alter the first plot to give two legends for the fill aesthetic?
  2. Or, if I do two separate graphs, can I remove the y strip on one of the plots to allow me to press them together - messing about with the theme, suggests this isn't possible.

Data

Data to reproduce graphs

dd = expand.grid(x=1:10, y=1:10)
dd = data.frame(dd, type=rep(LETTERS[1:2], each=100), 
           var =rep(c("C", "D"), each=200) )
dd$z = rnorm(400, rep(c(0, 100), each=200))

解决方案

What about this:

library(gridExtra)
p1 <- ggplot(subset(dd, var=="C"), aes(x,y))+
  geom_raster(aes(fill=z)) + facet_grid(type ~ var) + 
  theme(legend.position="bottom", plot.margin = unit(c(1,-1,1,0.2), "line"))
p2 <- ggplot(subset(dd, var=="D"), aes(x,y))+
  geom_raster(aes(fill=z)) + facet_grid(type ~ var) + 
  theme(legend.position="bottom", plot.margin = unit(c(1,1,1,-0.8), "line"),
        axis.text.y = element_blank(), axis.ticks.y = element_blank()) + ylab("")
grid.arrange(arrangeGrob(p1, p2, nrow = 1))

also you might want to play around with plot.margin. And it seems that a negative answer to your first question can be found here.

这篇关于同一种美学的多种传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆