如何按年和月绘制平均密度 [英] How to plot mean density by year and month

查看:50
本文介绍了如何按年和月绘制平均密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在R中绘制以下数据.

I have the following data that I am trying to plot in R..

     date       year    month  Zone.B   Zone.D
1   2008-May    2008    May 1.5469086   0.9628121
2   2008-June   2008    June    0.5436808   1.3583104
3   2008-July   2008    July    0.5343952   1.0014050
4   2008-Aug    2008    Aug 0.8457998   1.9633247
5   2008-Sept   2008    Sept    1.0564309   1.0598237
6   2008-Nov    2008    Nov 0.8400382   0.6224550
7   2008-Dec    2008    Dec 0.5000899   0.4628020
8   2009-May    2009    May 1.6857730   0.4686881
9   2009-June   2009    June    2.1144817   1.2159128
10  2009-July   2009    July    1.3032429   0.9161256
11  2009-Aug    2009    Aug 1.7283975   1.5130496
12  2009-Sept   2009    Sept    1.1234053   1.5315700
13  2009-Nov    2009    Nov 1.1072778   1.3294973
14  2009-Dec    2009    Dec 1.4293872   1.2318001
15  2010-May    2010    May 1.2573056   2.9030824
16  2010-June   2010    June    0.8183244   1.9133592
17  2010-July   2010    July    1.1637721   1.0880351
18  2010-Aug    2010    Aug 1.2357399   1.4476880
19  2010-Sept   2010    Sept    0.8154475   1.9440145
20  2010-Nov    2010    Nov 0.8625087   1.7255681
21  2010-Dec    2010    Dec 0.7454908   1.8538506
22  2011-May    2011    May 1.0643353   1.9391681
23  2011-June   2011    June    1.6620765   2.2622461
24  2011-July   2011    July    0.8392645   1.4462998
25  2011-Aug    2011    Aug 1.0730535   2.2823350
26  2011-Sept   2011    Sept    1.1551744   1.1851883
27  2011-Nov    2011    Nov 0.6946148   1.1089916
28  2011-Dec    2011    Dec 1.1289277   0.9832297
29  2012-May    2012    May 0.7801685   1.3918411
30  2012-June   2012    June    0.7026750   1.3219030
31  2012-July   2012    July    1.3585219   1.6716370
32  2012-Aug    2012    Aug 1.2826630   1.6898635
33  2012-Sept   2012    Sept    1.8615806   1.2897994
34  2012-Nov    2012    Nov 1.7114777   1.0998009
35  2012-Dec    2012    Dec 0.7149941   0.3424369
36  2013-May    2013    May 1.3469518   5.3418421
37  2013-June   2013    June    3.2474936   3.6502369
38  2013-July   2013    July    1.2859735   0.9634012
39  2013-Aug    2013    Aug 2.2181734   2.5195328
40  2013-Sept   2013    Sept    2.2866214   1.1138549
41  2013-Nov    2013    Nov 0.6300820   0.8241262
42  2013-Dec    2013    Dec 0.8444934   0.5658561
43  2014-May    2014    May 0.5130557   0.7943081
44  2014-June   2014    June    0.2296881   1.7998841
45  2014-July   2014    July    0.7425870   1.1508025
46  2014-Aug    2014    Aug 0.6200843   1.2819195
47  2014-Sept   2014    Sept    0.3960585   1.1619590
48  2014-Nov    2014    Nov 0.3980511   0.7375606
49  2014-Dec    2014    Dec 0.3009843   0.6061867
50  2015-May    2015    May 1.2674316   5.4225521
51  2015-June   2015    June    1.0184140   3.5031324
52  2015-July   2015    July    1.2698522   1.2475438
53  2015-Aug    2015    Aug 1.0985706   1.3307636
54  2015-Sept   2015    Sept    1.1795278   1.1892627
55  2015-Nov    2015    Nov 0.6699403   1.4401562
56  2015-Dec    2015    Dec 0.9199800   1.7972394
57  2016-June   2016    June    0.5443802   2.3273397
58  2016-July   2016    July    0.6212349   0.9363671
59  2016-Aug    2016    Aug 1.2685108   1.7920707
60  2016-Sept   2016    Sept    1.6758284   1.3687859
61  2016-Nov    2016    Nov 0.8589559   1.1374661
62  2016-Dec    2016    Dec 0.8298990   0.8522818
63  2017-June   2017    June    1.2096848   1.8674565
64  2017-July   2017    July    1.7883816   1.5487620
65  2017-Aug    2017    Aug 2.2732680   1.5071044
66  2017-Sept   2017    Sept    2.3455175   1.8381368
67  2017-Nov    2017    Nov 1.7463599   1.9304698
68  2017-Dec    2017    Dec 0.8478681   0.9660615
69  2018-June   2018    June    4.2659266   1.2897004
70  2018-July   2018    July    1.8813193   1.6957090
71  2018-Aug    2018    Aug 3.6125893   1.6265312
72  2018-Sept   2018    Sept    2.5180816   0.9977127
73  2018-Nov    2018    Nov 1.3147816   1.3784422
74  2018-Dec    2018    Dec 1.4117959   1.6234253
Showing 1 to 20 of 74 entries, 5 total columns

