繁体   English   中英

如何在 R 中将 x 轴上的日期格式化为月份和年份

[英]How to format date on x-axis to month and year in R

我已经看到发布了几个关于格式化日期的问题,但无法找到一个来解决我在这里遇到的问题。

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

我在这里问了一个类似的问题: How to plot mean density by year and month但是这个答案不能解决日期问题。

我试过这段代码

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

这给了我这个 plot .. 这很接近,但它不是 label 月份,它通过非抽样月份将抽样月份与一条线连接起来。

在此处输入图像描述

我正在尝试用日期显示在 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.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")

提前感谢您的帮助!!

在下文中,我将group = year(date)添加到美学中,以便这条线仅连接同一年的那些点。

scale_x_date(date_labels = "%b-%Y")是每月刻度 - 不确定你是否仍然想要这个。

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM