简体   繁体   English

以共同的日-月尺度绘制单独的年份

[英]Plot separate years on a common day-month scale

I want to create a time series plot of temperatures for the summers of 2012 and 2013. 我想创建一个2012年和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 这是我到目前为止使用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, ie 该图不需要在x轴上列出完整的日期,实际上,它可能应该只包含月份和日期,并且可以解决问题,即

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. 我没有找到一种方法来获取as.Dateas.POSIXctstrptime )以允许这种日月格式。

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). 编辑 (响应OP的评论)。

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. 因此,这将上述方法与Henrik的方法结合在一起,在x轴上使用日期而不是char,并避免了对原始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")

Another possibility is to create a fake date where a single year is concatenated with month and day from the original 'date'. 另一种可能性是创建一个假日期,其中将一年与原始“日期”的月份和日期连接在一起。 An x variable of class Date is easy to format with scale_x_date . Date类的x变量很容易使用scale_x_date Load scales package to access nice breaks and formatting functions: labels = date_format() ; 加载scales包以访问漂亮的中断和格式化功能: labels = date_format() ; breaks = date_breaks() . breaks = date_breaks() See strptime for other date formats. 有关其他日期格式,请参见strptime

library(scales)

df$date2 <- as.Date(paste(2014, format(date, "%m-%d"), sep = "-"))

ggplot(df, aes(date2, temp, color = year)) +
  geom_point() +
  scale_x_date(labels = date_format("%m/%d"))

ggplot(df, aes(date2, temp, color=year)) +
  geom_point() +
  scale_x_date(labels = date_format("%b-%d"),
               breaks = date_breaks("months"))

If you want to plot both years in the same plot above each other then use dayMo as your x values (they don't have to be date object). 如果要在同一图dayMo两个年份绘制在彼此之上,则可以将dayMo用作x值(它们不必是date对象)。

df$dayMo<-c("06-01", "07-01","08-01","09-01","06-01", "07-01","08-01","09-01")
ggplot(df, aes(dayMo, temp, color=year))+
  geom_point()

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

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