首先讓多準備一些數據
set.seed(123)
df <- data.frame(Time = paste0("08:", sample(35:55, 40, replace = TRUE)),
Length = sample(20:50, 40, replace = TRUE),
stringsAsFactors = FALSE)
df <- df[order(df$Time), ]
df$Attempt <- unlist(sapply(rle(df$Time)$lengths, function(i) 1:i))
df$Time <- as.POSIXct(df$Time, format = "%H:%M") # Fixing y axis
head(df)
Time Length Attempt
6 08:35 24 1
18 08:35 43 2
35 08:35 34 3
15 08:37 37 1
30 08:38 33 1
38 08:39 38 1
據我瞭解,你想保留的同一離開家的時間觀察的順序。起初,我忽略了,並得到了散點圖這樣的:
ggplot(data = df, aes(x = Length, y = Time)) +
geom_point(aes(size = Length, colour = Length)) +
geom_path(aes(group = Time, colour = Length), alpha = I(1/3)) +
scale_size(range = c(2, 7)) + theme(legend.position = 'none')
但考慮三個維度(Time
,Length
和Attempt
)散點圖不再可以向我們展示的所有信息。我希望我理解正確的你,這是你在找什麼:
ggplot(data = df, aes(y = Time, x = Attempt)) + geom_tile(aes(fill = Length))
這是接近,但圖形不具有縮放時間y軸。事情看起來相距1分鐘,但你看到8:51緊接着8:53(8:41和8:43同上)。 – Dave
@Dave,我又增加了一行來解決這個問題並更新了兩張圖片。 – Julius