如何将X轴上的日期格式化为R中的月份和年份 [英] How to format date on x-axis to month and year in R

查看:41
本文介绍了如何将X轴上的日期格式化为R中的月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到有关格式化日期的几个问题,但找不到一个解决我在这里遇到的问题的问题.

我的数据范围从2008年至2015年的5月至11月,然后从5月降低至2016年至2018年的6月至11月.我的目标是创建一个图以显示每年的采样月份,并消除未采样的月份.

我在这里问了类似的问题:

我正在尝试通过将日期在x轴上显示的方式(类似于所有采样月份的垂直方向和相应的年份的水平方向下方)进行类似的操作.

这是否有可能是由于我的未抽样月份造成的缺口?有没有更好的方法来显示这样的数据?

我正在使用这些数据

  structure(list(date = structure(c(14012,14021,14050,14056,14076、14083、14110、14118、14124、14139、14141、14168、14174,14201、14208、14371、14372、14398、14405、14439、14442、14468,14469、14498、14512、14525、14547、14562、14565、14733、14736,14764、14770、14811、14813、14826、14848、14854、14862、14895,14896、14905、14924、14931、15105、15106、15135、15147、15161,15168、15189、15191、15231、15233、15261、15272、15300、15301,15469、15476、15504、15506、15527、15539、15567、15576、15604,15605、15631、15637、15665、15671、15835、15841、15868、15869,15896、15902、15924、15926、15961、15966、15988、15995、16017,16028、16211、16213、16238、16247、16267、16270、16274、16302,16304、16332、16333、16366、16367、16387、16395、16563、16564,16592、16596、16637、16638、16666、16668、16693、16696、16723,16731、16756、16759、16961、16962、16991、16996、17017、17025,17051、17060、17093、17094、17109、17116、17334、17345、17366,17367、17396、17402、17430、17437、17451、17457、17479、17480,17695、17696、17725、17726、17758、17760、17786、17788、17821,17845,17848),class ="Date"),mean_cpue = c(1.34147348124682,1.86964964075215、1.81794067339267、0.665667643204071、0.58288080140313,1.21618562008796、0.915544650452313、2.08485242966155、1.03716945493794,1.32653950869479、1.30387017192198、0.818696953343472、1.80344708020158,0.462802005064552、0.500089944234575、0.468688145292134、3.77762506069873,2.06756426400247、2.60938827682934、0.936798217363299、1.50723642250895,1.72839753752143、1.58423249977652、1.85978443365114、1.22923787251904,1.86003452037686、1.10727777349472、1.4293872174252、1.50010309934371,3.06022031046633、1.49412683285299、2.14503371546005、1.06420000907848,1.1748728446591,1.10022108873144,1.44768795978073,1.25036934931668,2.13048237961316、1.06834543832824、1.8725166760057、0.758904479222882,0.961385073330007,1.8663428674222,0.745490766022096,1.15427953668245,1.98858189404051、2.27561614501712、1.71143613797583、1.49915849827066,0.841476235553629、2.31692888870439、1.35038151346165、1.18518826020175,1.22169567368232,1.10179032565335,0.694614793373563,0.983229683310633,1.12892772438535、1.53364097932751、0.787500879889251、1.33114386888348,0.75361846092619、1.71920865710658、1.35852187828517,1.689863533171,1.28266297991069、1.86158062664132、1.5293414824795、1.0998008820738,1.94141263432714、0.342436942826791、0.714994071540338、5.62005947683777,1.62440270638761、3.82262996941896、3.25889497807134、1.54143260146352,1.86769619502575、2.8059505280639、3.58304933614583、1.20470762672598,2.29674008364814、0.689037082722997、0.959492687511224、0.567406828456012,0.844493420881774、0.513055729898873、0.986197068455855、1.88654317723012,0.229688101851428、1.25022486058644、1.1701621335822、0.742587041289875,0.625081199656213,1.29798096608674,0.396058527329973,1.16988984259697,0.739772339659098、0.39805108067041、0.606186664268154、0.300984338326838,1.27916344040608、5.7551524259318、3.58466880968582、1.02559789553853,1.26985222650329、1.54851107697843、1.10318926209412、1.3337665961789,1.21571684379214、1.18341136267535、1.44015621029248、0.669940304071688,1.80318080838661、0.919979963710556、0.555260577151843、2.32733965810763,1.08046383335355、0.621234946065283、1.79207073432654、1.3398375035128,1.67582836446281、1.3687859338262、1.64687245759481、0.880872177773362,0.972535713988714、0.83891596232892、2.09933443707235、1.24879096699177,1.99677155577555,1.67638127852795,1.50710438838265,2.27326799319906,1.83813683299605、2.34551745405533、1.99387974527393、1.74635992056851,0.966061489637619、1.1709324985094、2.77527932681737、4.63212808059003,2.50472488366812、2.19392784029982、3.64096784095526、1.91684726897895,1.6466312330028、2.84951794706046、1.6841559743533、1.62105139974506,1.46007933985039)),类= c("tbl_df","tbl","data.frame"),row.names = c(NA,-150L),. internal.selfref =<指针:0x000001d8f6d11ef0> ;,已排序="NODCCODE") 

