Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在几年的时间序列中只绘制特定的月份?_R_Ggplot2 - Fatal编程技术网

R 如何在几年的时间序列中只绘制特定的月份?

R 如何在几年的时间序列中只绘制特定的月份?,r,ggplot2,R,Ggplot2,我的数据框架包括2002-2017年6月、7月和8月的每日温度数据 date temperature 2002-06-01 9.882000 2002-06-02 9.698667 2002-06-03 12.941667 ... 2017-08-29 13.271125 2017-08-30 16.332750 2017-08-31 20.986000 日期的格式为POSIXct。我试图创建一个只显示每年相关月份的绘图。我使用以下代码尝试

我的数据框架包括2002-2017年6月、7月和8月的每日温度数据

date          temperature
2002-06-01    9.882000
2002-06-02    9.698667
2002-06-03    12.941667
...
2017-08-29    13.271125
2017-08-30    16.332750
2017-08-31    20.986000
日期的格式为POSIXct。我试图创建一个只显示每年相关月份的绘图。我使用以下代码尝试了ggplot2:

ggplot(NULL, aes(x = date, y = temperature)) +
   geom_line(data = tair_summer_night_mean, color = "blue") +
   xlab(label = "Time") +
   ylab(label = "Night time temperature in °C")
这导致:


有没有办法告诉ggplot不要显示我没有数据的空间?

这是按照卡米尔的建议进行的。按面绘制数据可能是最简单的解决方案。您可以在数据中创建一个年份列,如她的引用中所示,然后对该列进行分组,或者您可以在gglot中计算分组变量,如下所示

library(ggplot2)
#
#  make some test data
#
  tair_summer_night_mean <- data.frame(date=c( seq.Date(as.Date("2002-06-01"), as.Date("2002-08-31"),1), 
                                        seq.Date(as.Date("2003-06-01"), as.Date("2003-08-31"),1), 
                                        seq.Date(as.Date("2004-06-01"), as.Date("2004-08-31"),1)))
  tair_summer_night_mean[,"temperature"] <-  runif(nrow(tair_summer_night_mean), min=9, max=21 )
#
#  make plots faceted by year
# 
  sp <-  ggplot(tair_summer_night_mean, aes(x = date, y = temperature)) +
         geom_line(color = "blue") + 
         xlab(label = "Time") +
         ylab(label = "Night time temperature in °C") +
         facet_wrap( ~ format(date, "%Y"), scales = "free_x")
   plot(sp)
库(ggplot2)
#
#做一些测试数据
#

tair_summer_night_是指数据帧中的no数据是如何编码的?是否将其编码为
NA
?否,列表中仅列出了6月、7月和8月的日期df@beetroot你为什么改变图像?对不起,我不知道那里发生了什么。希望它现在是正确的。不是一个完全相同的,但这篇文章应该有帮助:看起来很不错。非常感谢你!