Join two ggplot2 histograms and density plots

I had to revisit this and was asked to create a density plot showing the frequency (histogram data) with a smoothed line. I used geom_freqpoly

, but it turned out to be awkward.

I have a graph that connects geom_histogram with geom_density

on the same y-axis and is a very nice plot, but I would like to have the same color aesthetics (i.e. factor year) on the density curves as they are on the histogram, but without filling the density graph. I've tried a few different things, but this gives me the closest, albeit losing the 0 to 1 color scale on geom_density

. Here is my code, graph and sample data. I tried to combine two graphs a geom_histogram

with geom_density

but to no avail. I get an error. I changed geom_density

so it gives the same frequency of y-value as bin width in geom_histogram

.

ps All credits go to 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

Sample data

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

      

+3


source to share





All Articles