首先让我们准备更多数据
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))