需要使用嵌套的ddply函数和facet wrap来标记每个geom_vline [英] Need to label each geom_vline with the factors using a nested ddply function and facet wrap

查看:335
本文介绍了需要使用嵌套的ddply函数和facet wrap来标记每个geom_vline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cannot figure out how to label each vline with year. can someone help? Below is an example of my dataset and code. I would like to label the year on the mean length vline and/or have the same colour code as the year in the figure legend.

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")

推荐答案

要使vlines具有与条纹相同的颜色,请添加参数 color = Year (假定Year是数据框中的因子)到 aes() of geom_vline(),然后使用 scale_color_grey(),其值与fill相同。

To have the vlines the same colors as the bars then add argument color=Year (assuming that Year is factor in your data frame) to aes() of geom_vline() and then use scale_color_grey() with the same values as for fill.

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)+

这篇关于需要使用嵌套的ddply函数和facet wrap来标记每个geom_vline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