问题描述
我正在研究一个时间序列,以检查数据异质性,并希望向一些数据分析师解释这一方面的重要方面.我有一个KDE图覆盖的密度直方图(为了清楚地看到两个图).但是原始数据是计数,我想将计数值作为标签放置在直方图栏上方.
I have a time-series that I'm examining for data heterogeneity, and wish to explain some important facets of this to some data analysts. I have a density histogram overlayed by a KDE plot (in order to see both plots obviously). However the original data are counts, and I want to place the count values as labels above the histogram bars.
这是一些代码:
$tix_hist <- ggplot(tix, aes(x=Tix_Cnt))
+ geom_histogram(aes(y = ..density..), colour="black", fill="orange", binwidth=50)
+ xlab("Bin") + ylab("Density") + geom_density(aes(y = ..density..),fill=NA, colour="blue")
+ scale_x_continuous(breaks=seq(1,1700,by=100))
tix_hist + opts(
title = "Ticket Density To-Date",
plot.title = theme_text(face="bold", size=18),
axis.title.x = theme_text(face="bold", size=16),
axis.title.y = theme_text(face="bold", size=14, angle=90),
axis.text.x = theme_text(face="bold", size=14),
axis.text.y = theme_text(face="bold", size=14)
)
我考虑过使用KDE带宽等推断计数值,等等.是否可以对ggplot频率直方图的数字输出进行数据构架并将其添加为图层".我还不了解layer()函数,但是任何想法都会有所帮助.非常感谢!
I thought about extrapolating count values using KDE bandwidth, etc, . Is it possible to data frame the numeric output of a ggplot frequency histogram and add this as a 'layer'. I'm not savvy on the layer() function yet, but any ideas would be helpful. Many thanks!
推荐答案
如果希望y轴显示bin_count
数字,同时在此直方图上添加密度曲线,
if you want the y-axis to show the bin_count
number, at the same time, adding a density curve on this histogram,
您可能首先使用geom_histogram()
并记录binwidth
值! (这非常重要!),接下来添加geom_density()
层以显示拟合曲线.
you might use geom_histogram()
first and record the binwidth
value! (this is very important!), next add a layer of geom_density()
to show the fitting curve.
如果您不知道如何选择binwidth
值,则可以进行以下计算:
if you don't know how to choose the binwidth
value, you can just calculate:
my_binwidth = (max(Tix_Cnt)-min(Tix_Cnt))/30;
(这正是geom_histogram
的默认设置.)
(this is exactly what geom_histogram
does in default.)
代码如下:
(假设您刚才计算的binwith
值为0.001)
(suppose the binwith
value you just calculated is 0.001)
tix_hist <- ggplot(tix, aes(x=Tix_Cnt)) ;
tix_hist<- tix_hist + geom_histogram(aes(y=..count..),colour="blue",fill="white",binwidth=0.001);
tix_hist<- tix_hist + geom_density(aes(y=0.001*..count..),alpha=0.2,fill="#FF6666",adjust=4);
print(tix_hist);
这篇关于R:ggplot2:使用密度叠加将计数标签添加到直方图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!