我正在尝试按月和年绘制每个区域的均值.

I am trying to plot the means for each Zone by month and year.

我已经尝试过此代码

ggplot(df, aes(x=Month, y=Zone.B)) + 
  geom_line(aes()) +
  geom_point(aes())+
  labs(title = "Mean Density", y = "Mean Density (# fish/100m2", x = "Date")+
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

但是我无法弄清楚如何绘制年份,该代码按月绘制均值,但不包含年份.

But I can't figure out how to make it plot the year, that code plots the means by month but doesn't break out the year..

当我按照下面Dave2e的建议尝试aes(x = date,y = Zone.B)时,它会执行此操作..我该如何清理它,以便您可以实际读取日期?

When I try the aes(x=date, y=Zone.B) as Dave2e suggested below it does this.. how can I clean this up so you can actually read the dates?

我正在尝试执行类似的操作.如果我可以在同一图形上绘制两个区域,那就更好了!

I'm trying to do something kind of like this.. If I could plot both zones on the same figure that would be even better!

推荐答案

在R中使用日期时,始终最好将其转换为日期对象并维护它们为日期对象.分组和格式化总是可以在需要时进行.
ggplot也喜欢它的数据是整洁"的格式.因此,"Zone.B"和"Zone.D"应为Zone变量的值.

When working with dates in R it is always best to covert them to a date object and maintain them a date object. Grouping and formatting can always happen when needed.
Also ggplot likes its data is a "tidy" format. Thus "Zone.B" and "Zone.D" should be values of a Zone variable.

下面的代码将date列转换为date对象.然后它将数据框从较宽的格式转换为较长的格式,从而创建Zone变量.然后绘制数据.
最终图形接近您要求的图像.我将其留给以后的问题以完全按照需要修改轴.

The below code with convert the date column to a date object. Then it will convert the dataframe from a wide format to a longer format, creating the Zone variable. The data is then plotted.
The final graph is close to your requested image. I leave it to a future question to modify the axis exactly as desired.

#convert date column to dates
df$date <-  as.Date(paste(df$date, 01), "%Y-%b %d")

#convert from wide to long
library(tidyr)
dflong <- pivot_longer(df, cols = starts_with("Zone"), names_to = "Zone", values_to = "value")

library(ggplot2)
ggplot(dflong, aes(x=date, y=value, color=Zone, group=Zone)) + 
  geom_line(aes()) +
  geom_point(aes())+
  labs(title = "Mean Density", y = "Mean Density (# fish/100m2", x = "Date")+
  scale_x_date(date_breaks = "4 month", date_labels ="%b-%y") +
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.text.x = element_text(angle = 60))

开始数据:

