1

私は何ヶ月にもわたるデータを持っており、毎日の 1 秒あたりの読み取り値があります。欠損値がいくつかあります。データは、次の形式の 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 の間の値など) でビン化された値の各範囲のインスタンスの数を示します。ビン境界の値を下限、上限などとして既に定義しました。このプロットでは、ポイントのサイズを、その日のその範囲内の値のインスタンス数に対応させたいと考えています。

(プロットのサンプル画像を作成しましたが、まだ投稿するのに十分な評判ポイントがありません!)

これを行うための最も効率的な方法を書いたわけではありませんが、私の主な質問は、日ごとの値のビン化に成功した今、実際にプロットを生成する方法です。また、これを行うためのより良い方法についての提案もお待ちしています。これが私がこれまでに持っているコードです:

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

あなたが提供できるどんな助けにも感謝します!

4

1 に答える 1

1

これが私の試みです。あなたの質問を正しく読んでいただければ幸いです。cut()毎日5つのグループを作成するために使用したいようです。次に、各グループに存在するデータ ポイントの数をカウントします。この操作を毎日実行します。私が行ったことを示すために、サンプル データを作成しました。

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()。これがお役に立てば幸いです。

ここに画像の説明を入力

于 2015-08-20T02:06:40.967 に答える