根据物种,地点和日期的不同直方图图

时间:2018-08-20 00:59:09

标签: r ggplot2 dplyr histogram lapply

我正在尝试为以下数据绘制图表网格。有5个地点,4个物种和5个采集期。我需要每个站点,日期,物种组的质量大小频率直方图。即260个地块。我一直在玩split,lapply,cut(),aggregate()以及dplyr中的管道,但是似乎找不到任何可以干净地执行此操作的东西。

我最近得到的是:

newdata = split(data, list(data$Species,data$Site,data$Date))
histograms = lapply(newdata, function(x) {
histograms = hist(x$Mass, w=.1)
return(histograms)    
})

或划分一个物种和一个地点:

infrequens = data[which(data$Species =='infrequens' & data$Site == '1'), ]

ggplot(gather(infrequens), aes(Mass)) + 
geom_histogram(binwidth = .1) + 
facet_wrap(~Date, scales = 'free_x')

,但要保持有关箱长度的错误。任何建议将不胜感激!谢谢!

站点日期种类BodySize质量

1   0017-03-19  grandis  7.4    2.0555241       
2   0017-04-19  grandis  7.6    2.2167792       
1   0017-03-19  grandis  6.6    1.4866433       
1   0017-03-19  doddsii  8.2    2.7490281   
4   0017-04-19  doddsii  7.0    1.7562082       
3   0017-05-19  doddsii  7.8    2.3859990       
1   0017-03-19  doddsii  7.8    2.3859990       
1   0017-03-19  doddsii  7.6    2.2167792   
5   0017-03-19  doddsii  9.0    3.5782702   
5   0017-03-19  doddsii  7.2    1.9020590
1   0017-03-19  infrequens  4.2 0.4133332       
1   0017-03-19  infrequens  4.2 0.4133332       
1   0017-03-19  infrequens  4.6 0.5347965       
2   0017-04-19  infrequens  4.8 0.6033003       
2   0017-05-19  infrequens  6.2 1.2454079       
4   0017-04-19  infrequens  6.8 1.6177954       
1   0017-03-19  infrequens  4.0 0.3599915   
2   0017-02-19  infrequens  6.0 1.1349656
3   0017-04-19  cockerelli  12.0    6.4860992       
4   0017-02-19  cockerelli  11.8    6.1838799       
1   0017-03-19  cockerelli  10.8    4.8092579       
1   0017-03-19  doddsii     7.6     2.2167792
5   0017-03-19  grandis  7.4    2.0555241       
2   0017-04-19  grandis  7.6    2.2167792       
1   0017-03-19  grandis  6.6    1.4866433       
1   0017-03-19  doddsii  8.2    2.7490281   
4   0017-04-19  doddsii  7.0    1.7562082       
3   0017-05-19  doddsii  7.8    2.3859990       
5   0017-03-19  doddsii  7.8    2.3859990       
1   0017-03-19  doddsii  7.6    2.2167792   
5   0017-03-19  doddsii  9.0    3.5782702   
5   0017-03-19  doddsii  7.2    1.9020590
1   0017-03-19  infrequens  4.2 0.4133332       
1   0017-03-19  infrequens  4.2 0.4133332       
1   0017-03-19  infrequens  4.6 0.5347965       
2   0017-04-19  infrequens  4.8 0.6033003       
2   0017-05-19  infrequens  6.2 1.2454079       
1   0017-03-19  infrequens  6.8 1.6177954       
2   0017-03-19  infrequens  4.0 0.3599915   
1   0017-03-19  infrequens  6.0 1.1349656
3   0017-04-19  cockerelli  12.0    6.4860992       
4   0017-02-19  cockerelli  11.8    6.1838799       
1   0017-03-19  cockerelli  10.8    4.8092579       
1   0017-03-19  doddsii     7.6     2.2167792
1   0017-03-19  grandis  7.4    2.0555241       
2   0017-04-19  grandis  7.6    2.2167792       
1   0017-03-19  grandis  6.6    1.4866433       
1   0017-03-19  doddsii  8.2    2.7490281   
4   0017-04-19  doddsii  7.0    1.7562082       
3   0017-05-19  doddsii  7.8    2.3859990       
1   0017-03-19  doddsii  7.8    2.3859990       
3   0017-03-19  doddsii  7.6    2.2167792   
5   0017-03-19  doddsii  9.0    3.5782702   
5   0017-03-19  doddsii  7.2    1.9020590
1   0017-03-19  infrequens  4.2 0.4133332       
3   0017-04-19  infrequens  4.2 0.4133332       
1   0017-03-19  infrequens  4.6 0.5347965       
2   0017-04-19  infrequens  4.8 0.6033003       
2   0017-05-19  infrequens  6.2 1.2454079       
5   0017-02-19  infrequens  6.8 1.6177954       
5   0017-04-19  infrequens  4.0 0.3599915   
1   0017-03-19  infrequens  6.0 1.1349656
3   0017-04-19  cockerelli  12.0    6.4860992       
4   0017-02-19  cockerelli  11.8    6.1838799       
1   0017-03-19  cockerelli  10.8    4.8092579       
2   0017-03-19  doddsii     7.6     2.2167792           

1 个答案:

答案 0 :(得分:0)

这样的事情怎么办?

image2.foo.com

enter image description here


样本数据

ggplot(df, aes(Mass, fill = Species)) +
    geom_histogram(binwidth = 0.1) +
    facet_grid(Date ~ Site, scales = "free_x")