如何使用ggplot2创建黑白透明重叠直方图

如何使用ggplot2创建黑白透明重叠直方图

本文介绍了如何使用ggplot2创建黑白透明重叠直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ggplot2创建了两个透明的重叠直方图。

$ p $ test $ data.frame(condition = rep(c(每个= 500),值= rep(-1,1000))
test [1:500,] $ value = rnorm(500)
test [501:1000 ,] $ value = rnorm(500)+ 2

fig = ggplot(test,aes(x = value,fill = condition))+
#scale_fill_grey()+
geom_histogram(position =identity,alpha = .5)
fig

看起来不错,但它的颜色。我需要一个灰度或黑色/白色阴谋。



使用scale_fill_grey()会产生透明度很难读取的情节。



理想情况下,我想要一个使用纹理而不是颜色的黑色/白色阴谋,例如交叉阴影:///表示一种情况,\\\\ 对于其他情况,当条重叠时导致XXX。这是可能的吗?

解决方案

这怎么样(没有纹理仍然)?

  fig = ggplot(test,aes(x = value,fill = condition))+ 
geom_histogram(position =identity,alpha = .8)+
scale_fill_manual(values = c(grey20,grey60))+ theme_bw()
fig

I used ggplot2 to create two transparent overlapping histograms.

test = data.frame(condition = rep(c("a", "b"), each = 500), value = rep(-1, 1000))
test[1:500,]$value = rnorm(500)
test[501:1000,]$value = rnorm(500) + 2

fig = ggplot(test, aes(x = value, fill = condition)) +
      #scale_fill_grey() +
      geom_histogram(position = "identity", alpha = .5)
fig

The resulting plot looks great, but it's in color. I need a grayscale or black/white plot.

Using "scale_fill_grey()" results in a plot with transparency that is very difficult to "read".

Ideally, I would like a black/white plot that uses texture instead of color, for instance, cross hatching: "///" for one condition, "\\\" for the other condition, resulting in "XXX" when the bars overlap. Is this possible?

解决方案

How about this (no texturing still)?

fig = ggplot(test, aes(x = value, fill = condition)) +
    geom_histogram(position = "identity", alpha = .8) +
    scale_fill_manual(values=c("grey20", "grey60")) + theme_bw()
fig

这篇关于如何使用ggplot2创建黑白透明重叠直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:05