2012-10-31 64 views
11

我想使用ggplot2的stat_binhex()同时在同一个图表上绘制两个独立变量,每个变量都使用scale_colour_gradientn()自己的颜色渐变。ggplot2多个stat_binhex()在一个图像中绘制不同颜色渐变

如果我们忽视x轴单元不匹配的事实,一个可重复的例子是在同一图像中绘制以下图形,同时保持单独的填充渐变。

d <- ggplot(diamonds, aes(x=carat,y=price))+ 
    stat_binhex(colour="white",na.rm=TRUE)+ 
    scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) 
try(ggsave(plot=d,filename=<some file>,height=6,width=8)) 

enter image description here

d <- ggplot(diamonds, aes(x=depth,y=price))+ 
    stat_binhex(colour="white",na.rm=TRUE)+ 
    scale_fill_gradientn(colours=c("yellow","black"),name = "Frequency",na.value=NA) 
try(ggsave(plot=d,filename=<some other file>,height=6,width=8)) 

enter image description here

我发现在谷歌GGPLOT2组here相关问题的一些谈话。

回答

10

这里是另一个可能的解决方案:我已@ MNEL的映射斌的想法算到Alpha透明度,我已经改变了X变量,使他们能够在同一坐标绘制。

library(ggplot2) 

# Transforms range of data to 0, 1. 
rangeTransform = function(x) (x - min(x))/(max(x) - min(x)) 

dat = diamonds 
dat$norm_carat = rangeTransform(dat$carat) 
dat$norm_depth = rangeTransform(dat$depth) 

p1 = ggplot(data=dat) + 
    theme_bw() + 
    stat_binhex(aes(x=norm_carat, y=price, alpha=..count..), fill="#002BFF") + 
    stat_binhex(aes(x=norm_depth, y=price, alpha=..count..), fill="#FFD500") + 
    guides(fill=FALSE, alpha=FALSE) + 
    xlab("Range Transformed Units") 

ggsave(plot=p1, filename="plot_1.png", height=5, width=5) 

思想:

  1. 我试图(和失败的),以显示一个合理的颜色/ alpha图例。似乎很棘手,但应该可以给所有ggplot2的传奇定制功能。

  2. X轴单位标签需要某种解决方案。在一个轴上绘制两套单位被许多人所诟病,而ggplot2没有这种功能。

  3. 在这个例子中,重叠颜色的单元格的解释看起来很清楚,但取决于所使用的数据集和所选的颜色,可能会变得非常混乱。

  4. 如果两种颜色是加性补足,那么它们在任何相同重叠的地方都会看到中性灰。在重叠不等的地方,灰色将变为更黄或更蓝。我的颜色并不完美,以灰色重叠单元的略带粉红色的色调来判断。

enter image description here

+0

这是正确的方向。任何关于如何为每个stat_binhex()分配scale_fill_gradientn()的想法?此外,思考#2正式注意到 - 我的预期应用程序对x变量使用相同的单位 – metasequoia

+0

每个'ggplot'调用中只能有一个'fill'标度。我可以通过手动指定颜色为每个值范围*变量组合定义'scale_fill_manual'来描述某种黑客行为。然后,每次调用'stat_binhex'都会将'fill'映射到变量特定的因素......但现在我只是喋喋不休地说...... – bdemarest

5

我觉得你想要的东西与ggplot2的原理相比较,而更一般的图形语法的方法。直到issue被寻址(我不认为我的呼吸),你有两个选择

使用facet_wrapalpha

这是不会产生不错传说,但需要你好歹你想要什么。

可以设置alpha值由计算Frequency,访问规模由..Frequency..

我不认为你可以很好地虽然合并的传说。

library(reshape2) 
# in long format 
dm <- melt(diamonds, measure.var = c('depth','carat')) 

ggplot(dm, aes(y = price, fill = variable, x = value)) + 
    facet_wrap(~variable, ncol = 1, scales = 'free_x') + 
    stat_binhex(aes(alpha = ..count..), colour = 'grey80') + 
    scale_alpha(name = 'Frequency', range = c(0,1)) + 
    theme_bw() + 
    scale_fill_manual('Variable', values = setNames(c('darkblue','yellow4'), c('depth','carat'))) 

enter image description here

使用gridExtragrid.arrangearrangeGrob

您可以创建单独的情节和使用gridExtra::grid.arrange单个图像上安排。

d_carat <- ggplot(diamonds, aes(x=carat,y=price))+ 
    stat_binhex(colour="white",na.rm=TRUE)+ 
    scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) 

d_depth <- ggplot(diamonds, aes(x=depth,y=price))+ 
    stat_binhex(colour="white",na.rm=TRUE)+ 
    scale_fill_gradientn(colours=c("yellow","black"),name = "Frequency",na.value=NA) 

library(gridExtra) 


grid.arrange(d_carat, d_depth, ncol =1) 

enter image description here

如果你想这与ggsave工作(感谢@bdemarest下面的评论和@baptiste)

arrangeGrob像更换grid.arrange

ggsave(plot=arrangeGrob(d_carat, d_depth, ncol=1), filename="plot_2.pdf", height=12, width=8) 
+2

你可以用'ggsave()'如果你代替'arrangeGrob'为'grid.arrange'。例如:'ggsave(plot = arrangeGrob(d_carat,d_depth,ncol = 1),filename =“plot_2.pdf”,height = 12,width = 8)''。 (@baptiste在先前的评论中指出了这一点。) – bdemarest

+0

感谢@bdemarest,非常有用。 – mnel

+0

第一个答案可能不再适用于较新版本的ggplot,请参阅[这里](https://stackoverflow.com/questions/39446852/geom-hexbin-map-bincount-to-alpha/)。 – Axeman

相关问题