ggplot2多个stat_binhex()在一个图像中绘制不同的颜色渐变

时间:2012-10-31 21:14:41

标签: r ggplot2 data-visualization hexagonal-tiles

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

如果我们忽略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 google groups here中找到了一个相关问题的对话。

2 个答案:

答案 0 :(得分:10)

这是另一个可能的解决方案:我已经采用了@ mnel关于将bin计数映射到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. 如果这两种颜色是附加补充,那么只要它们重叠相同,您就会看到中性灰色。在重叠不相等的情况下,灰色将变为更黄或更蓝。从灰色重叠细胞的略带粉红色调来判断,我的颜色并不完全相互补充。

  5. enter image description here

答案 1 :(得分: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)

grid.arrange替换为arrangeGrob之类的内容。

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