在R中使用ggplot2覆盖直方图

时间:2021-02-01 14:56:52

I am new to R and am trying to plot 3 histograms onto the same graph. Everything worked fine, but my problem is that you don't see where 2 histograms overlap - they look rather cut off: Histogram

我是R的新手,我想在同一个图上画3个直方图。一切都很正常,但我的问题是,你看不到2个直方图重叠的地方——它们看起来很像被切断了:直方图。

When I make density plots, it looks perfect: each curve is surrounded by a black frame line, and colours look different where curves overlap: Density Plot

当我画密度图时,它看起来很完美:每条曲线都被一条黑色的框架线包围着,在曲线重叠的地方,颜色看起来也不一样:密度图

Can someone tell me if something similar can be achieved with the histograms in the 1st picture? This is the code I'm using:

有没有人能告诉我,在第一幅图中,直方图是否可以达到类似的效果?这是我使用的代码:

lowf0 <-read.csv (....)
mediumf0 <-read.csv (....)
highf0 <-read.csv(....)
lowf0$utt<-'low f0'
mediumf0$utt<-'medium f0'
highf0$utt<-'high f0'
histogram<-rbind(lowf0,mediumf0,highf0)
ggplot(histogram, aes(f0, fill = utt)) + geom_histogram(alpha = 0.2)

Thanks in advance for any useful tips!

谢谢你的建议!

2 个解决方案

#1


95  

Your current code:

你当前的代码:

ggplot(histogram, aes(f0, fill = utt)) + geom_histogram(alpha = 0.2)

is telling ggplot to construct one histogram using all the values in f0 and then color the bars of this single histogram according to the variable utt.

告诉ggplot使用f0中的所有值构建一个直方图,然后根据变量utt给这个单直方图的条形着色。

What you want instead is to create three separate histograms, with alpha blending so that they are visible through each other. So you probably want to use three separate calls to geom_histogram, where each one gets it's own data frame and fill:

相反,您需要创建三个单独的直方图,使用alpha混合,使它们彼此可见。因此,您可能需要对geom_histogram使用三个单独的调用,每个调用都获得自己的数据帧并填充:

ggplot(histogram, aes(f0)) + 
    geom_histogram(data = lowf0, fill = "red", alpha = 0.2) + 
    geom_histogram(data = mediumf0, fill = "blue", alpha = 0.2) +
    geom_histogram(data = highf0, fill = "green", alpha = 0.2) +

Here's a concrete example with some output:

这里有一个输出的具体例子:

dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))

ggplot(dat,aes(x=xx)) + 
    geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'b'),fill = "blue", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'c'),fill = "green", alpha = 0.2)

which produces something like this:

产生了这样的东西:

在R中使用ggplot2覆盖直方图

Edited to fix typos; you wanted fill, not colour.

编辑修正拼写错误;你想要填充,而不是颜色。

#2


169  

Using @joran's sample data,

使用@joran的样本数据,

ggplot(dat, aes(x=xx, fill=yy)) + geom_histogram(alpha=0.2, position="identity")

note that the default position of geom_histogram is "stack."

注意,geom_histogram的默认位置是“stack”。

see "position adjustment" of this page:

参见本页“职位调整”:

docs.ggplot2.org/current/geom_histogram.html

docs.ggplot2.org/current/geom_histogram.html

#1


95  

Your current code:

你当前的代码:

ggplot(histogram, aes(f0, fill = utt)) + geom_histogram(alpha = 0.2)

is telling ggplot to construct one histogram using all the values in f0 and then color the bars of this single histogram according to the variable utt.

告诉ggplot使用f0中的所有值构建一个直方图,然后根据变量utt给这个单直方图的条形着色。

What you want instead is to create three separate histograms, with alpha blending so that they are visible through each other. So you probably want to use three separate calls to geom_histogram, where each one gets it's own data frame and fill:

相反,您需要创建三个单独的直方图,使用alpha混合,使它们彼此可见。因此,您可能需要对geom_histogram使用三个单独的调用,每个调用都获得自己的数据帧并填充:

ggplot(histogram, aes(f0)) + 
    geom_histogram(data = lowf0, fill = "red", alpha = 0.2) + 
    geom_histogram(data = mediumf0, fill = "blue", alpha = 0.2) +
    geom_histogram(data = highf0, fill = "green", alpha = 0.2) +

Here's a concrete example with some output:

这里有一个输出的具体例子:

dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))

ggplot(dat,aes(x=xx)) + 
    geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'b'),fill = "blue", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'c'),fill = "green", alpha = 0.2)

which produces something like this:

产生了这样的东西:

在R中使用ggplot2覆盖直方图

Edited to fix typos; you wanted fill, not colour.

编辑修正拼写错误;你想要填充,而不是颜色。

#2


169  

Using @joran's sample data,

使用@joran的样本数据,

ggplot(dat, aes(x=xx, fill=yy)) + geom_histogram(alpha=0.2, position="identity")

note that the default position of geom_histogram is "stack."

注意,geom_histogram的默认位置是“stack”。

see "position adjustment" of this page:

参见本页“职位调整”:

docs.ggplot2.org/current/geom_histogram.html

docs.ggplot2.org/current/geom_histogram.html