【问题标题】:Selecting and plotting months in ggplot2在 ggplot2 中选择和绘制月份
【发布时间】:2014-10-22 18:44:00
【问题描述】:

我有一个这种格式的时间序列数据集,其中包含两列日期(例如 1980 年 1 月、1980 年 2 月...2013 年 12 月)及其对应的温度。这个数据集是从 1980 年到 2013 年。我正在尝试在 ggplot 中分别对月份的时间序列进行子集化和绘制(例如,我只想要所有 2 月,以便我可以使用 ggplot 绘制它)。尝试了以下,但二月1是空的

Feb1 <- subset(temp, date ==5)

我的数据集的结构是:

'data.frame':   408 obs. of  2 variables:
$ date   :Class 'yearmon'  num [1:359] 1980 1980 1980 1980 1980 ...
$ temp: int  16.9 12.7 13 6 6.0 5 6 10.9 0.9 16 ...

【问题讨论】:

  • 任何答案对你有用吗?
  • 是的,非常感谢大家。向我展示了做同一件事的不同方式!感谢您的帮助!

标签: r ggplot2 subset


【解决方案1】:

这个呢?

library(zoo)

# Generating some data:
df <- data.frame(date = as.yearmon("1980-01") + 0:407/12, val = rnorm(408))

# Subsetting to get a specific month:
df.sub <- subset(df, format(df$date,"%b")=="Jan")

# The actual plot:
ggplot(df.sub) + geom_line(aes(x = as.Date(date), y = val))

【讨论】:

    【解决方案2】:

    我相信您的专栏属于“yearmon”类,格式为“mm YY”。我对你如何通过'date == 5'对数据进行子集有点困惑。下面我尝试一个方法。

    temp$month<-substr(temp$date,1,3)
    Feb1<-subset(temp,month=='Feb')
    
    #more elegant
    
    Feb1<-subset(temp,substr(temp$date,1,3)=='Feb')
    

    【讨论】:

      【解决方案3】:

      您也可以直接在 ggplot2 中绘制子集,而无需创建新的数据框。

      基于 RStudent 的解决方案:

      library(zoo)
      
      # Generating some data:
      df <- data.frame(date = as.yearmon("1980-01") + 0:407/12, val = rnorm(408))
      
      library(ggplot2)
      ggplot(df[format(df$date,"%b")=="Jan", ], aes(x = as.Date(date), y = val))+
         geom_line()
      

      【讨论】:

        【解决方案4】:

        将数据转换为动物园,使用cycle分割成月份,使用autoplot.zoo进行绘图。下面我们展示了四种不同的绘图方式。首先我们只在一月份绘制。然后我们将每个月的所有月份绘制在一个单独的面板中,然后我们将每个月的所有月份绘制为一个单独的系列,所有这些都在同一个面板中。最后我们使用monthplot(不是ggplot2)以不同的方式将它们全部绘制在一个面板中。

        library(zoo)
        library(ggplot2)
        
        # test data
        set.seed(123)
        temp <- data.frame(date = as.yearmon(1980 + 0:479/12), value = rnorm(480))
        
        z <- read.zoo(temp, FUN = identity) # convert to zoo
        
        # split into 12 series and cbind them together so zz480 is 480 x 12
        # Then aggregate to zz which is 40 x 12
        
        zz480 <- do.call(cbind, split(z, cycle(z)))
        zz <- aggregate(zz480, as.numeric(trunc(time(zz480))), na.omit)
        
        ### now we plot this 4 different ways
        #####################################
        
        # 1. plot just January
        autoplot(zz[, 1]) + ggtitle("Jan")
        
        # 2. plot each in separate panel
        autoplot(zz)
        
        # 3. plot them all in a single panel
        autoplot(zz, facet = NULL)
        
        # 4. plot them all in a single panel in a different way (not using ggplot2)
        monthplot(z)
        

        请注意,计算zz 的另一种方法是:

        zz <- zoo(matrix(coredata(z), 40, 12, byrow=TRUE), unique(as.numeric(trunc(time(z)))))
        

        更新:添加了绘图类型并改进了方法。

        【讨论】:

          猜你喜欢
          • 2020-09-30
          • 2011-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-26
          • 2023-04-08
          • 2017-07-12
          • 2017-11-09
          相关资源
          最近更新 更多