在此先感谢您的帮助!

解决方案

在下面,我在美学中添加了 group = year(date),以便该线仅连接同一点中的那些点.年.

scale_x_date(date_labels =%b-%Y")要有每月的滴答声-不确定是否仍要这样做.

 库(ggplot2)图书馆(dplyr)库(润滑)ggplot(MeanCPUE,aes(x = date,y = mean_cpue,group = year(date)))+geom_line()+geom_point()+实验室(标题=平均密度",y =平均密度(#鱼/100m2)",x =日期")+theme_bw()+主题(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 = 90))+scale_x_date(date_breaks ='1 year',date_labels =%b-%Y") 

I've seen several questions posted about formatting the date and haven't been able to find one to resolve the issue I'm having here.

My data ranges from May-November from 2008-2015 and then drops May and goes from June-November from 2016-2018. My goal is to create a plot to show the months sampled for each year and eliminate the months that were not sampled.

I asked a similar question here: How to plot mean density by year and month But this answer does not resolve the date issue.

I've tried this code

MeanCPUE <- BD %>%
  group_by(date) %>%
  summarise(mean_cpue = mean (CPUE)) %>%
  ungroup()
MeanCPUE

p <- ggplot(MeanCPUE, aes(x=date, y=mean_cpue))+
  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"),
                     axis.text.x = element_text(angle = 90))

Which gives me this plot.. This is close, but it doesn't label the months and it's connected the sampled months with a line through the un-sampled months.

I'm trying to make something like this with the way the dates are displayed on the x-axis with all the sampled months vertical and the corresponding year horizontal below.

Is this possible with the gaps caused by un-sampled months that I have? Is there a better way to display data like this?

I'm using this data

