加入两个ggplot2直方图和密度图

时间:2014-08-26 16:05:34

标签: r ggplot2 histogram data-visualization density-plot

我不得不重新审视这个问题并被要求生成一个密度图,显示频率(直方图数据)和平滑线。我已经使用了geom_freqpoly,但这一切都很笨拙。

我有一个在相同的缩放y轴上将geom_histogram与geom_density连接起来的情节,这是一个非常好的情节,但我希望在密度曲线上具有相同的颜色美学(即因子年)在直方图上,但没有填充密度图。我已经尝试了几种不同的东西,但这给了我最接近但尽管在geom_density上丢失了0到1的色标。这是我的代码,绘图和示例数据。我试图将geom_histogramgeom_density两个图组合在一起,但无济于事。我收到了错误。我修改了geom_density,因此它提供了与geom_histogram中的binwidth相同的y值频率。

P.S。所有的信息都归于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)

enter image description here

样本数据

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

0 个答案:

没有答案