【问题标题】:creating a time series plot in R, binning instances in each day, and plotting point size by number of instances in the bin在 R 中创建时间序列图,每天对实例进行分箱,并按箱中实例数绘制点大小
【发布时间】:2015-08-20 00:13:54
【问题描述】:

我有好几个月的数据,每天都有每秒的读数。有几个缺失值。数据位于 R 格式的数据框中:


日期                  值
2015-01-01      100
2015-01-01      300
2015-01-01      350
2015-02-01      400
2015-02-01      50

在我的代码中,此数据框称为“组合”,包含组合$time(用于日期)和组合$value(用于值)。我想按天绘制值,显示按五分位数分类的每个值范围的实例数(例如,每天介于 100 和 200 之间的值的数量、介于 200 和 300 之间的数字等)。我已经将 bin 边界的值定义为下限、上限等。在这个图中,我希望点的大小与当天该范围内值的实例数相对应。

(我制作了该情节的示例图片,但我还没有足够的声望点来发布它!)

我当然没有写出最有效的方法来做到这一点,但我的主要问题是,既然我已经成功地将这些值按天分类,那么如何实际生成绘图。我也喜欢任何关于更好方法的建议。这是我到目前为止的代码:

lim<-c(lowlimit, midlowlimit, midupperlimit, uplimit)
bin <- c(0, 0, 0, 0)
for (i in 2:length(combined$values){
  if (is.finite(combined$value[i])=='TRUE'){  # account for NA values 
    if (combined$time[i]==combined$time[i-1]){
      if (combined$value[i] <= lowlimit){
        bin[1]=bin[1]+1
        i=i+1
      }
      else if (combined$value[i] > lowlimit && combined$value[i] <= midlowlimit){
        bin[2]=bin[2]+1
        i=i+1
      }
      else if (combined$value[i] > midlowlimit && combined$value[i] <= midupperlimit ){
        bin[3]=bin[3]+1
        i=i+1
      }
      else if (combined$value[i] > midupperlimit && combined$value[i] <= uplimit){
        bin[4]=bin[4]+1
        i=i+1
      }
      else if (combined$skin_temp[i] > uplimit ){
        bin[5]=bin[5]+1
        i=i+1
      }
    }

  else{
     ### I know the plotting portion here is incorrect ###
    for (j in 1:5){
    ggplot(combined$date[i], lim[j]) + geom_point(aes(size=bin[j]))}
    i = i+1}
  }
}

非常感谢您提供的任何帮助!

【问题讨论】:

    标签: r ggplot2 binning timeserieschart


    【解决方案1】:

    这是我对你的尝试。我希望我正确地阅读了你的问题。您似乎想使用cut() 为每天创建五个组。然后,您要计算每组中存在多少数据点。您希望每天都执行此操作。我创建了一个示例数据来展示我所做的事情。

    mydf <- data.frame(Date = as.Date(c("2015-01-01", "2015-01-01", "2015-01-01", "2015-01-01",
                                        "2015-01-02", "2015-01-02", "2015-01-02", "2015-01-02"),
                                        format = "%Y-%m-%d"),
                       Value = c(90, 300, 350, 430, 210, 330, 410, 500),
                       stringsAsFactors = FALSE)
    
    ### This is necessary later when you use left_join().
    foo <- expand.grid(Date = as.Date(c("2015-01-01", "2015-01-02"), format = "%Y-%m-%d"),
                       group = c("a", "b", "c", "d", "e"))
    
    library(dplyr)
    library(ggplot2)
    library(scales)
    
    ### You group your data by Date, and create five sub groups using cut().
    ### Then, you want to count how many data points exist for each date by
    ### group. This is done with count(). In this case, there are some subgroups
    ### which have no data points. They do not exist in the data frame that
    ### count() returns. So you want to use left_join() with foo. foo has all
    ### possible combination of Date and group. Once you join the two data frames,
    ### You want to replace NA with 0, which is done in the last mutate().
    
    mutate(group_by(mydf, Date),
           group = cut(Value, breaks = c(0, 100, 200, 300, 400, 500),
           labels = c("a", "b", "c", "d", "e"))) %>%
    count(Date, group) %>%
    left_join(foo, ., by = c("Date" = "Date", "group" = "group")) %>%
    rename(Total = n) %>%
    mutate(Total = replace(Total, which(Total %in% NA), 0)) -> out
    
    
    ### Time to draw a figure
    ggplot(data = out, aes(x = Date, y = Total, size = Total, color = group)) +
    geom_point() +
    scale_x_date(breaks = "1 day")
    

    如果要修改 y 轴,可以使用scale_y_continuous()。我希望这会对你有所帮助。

    【讨论】:

    • 谢谢!这正是我一直在寻找的,除了绘图略有不同。我想把“箱子”放在 y 轴上(所以在 100、200、300、400 和 500 处打勾)。所以我认为在这种情况下,我可以在 ggplot 中使用 y = value,但 size = total。我真的很感激!
    • @epi_bio 很高兴为您提供帮助。 :)
    • 只是对绘图代码的更新,除非它对其他人有用:ggplot(data = foo, aes(x = Date, y = group, size = out$Total, color = out$Total)) 再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多