有没有一种方法可以在不调整透明度的情况下在R中显示重叠的直方图?

有没有一种方法可以在不调整透明度的情况下在R中显示重叠的直方图?,r,colors,histogram,R,Colors,Histogram,目标是显示重叠的直方图,但我希望避免使用alpha调整,以便颜色保持明亮 有没有办法在不调整alpha arg的情况下实现这一点 目标是显示如下所示的颜色: hist(rnorm(mean=10, n = 1000), col='blue') hist(rnorm(mean=11, n = 1000), col='red', add=T) 但也显示此处所示的重叠区域 hist(rnorm(mean=10, n = 1000), col='blue') hist(rnorm(mean=11,

目标是显示重叠的直方图,但我希望避免使用alpha调整,以便颜色保持明亮

有没有办法在不调整alpha arg的情况下实现这一点

目标是显示如下所示的颜色:

hist(rnorm(mean=10, n = 1000), col='blue')
hist(rnorm(mean=11, n = 1000), col='red', add=T)

但也显示此处所示的重叠区域

hist(rnorm(mean=10, n = 1000), col='blue')
hist(rnorm(mean=11, n = 1000), col=rgb(1,0,0,0.5), add=T)

类似的问题并没有完全解决透明度问题:

我对密度和其他绘图软件包(如lattice、ggplot2等)的使用很在行


编辑:我希望填充绘图,并将相交区域设置为不同的颜色(例如红色和蓝色相交处的紫色)。

使用
ggplot2
几何密度的解决方案

库(ggplot2)
图书馆(tidyr)
#创建数据
种子集(1234)
df%
聚集(键、值)#使用tidyr::gather将宽格式转换为长格式
ggplot(df、aes(值、颜色=键))+
几何密度(show.legend=F)+
主题_极小值()+
比例颜色手册(数值=c(x=“红色”,y=“蓝色”))

直方图 因为alpha是没有选择的,除了使用密度,你可以把直方图叠加在一起,尽管我更喜欢密度,因为它们更容易比较

# using stacked histograms
ggplot(df, aes(value, fill = key)) +
  geom_histogram(show.legend = F) +
  theme_minimal() +
  scale_fill_manual(values = c(x = "red", y = "blue"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

我使用层的概念对此提出了一个难题。本质上,我在没有alpha的情况下放置红色,在下面添加蓝色层,然后使用alpha调整再次放置红色,以使重叠区域保持我想要的对比度(即,它保持紫色)


< P>如果你不坚持重叠,那么你可以考虑使用“DoGGE”定位选项“代码> GGPrP
# generate data, some Normal and Gamma variates with the same mean & SD
set.seed(137)
rd <- data.frame(
  n=rnorm(1000, mean=6.0, sd=4.243), 
  g=rgamma(1000, shape=2, scale=3)
)

# convert the data frame to "tall" format
tall.rd <- stack(rd)

# make the plot
ggplot(tall.rd) + geom_histogram(
  aes(values,..density..,colour=ind, fill=ind),
  bins=20,position="dodge"
)
#生成具有相同平均值和标准差的数据、一些正态变量和伽马变量
种子(137)

谢谢这些。最后,我希望图被填充,交叉区域被着色,其他颜色保持原样。我更新了这个问题以澄清这一点。有没有办法做到这一点?@Minnow我认为可以单独计算这些段,然后将它们与
geom_段
相结合,尽管这似乎有点复杂。不管怎么说,这都超出了我的能力和时间:对于匿名的投票人来说:如果能给我一个解释,我将不胜感激,这样我和其他人就可以从中学习。我怀疑问题在于,正如我在开始时所说,“如果你不坚持重叠……”,而OP要求重叠直方图。Thomas K还提供了与此要求不完全匹配的解决方案。此外,以这种方式绘制直方图本身并没有什么问题。或者如果有,请解释原因。
one <- rnorm(mean=10, n = 1000)
two <- rnorm(mean=11, n = 1000)
hist(one, col='blue', main='Bright colors, visible overlap')
hist(two, col='red', add=T)
hist(one, col='blue', add=T)
hist(two, col=rgb(1,0,0,0.5), add=T)
qplot(one, fill=I('blue'))+
  geom_histogram(aes(two), fill=I('red'))+
  geom_histogram(aes(one), fill=I('blue'))+
  geom_histogram(aes(two), fill=I('red'), alpha=I(0.5))
# generate data, some Normal and Gamma variates with the same mean & SD
set.seed(137)
rd <- data.frame(
  n=rnorm(1000, mean=6.0, sd=4.243), 
  g=rgamma(1000, shape=2, scale=3)
)

# convert the data frame to "tall" format
tall.rd <- stack(rd)

# make the plot
ggplot(tall.rd) + geom_histogram(
  aes(values,..density..,colour=ind, fill=ind),
  bins=20,position="dodge"
)