df<-structure(list(date = structure(c(5L, 4L, 3L, 1L, 7L, 6L, 2L, 
12L, 11L, 10L, 8L, 14L, 13L, 9L, 19L, 18L, 17L, 15L, 21L, 20L, 
16L, 26L, 25L, 24L, 22L, 28L, 27L, 23L, 33L, 32L, 31L, 29L, 35L, 
34L, 30L, 40L, 39L, 38L, 36L, 42L, 41L, 37L, 47L, 46L, 45L, 43L, 
49L, 48L, 44L, 54L, 53L, 52L, 50L, 56L, 55L, 51L, 60L, 59L, 57L, 
62L, 61L, 58L, 66L, 65L, 63L, 68L, 67L, 64L, 72L, 71L, 69L, 74L, 
73L, 70L), .Label = c("2008-Aug", "2008-Dec", "2008-July", "2008-June", 
"2008-May", "2008-Nov", "2008-Sep", "2009-Aug", "2009-Dec", "2009-July", 
"2009-June", "2009-May", "2009-Nov", "2009-Sep", "2010-Aug", 
"2010-Dec", "2010-July", "2010-June", "2010-May", "2010-Nov", 
"2010-Sep", "2011-Aug", "2011-Dec", "2011-July", "2011-June", 
"2011-May", "2011-Nov", "2011-Sep", "2012-Aug", "2012-Dec", "2012-July", 
"2012-June", "2012-May", "2012-Nov", "2012-Sep", "2013-Aug", 
"2013-Dec", "2013-July", "2013-June", "2013-May", "2013-Nov", 
"2013-Sep", "2014-Aug", "2014-Dec", "2014-July", "2014-June", 
"2014-May", "2014-Nov", "2014-Sep", "2015-Aug", "2015-Dec", "2015-July", 
"2015-June", "2015-May", "2015-Nov", "2015-Sep", "2016-Aug", 
"2016-Dec", "2016-July", "2016-June", "2016-Nov", "2016-Sep", 
"2017-Aug", "2017-Dec", "2017-July", "2017-June", "2017-Nov", 
"2017-Sep", "2018-Aug", "2018-Dec", "2018-July", "2018-June", 
"2018-Nov", "2018-Sep"), class = "factor"), year = c(2008L, 2008L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L
), month = structure(c(5L, 4L, 3L, 1L, 7L, 6L, 2L, 5L, 4L, 3L, 
1L, 7L, 6L, 2L, 5L, 4L, 3L, 1L, 7L, 6L, 2L, 5L, 4L, 3L, 1L, 7L, 
6L, 2L, 5L, 4L, 3L, 1L, 7L, 6L, 2L, 5L, 4L, 3L, 1L, 7L, 6L, 2L, 
5L, 4L, 3L, 1L, 7L, 6L, 2L, 5L, 4L, 3L, 1L, 7L, 6L, 2L, 4L, 3L, 
1L, 7L, 6L, 2L, 4L, 3L, 1L, 7L, 6L, 2L, 4L, 3L, 1L, 7L, 6L, 2L
), .Label = c("Aug", "Dec", "July", "June", "May", "Nov", "Sept"
), class = "factor"), Zone.B = c(1.5469086, 0.5436808, 0.5343952, 
0.8457998, 1.0564309, 0.8400382, 0.5000899, 1.685773, 2.1144817, 
1.3032429, 1.7283975, 1.1234053, 1.1072778, 1.4293872, 1.2573056, 
0.8183244, 1.1637721, 1.2357399, 0.8154475, 0.8625087, 0.7454908, 
1.0643353, 1.6620765, 0.8392645, 1.0730535, 1.1551744, 0.6946148, 
1.1289277, 0.7801685, 0.702675, 1.3585219, 1.282663, 1.8615806, 
1.7114777, 0.7149941, 1.3469518, 3.2474936, 1.2859735, 2.2181734, 
2.2866214, 0.630082, 0.8444934, 0.5130557, 0.2296881, 0.742587, 
0.6200843, 0.3960585, 0.3980511, 0.3009843, 1.2674316, 1.018414, 
1.2698522, 1.0985706, 1.1795278, 0.6699403, 0.91998, 0.5443802, 
0.6212349, 1.2685108, 1.6758284, 0.8589559, 0.829899, 1.2096848, 
1.7883816, 2.273268, 2.3455175, 1.7463599, 0.8478681, 4.2659266, 
1.8813193, 3.6125893, 2.5180816, 1.3147816, 1.4117959), Zone.D = c(0.9628121, 
1.3583104, 1.001405, 1.9633247, 1.0598237, 0.622455, 0.462802, 
0.4686881, 1.2159128, 0.9161256, 1.5130496, 1.53157, 1.3294973, 
1.2318001, 2.9030824, 1.9133592, 1.0880351, 1.447688, 1.9440145, 
1.7255681, 1.8538506, 1.9391681, 2.2622461, 1.4462998, 2.282335, 
1.1851883, 1.1089916, 0.9832297, 1.3918411, 1.321903, 1.671637, 
1.6898635, 1.2897994, 1.0998009, 0.3424369, 5.3418421, 3.6502369, 
0.9634012, 2.5195328, 1.1138549, 0.8241262, 0.5658561, 0.7943081, 
1.7998841, 1.1508025, 1.2819195, 1.161959, 0.7375606, 0.6061867, 
5.4225521, 3.5031324, 1.2475438, 1.3307636, 1.1892627, 1.4401562, 
1.7972394, 2.3273397, 0.9363671, 1.7920707, 1.3687859, 1.1374661, 
0.8522818, 1.8674565, 1.548762, 1.5071044, 1.8381368, 1.9304698, 
0.9660615, 1.2897004, 1.695709, 1.6265312, 0.9977127, 1.3784422, 
1.6234253)), class = "data.frame", row.names = c(NA, -74L))

这篇关于如何按年和月绘制平均密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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