structure(list(date = structure(c(14012, 14021, 14050, 14056, 
14076, 14083, 14110, 14118, 14124, 14139, 14141, 14168, 14174, 
14201, 14208, 14371, 14372, 14398, 14405, 14439, 14442, 14468, 
14469, 14498, 14512, 14525, 14547, 14562, 14565, 14733, 14736, 
14764, 14770, 14811, 14813, 14826, 14848, 14854, 14862, 14895, 
14896, 14905, 14924, 14931, 15105, 15106, 15135, 15147, 15161, 
15168, 15189, 15191, 15231, 15233, 15261, 15272, 15300, 15301, 
15469, 15476, 15504, 15506, 15527, 15539, 15567, 15576, 15604, 
15605, 15631, 15637, 15665, 15671, 15835, 15841, 15868, 15869, 
15896, 15902, 15924, 15926, 15961, 15966, 15988, 15995, 16017, 
16028, 16211, 16213, 16238, 16247, 16267, 16270, 16274, 16302, 
16304, 16332, 16333, 16366, 16367, 16387, 16395, 16563, 16564, 
16592, 16596, 16637, 16638, 16666, 16668, 16693, 16696, 16723, 
16731, 16756, 16759, 16961, 16962, 16991, 16996, 17017, 17025, 
17051, 17060, 17093, 17094, 17109, 17116, 17334, 17345, 17366, 
17367, 17396, 17402, 17430, 17437, 17451, 17457, 17479, 17480, 
17695, 17696, 17725, 17726, 17758, 17760, 17786, 17788, 17821, 
17845, 17848), class = "Date"), mean_cpue = c(1.34147348124682, 
1.86964964075215, 1.81794067339267, 0.665667643204071, 0.58288080140313, 
1.21618562008796, 0.915544650452313, 2.08485242966155, 1.03716945493794, 
1.32653950869479, 1.30387017192198, 0.818696953343472, 1.80344708020158, 
0.462802005064552, 0.500089944234575, 0.468688145292134, 3.77629506069873, 
2.06756426400247, 2.60938827682934, 0.936798217363299, 1.50723642250895, 
1.72839753752143, 1.58423249977652, 1.85978443365114, 1.22923787251904, 
1.86003452037686, 1.10727777349472, 1.4293872174252, 1.50010309934371, 
3.06022031046633, 1.49412683285299, 2.14503371546005, 1.06420000907848, 
1.1748728446591, 1.10022108873144, 1.44768795978073, 1.25036934931668, 
2.13048237961316, 1.06834543832824, 1.8752166760057, 0.758904479222882, 
0.961385073330007, 1.8663428674222, 0.745490766022096, 1.15427953668245, 
1.98858189404051, 2.27561614501712, 1.71143613797583, 1.49915849827066, 
0.841476235553629, 2.31692888870439, 1.35038151346165, 1.18518826020175, 
1.22169567368232, 1.10179032565335, 0.694614793373563, 0.983229683310633, 
1.12892772438535, 1.53364097932751, 0.787500879889251, 1.33114386888348, 
0.75361846092619, 1.71920865710658, 1.35852187828517, 1.689863533171, 
1.28266297991069, 1.86158062664132, 1.5293414824795, 1.0998008820738, 
1.94141263432714, 0.342436942826791, 0.714994071540338, 5.62005947683777, 
1.62440270638761, 3.82262996941896, 3.25889497807134, 1.54143260146352, 
1.86769619502575, 2.8059505280639, 3.58304933614583, 1.20470762672598, 
2.29674008364814, 0.689037082722997, 0.959492687511224, 0.567406828456012, 
0.844493420881774, 0.513055729898873, 0.986197068455855, 1.86584317723012, 
0.229688101851428, 1.25022486058644, 1.1701621335822, 0.742587041289875, 
0.625081199656213, 1.29798096608674, 0.396058527329973, 1.16988984259697, 
0.739772339659098, 0.39805108067041, 0.606186664268154, 0.300984338326838, 
1.27916344040608, 5.75511524259318, 3.58466880968582, 1.02559789553853, 
1.26985222650329, 1.54851107697843, 1.10318926209412, 1.3337665961789, 
1.21571684379214, 1.18341136267535, 1.44015621029248, 0.669940304071688, 
1.80318080838661, 0.919979963710556, 0.555260577151843, 2.32733965810763, 
1.08046383335355, 0.621234946065283, 1.79207073432654, 1.3398375035128, 
1.67582836446281, 1.3687859338262, 1.64687245759481, 0.880872177773362, 
0.972535713988714, 0.83891596232892, 2.09933443707235, 1.24879096699177, 
1.99677155577555, 1.67638127852795, 1.50710438838265, 2.27326799319906, 
1.83813683299605, 2.34551745405533, 1.93879794527393, 1.74635992056851, 
0.966061489637619, 1.1709324985094, 2.77527932681737, 4.63212808059003, 
2.50472488366812, 2.19392784029982, 3.64096784095526, 1.91684726897895, 
1.6466312330028, 2.84951794706046, 1.6841559743533, 1.62105139974506, 
1.46007933985039)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-150L), .internal.selfref = <pointer: 0x000001d8f6d11ef0>, sorted = "NODCCODE")

Thanks in advance for any help!!

解决方案

In the following, I have added group = year(date) to aesthetics so that line will connect only those points in the same year.

The scale_x_date(date_labels = "%b-%Y") is to have monthly ticks - not sure if you still want this.

library(ggplot2)
library(dplyr)
library(lubridate)

ggplot(MeanCPUE, aes(x=date, y=mean_cpue, group = year(date)))+
geom_line() +
geom_point()+
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"),
                   axis.text.x = element_text(angle = 90))  +
   scale_x_date(date_breaks = '1 year', date_labels = "%b-%Y") 

这篇关于如何将X轴上的日期格式化为R中的月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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