I want to create the next histogram density plot with ggplot2
. In the "normal" way (base packages) is really easy:
我想用ggplot2创建下一个直方图密度图。用“正常”的方式(基本包)真的很简单:
set.seed(46)vector <- rnorm(500) breaks <- quantile(vector,seq(0,1,by=0.1))labels = 1:(length(breaks)-1)den = density(vector)hist(df$vector, breaks=breaks, col=rainbow(length(breaks)), probability=TRUE)lines(den)
With ggplot I have reached this so far:
关于ggplot,我到目前为止已经达到了:
seg <- cut(vector,breaks, labels=labels, include.lowest = TRUE, right = TRUE)df = data.frame(vector=vector,seg=seg)ggplot(df) + geom_histogram(breaks=breaks, aes(x=vector, y=..density.., fill=seg)) + geom_density(aes(x=vector, y=..density..))
But the "y" scale has the wrong dimension. I have noted that the next run gets the "y" scale right.
但是“y”的尺度是错误的。我已经注意到,下一轮的“y”刻度是正确的。
ggplot(df) + geom_histogram(breaks=breaks, aes(x=vector, y=..density.., fill=seg)) + geom_density(aes(x=vector, y=..density..))
I just do not understand it. y=..density..
is there, that should be the height. So why on earth my scale gets modified when I try to fill it?
我就是不明白。y = . .密度. .这应该是高度。那么,为什么我的刻度被修改了呢?
I do need the colours. I just want a histogram where the breaks and the colours of each block are directionally set according to the default ggplot fill colours.
我确实需要颜色。我只是想要一个直方图,其中每个块的断点和颜色都是根据默认的ggplot填充颜色定向设置的。
2 个解决方案
#1
11
Manually, I added colors to your percentile bars. See if this works for you.
手工地,我给你的百分位数增加了颜色。看看这对你是否有效。
library(ggplot2)ggplot(df, aes(x=vector)) + geom_histogram(breaks=breaks,aes(y=..density..),colour="black",fill=c("red","orange","yellow","lightgreen","green","darkgreen","blue","darkblue","purple","pink")) + geom_density(aes(y=..density..)) + scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3)) + ylab("Density") + xlab("df$vector") + ggtitle("Histogram of df$vector") + theme_bw() + theme(plot.title=element_text(size=20), axis.title.y=element_text(size = 16, vjust=+0.2), axis.title.x=element_text(size = 16, vjust=-0.2), axis.text.y=element_text(size = 14), axis.text.x=element_text(size = 14), panel.grid.major = element_blank(), panel.grid.minor = element_blank())
#2
2
fill=seg
results in grouping. You are actually getting a different histogram for each value of seg
. If you don't need the colours, you could use this:
填补=赛格结果分组。每个seg值都有不同的直方图。如果你不需要颜色,你可以用这个:
ggplot(df) + geom_histogram(breaks=breaks,aes(x=vector,y=..density..), position="identity") + geom_density(aes(x=vector,y=..density..))
If you need the colours, it might be easiest to calculate the density values outside of ggplot2.
如果需要颜色,可能最容易计算ggplot2之外的密度值。
#1
11
Manually, I added colors to your percentile bars. See if this works for you.
手工地,我给你的百分位数增加了颜色。看看这对你是否有效。
library(ggplot2)ggplot(df, aes(x=vector)) + geom_histogram(breaks=breaks,aes(y=..density..),colour="black",fill=c("red","orange","yellow","lightgreen","green","darkgreen","blue","darkblue","purple","pink")) + geom_density(aes(y=..density..)) + scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3)) + ylab("Density") + xlab("df$vector") + ggtitle("Histogram of df$vector") + theme_bw() + theme(plot.title=element_text(size=20), axis.title.y=element_text(size = 16, vjust=+0.2), axis.title.x=element_text(size = 16, vjust=-0.2), axis.text.y=element_text(size = 14), axis.text.x=element_text(size = 14), panel.grid.major = element_blank(), panel.grid.minor = element_blank())
#2
2
fill=seg
results in grouping. You are actually getting a different histogram for each value of seg
. If you don't need the colours, you could use this:
填补=赛格结果分组。每个seg值都有不同的直方图。如果你不需要颜色,你可以用这个:
ggplot(df) + geom_histogram(breaks=breaks,aes(x=vector,y=..density..), position="identity") + geom_density(aes(x=vector,y=..density..))
If you need the colours, it might be easiest to calculate the density values outside of ggplot2.
如果需要颜色,可能最容易计算ggplot2之外的密度值。