在日常月份范围内分开绘制年份 [英] Plot separate years on a common day-month scale

查看:109
本文介绍了在日常月份范围内分开绘制年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为2012年和2013年的夏季创建一个时间序列温度图。

唯一的问题是我想要数据系列绘制一个

  temp <-c(22, 22,26,23,18,20,18,17)
日期< - as.Date(c(2012-06-01,2012-07-01,2012-08-01 ,2012-09-01,2013-06-01,2013-07-01,2013-08-01,2013-09-01))
year < - as.factor(c(2012,2012,2012,2012,2013​​,2013​​,2013​​,2013​​))

df< - data.frame(temp,date,year)

到目前为止,我使用<$ c (ggplot2)
ggplot(df,aes(date))


  require(ggplot2)
ggplot ,temp,color = year))+
geom_point()

需要在x轴上列出完整的日期,事实上,它应该只有月和日,这可能会解决问题,我.e。

  df $ dayMo < -  c(07-01,07-02,07- 03,07-04,07-01,07-02,07-03,07-04)
>

我没有看到一种方法来获取 as.Date as.POSIXct strptime )来允许这个日日格式。



我也对某些人开放其他创造性的方式来完成这项工作。任何想法?如果你的基本数据集是temp和date,那么这样可以避免操纵原始数据框:



  ggplot(df)+ 
geom_point(aes(x = strftime(date,format =%m-%d),
y = temp,
color = strftime(date,format =%Y)),size = 3)+
scale_color_discrete(name =Year)+
labs( x =date)



编辑(响应OP的评论)。

因此,这将上面的方法与Henrik的方法结合起来,对x轴使用日期而不是char,并避免修改原始df。

  library(ggplot2)
ggplot(df)+
geom_point(aes(x = as.Date(paste(2014,strftime(date,format =%m-% d $),
y = temp,
color = strftime(date,format =%Y)),size = 3)+
scale_color_discr ete(name =Year)+
labs(x =date)


I want to create a time series plot of temperatures for the summers of 2012 and 2013.

The only problem is that I want the data series to plot one on top of the other so they can be easily compared instead of sequentially along the date axis.

temp <- c(22, 22, 26, 23, 18, 20, 18, 17)
date <- as.Date(c("2012-06-01", "2012-07-01","2012-08-01","2012-09-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01"))
year <- as.factor(c("2012", "2012", "2012", "2012","2013", "2013","2013","2013"))

df<- data.frame(temp, date, year)

Here's what I have so far using ggplot2

require(ggplot2)
ggplot(df, aes(date, temp, color=year))+
  geom_point()

The graph doesn't need to have the full dates listed on they x axis, in fact, it should probably just have month and day and that might solve the problem, i.e.

df$dayMo <- c("07-01", "07-02","07-03","07-04","07-01","07-02","07-03","07-04")

I didn't see a way to get as.Date or as.POSIXct (strptime) to allow this day-month format.

I'm also open to some other creative way of getting this done. Any ideas?

解决方案

If your base dataset is temp and date, then this avoids manipulating the original data frame:

ggplot(df) +
  geom_point(aes(x=strftime(date,format="%m-%d"),
                 y=temp, 
                 color=strftime(date,format="%Y")), size=3)+
  scale_color_discrete(name="Year")+
  labs(x="date")

EDIT (Response to OP's comment).

So this combines the approach above with Henrik's, using dates instead of char for the x-axis, and avoiding modification of the original df.

library(ggplot2)
ggplot(df) +
  geom_point(aes(x=as.Date(paste(2014,strftime(date,format="%m-%d"),sep="-")),
                 y=temp, 
                 color=strftime(date,format="%Y")), size=3)+
  scale_color_discrete(name="Year")+
  labs(x="date")

这篇关于在日常月份范围内分开绘制年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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