gpt4 book ai didi

Rcolorbrewer 和 ggplot2 R : colormap for geom_hex

转载 作者:行者123 更新时间:2023-12-04 02:55:00 27 4
gpt4 key购买 nike

我想在 R 中制作下面这种类型的图形,它是边缘直方图和我认为的 geom_hex 对象的组合。这最初是一个 matplotlib seaborn 图。

我无法让它与 RColorbrewer 对话。有什么想法吗?

enter image description here

到目前为止我有:

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");

最佳答案

您可以使用 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 package 创建于 2018-11-20 (v0.2.1)

关于Rcolorbrewer 和 ggplot2 R : colormap for geom_hex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com