使用ggplot2绘制特定的日期范围

时间:2022-09-14 14:59:06

I am trying to plot data within a specific date range for individuals. I've pasted example code below that has worked before, but I don't wall all the data, just what happens after 11/30/2016. I've tried the solution shown here (Select Data After Specific Date), but I get "Error: Invalid input: date_trans works with objects of class Date only." Any ideas?

我试图在个人的特定日期范围内绘制数据。我已经粘贴了以前有效的示例代码,但是我没有覆盖所有数据,只是在2016年11月30日之后会发生什么。我已经尝试了这里显示的解决方案(在特定日期后选择数据),但我得到“错误:输入无效:date_trans仅适用于类Date的对象。”有任何想法吗?

 mydf<- data.frame(Date = as.Date(Subject$date, format = "%m/%d/%Y"),
                   variable1 = (Subject$var1),
                   variable2 = (Subject$var2), 
                   variable3 = (Subject$var3),
                   variable4 = (Subject$var4))

ggplot(mydf, aes(Date > "2016-11-30", variable1)) + 
  geom_point() + 
  stat_smooth(color = "blue", fill = "lightskyblue") +
  theme(axis.title.x = element_blank()) + 
  ylab("") + xlab("")+ 
  ggtitle("Variable 1 units") + 
  scale_x_date(date_breaks = "months", 
               date_labels = "%b%y") + 
  theme_hc()

1 个解决方案

#1


2  

Try subsetting the dataframe while feeding it to ggplot like this:

尝试将数据帧子集化,同时将其提供给ggplot,如下所示:

ggplot(data=mydf[which(mydf$Date>"2016-11-30"),],
       aes(x=Date, y=variable1)) + 
       geom_point() + 
       stat_smooth(color = "blue", fill = "lightskyblue") +
       theme(axis.title.x = element_blank()) + 
       ylab("") + xlab("")+ 
       ggtitle("Variable 1 units") + 
       scale_x_date(date_breaks = "months", 
                    date_labels = "%b%y") + 
       theme_hc()

#1


2  

Try subsetting the dataframe while feeding it to ggplot like this:

尝试将数据帧子集化,同时将其提供给ggplot,如下所示:

ggplot(data=mydf[which(mydf$Date>"2016-11-30"),],
       aes(x=Date, y=variable1)) + 
       geom_point() + 
       stat_smooth(color = "blue", fill = "lightskyblue") +
       theme(axis.title.x = element_blank()) + 
       ylab("") + xlab("")+ 
       ggtitle("Variable 1 units") + 
       scale_x_date(date_breaks = "months", 
                    date_labels = "%b%y") + 
       theme_hc()