0

次のデータがあります。

> Data
          Date    Start       End
1   2011-11-15 12:01:27 12:30:15 
2   2011-11-16 12:01:25 12:32:15 
3   2011-11-17 12:01:02 12:39:12 
4   2011-11-19 12:01:12 12:30:18

Duration列も追加しました

Data[,4] <- as.numeric(difftime(Data$End,Data$Start))
names(Data)[4] <- "Duration"

頭の中に、株式ローソク足またはOHLCチャートのような Start,End の視覚化があります。ここで、x 値は日付、y は End - Start です。

End が一番上にあり、長方形が Start に向かって下降します。長方形の高さは、Duration によって時間とともに変化します。つまり、各 Date は、Start と End の違いによって決定される異なる四角形の高さを持ちます。

ここで、x 軸は 2011 年 11 月 15 日から 2011 年 11 月 19 日までです。Y 軸は 12:00:00 から 12:40:00 までです。

ggplot ウィザードでこれを行う簡単な方法はありますか? Start と End の両方が時間とともに変化するため、geom_bar または geom_area ではなく、geom_ribbon または geom_polygon を使用する必要がありますか?

Duration の値が標準偏差の 2 より大きい日にバーの色が赤に変わると、さらにクールです!

4

2 に答える 2

6

私は nico と同様の構造を使用しています (ありがとう!):

date = c("2011-11-15", "2011-11-16", "2011-11-17", "2011-11-19")
start = c("12:01:27", "12:01:25", "12:01:02", "12:01:12")
end = c("12:30:15", "12:32:15", "12:39:12", "12:30:18")

次に、長方形の角を含むデータ フレームに配置します。

##I've made the rectangles 2 hours wide
df = data.frame(date = as.POSIXct(date),
         ystart = as.POSIXct(start, format="%H:%M:%S"), 
         yend = as.POSIXct(end, format="%H:%M:%S"),
         xstart=as.POSIXct(paste(date, "12:00:00"), format="%Y-%m-%d %H:%M:%S"),
         xend = as.POSIXct(paste(date, "14:00:00"), format="%Y-%m-%d %H:%M:%S"))

次に、次を使用しますgeom_rect

ggplot() + geom_rect(data=df, aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart))

条件に基づいてそれらの一部を赤くしたい場合は、データ フレームに追加の列を作成するだけです。

##Your condition is something to do with the sd
df$isRed = c(TRUE, FALSE)

次に、2 つの ggplot レイヤーを追加します。

ggplot() + geom_rect(data=subset(df, !isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart)) +
           geom_rect(data=subset(df, isRed), aes(ymin=ystart, ymax=yend,
                           xmin=xend, xmax=xstart), colour="red")

グラフの例

ここに画像の説明を入力

于 2012-04-25T08:30:20.453 に答える
3

私は ggplot を使用しませんが、基本的な R ソリューションを提供できます

# Generate the data
date <- c("2011-11-15", "2011-11-16", "2011-11-17", "2011-11-19")
start <- c("12:01:27", "12:01:25", "12:01:02", "12:01:12")
end <- c("12:30:15", "12:32:15", "12:39:12", "12:30:18")

# Put everything in a data frame and convert to POSIXct objects
# The times will be all converted to today's date
# but this will not influence the plot
df <- data.frame(date = as.POSIXct(date),
                 start = as.POSIXct(start, format="%H:%M:%S"), 
                 end = as.POSIXct(end, format="%H:%M:%S"))

# Get the working range for the axes in order to make them nicer (see below)
x.from <- as.POSIXct(min(date))
x.to <- as.POSIXct(max(date))
y.from <- as.POSIXct(min(start), format="%H:%M:%S")
y.to <- as.POSIXct(max(end), format="%H:%M:%S")

# Create an empty plot, as rect will not create a new one
# We put no axes on the plot
plot(0, "n", xaxt="n", yaxt="n", ylab="", xlab="Day", 
     ylim=c(from, to), xlim=range(df$date))

# Now draw the rectangles (I made them 2 hours-wide)
rect(df$date-3600, df$start, df$date+3600, df$end, col="black")

days <- seq(x.from, x.to, 24*3600)
times <- seq(y.from, y.to, 300) # 5 min (=300 s) axis ticks
# Finally add the axes
axis(1, at=days, labels=strftime(days, "%d/%m"))
axis(2, at=times, labels=strftime(times, "%H:%M"), las=1)

結果:

ローソク足のようなプロット

于 2012-04-25T07:12:19.710 に答える