我不得不重新审视这一点,并被要求制作一个密度图,用平滑线显示频率(直方图数据)。我用过geom_freqpoly
,但结果很笨拙。
我有一个将 geom_histogram 与geom_density
相同比例的 y 轴连接的图,这是一个非常好的图,但我希望在密度曲线上具有与直方图相同的颜色美学(即按因子年份),但没有填写密度图。我尝试了几种不同的方法,但这给了我最接近的方法,尽管在geom_density
. 这是我的代码、绘图和示例数据。我试图将两个情节 ageom_histogram
与 a结合起来,geom_density
但无济于事。我得到了错误。我已经修改geom_density
,所以它给出了与 binwidth 中相同的 y 值频率geom_histogram
。
ps 所有信用都交给 SOverflow。
Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+"
lffam=ggplot(NMPSCFAM, aes(Length, fill=Year)) +
geom_histogram(position="dodge", binwidth=50, colour="black") + xlim(0, 700) +
scale_fill_grey(start = 1, end = 0) +
xlab("Length Class") +
ylab(expression(paste("Total Count"))) +
facet_wrap( ~ Family + Sector2, ncol=3, scales = "free_y") +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
strip.text = element_text (size = 15))
lffam = lffam+ # I've tried it with and without this lffam=lffam+ : I've put the next geom_density above facet_wrap.
geom_density(aes(y=50*..count..), alpha=0.2, adjust=1) +
geom_density(aes(colour = Year)) +
scale_colour_grey(start = 1, end = 0)
plot(lffam)
样本数据
Sector2 Family Year Total
BUN Acroporidae 2010 144.3595
MUR Faviidae 2010 34.983
NTH Pocilloporidae 2010 80.6952
BUN Poritidae 2010 219.616
MUR Acroporidae 2010 183.8265
NTH Faviidae 2010 221.4671429
BUN Pocilloporidae 2010 63.32033333
MUR Poritidae 2010 95.104
NTH Acroporidae 2011 21.955
BUN Faviidae 2011 77.766
MUR Pocilloporidae 2011 137.402
NTH Poritidae 2011 223.62175
BUN Acroporidae 2011 178.577
MUR Faviidae 2011 393.5435
NTH Pocilloporidae 2011 102.318
BUN Poritidae 2011 35.9815
MUR Acroporidae 2012 74.276
NTH Faviidae 2012 68.542
BUN Pocilloporidae 2012 37.5394
MUR Poritidae 2012 92.357125
NTH Acroporidae 2012 175.0937778
BUN Faviidae 2012 106.066375
MUR Pocilloporidae 2012 57.93733333
NTH Poritidae 2013 125.202
BUN Acroporidae 2013 106.092
MUR Faviidae 2013 143.02
NTH Pocilloporidae 2013 50.838
BUN Poritidae 2013 81.53
MUR Acroporidae 2013 75.169
NTH Faviidae 2012 348.796
BUN Pocilloporidae 2012 129.723
MUR Poritidae 2013 281.465
NTH Faviidae 2013 192.021
BUN Pocilloporidae 2010 192.021
MUR Poritidae 2010 56.294
NTH Acroporidae 2010 372.775
BUN Faviidae 2010 85.47
MUR Pocilloporidae 2010 67.844
NTH Poritidae 2011 129.592
BUN Acroporidae 2011 145.475
MUR Faviidae 2011 171.8246667
NTH Pocilloporidae 2011 88.825
BUN Poritidae 2012 189.823
MUR Faviidae 2012 80.465
NTH Pocilloporidae 2013 139.3605238
BUN Poritidae 2013 142.9917738
MUR Pocilloporidae 2013 146.6230238
NTH Poritidae 2010 150.2542738