Rcolorbrewer和ggplot2 R:用于geom_hex的颜色地图

3
我希望在R中制作以下类型的图形,它是边际直方图和geom_hex对象的组合。这最初是一个matplotlib seaborn图形。
我无法让它与RColorbrewer通信。有任何想法吗?
目前为止,我已经得到了:
require(ggplot2)
require(RColorBrewer)
require(ggExtra)
bl<-data.frame(beta=rnorm(100),lambda=rnorm(100))

p<-ggplot(bl,aes(x=beta,y=lambda))+
  stat_bin_hex()+
  #scale_fill_gradient(palette = "Greens") Neither of these work
  #scale_fill_continuous(palette = "Greens")+
  scale_fill_brewer()+
  theme_classic()


  ggExtra::ggMarginal(p, type = "histogram")

原始代码:

x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):

https://seaborn.pydata.org/tutorial/distributions.html

sns.jointplot(x=x, y=y, kind="hex", color="greens");

1
你需要将变量分配给填充。这可以在aes调用内部完成。ggplot2文档的使用部分提供了一个示例。 - camille
1个回答

6
您可以使用scale_fill_gradientn并传入使用brewer.pal的调色板。 然后,您只需要将正确的填充和颜色传递给ggMarginal.
library(ggplot2)
library(RColorBrewer)
library(ggExtra)

bl <- data.frame(beta=rnorm(10000),lambda=rnorm(10000))

p <- ggplot(bl, aes(x=beta, y = lambda))+
  stat_bin_hex() +
  scale_fill_gradientn(colors = brewer.pal(3,"Greens")) +
  theme_classic() +
  theme(legend.position = "bottom")

ggMarginal(p, type = "histogram", fill = brewer.pal(3,"Greens")[1], color = "white")

本示例由reprex包(v0.2.1)于2018年11月20日创建。


太好了!在RColorBrewer的帮助下,可以在state_binhex内完成。像这样:stat_binhex(aes(alpha=..count..), fill="forestgreen") - atsyplenkov
你可以通过修改传递给 scale_color_gradientn 的值使其变暗。 - Jake Kaupp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接