1

我无法弄清楚如何用年份标记每个 vline。有人可以帮忙吗?下面是我的数据集和代码的示例。我想在平均长度 vline 上标记年份和/或与图例中的年份具有相同的颜色代码。

Sector2 Family  Year    Length
BUN Acroporidae 2010    332.1300496
BUN Poritidae   2011    141.1467966
BUN Acroporidae 2012    127.479
BUN Acroporidae 2013    142.5940556
MUR Faviidae    2010    304.0405
MUR Faviidae    2011    423.152
MUR Pocilloporidae  2012    576.0295
MUR Poritidae   2013    123.8936667
NTH Faviidae    2010    60.494
NTH Faviidae    2011    27.427
NTH Pocilloporidae  2012    270.475
NTH Poritidae   2013    363.4635


require('ggplot2')
require('plyr')

ggplot(NMPSCFAM, aes(Length,  fill=Year)) + 
  geom_histogram(position="dodge", binwidth=50, colour="black") + xlim(0, 500) +
  scale_fill_grey(start = 1, end = 0)+ 

  geom_vline(data=ddply(NMPSCFAM, Year~Family~Sector2, numcolwise(mean)), 
   mapping=aes(xintercept=Length), linetype=2) + 
  xlab("Length Class") +
  ylab(expression(paste("Total Count"))) + #( ", m^2, ")", sep = 
  facet_wrap( ~ Family + Sector2, ncol=3, scales = "free")

geom_hist 使用 ggplot 和 facet wrap

4

1 回答 1

1

要使 vlines 的颜色与条形相同,然后将参数color=Year(假设 Year 是数据框中的因素)添加到aes()of geom_vline(),然后使用scale_color_grey()与填充相同的值。

geom_vline(data=ddply(NMPSCFAM, Year~Family~Sector2, numcolwise(mean)), 
                 mapping=aes(xintercept=Length,color=Year), linetype=2) + 
scale_color_grey(start=1,end=0)+
于 2014-08-17T15:12:02.093 回答