在 R 中创建时间序列图,每天对实例进行分箱,并根据分箱中的实例数绘制点大小

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-01-01 100
2015-01-01 300
2015-01-01 350
2015-02-01 400
2015-02-01 50

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

(我做了一个情节的示例图片,但我还没有足够的声望点数 post 它!)

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

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}
  }
}

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

这是我对你的尝试。我希望我能正确阅读你的问题。似乎您想使用 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()。希望对您有所帮助。