2016-03-30 44 views
3

我想繪製以下數據幀,其中有3個不同的時間序列(由user0,user1和user2標識) 。每行都有一個用戶標識符,日期和一個值。R - 繪製多個時間序列,相同的x軸值,但時間在數據幀內混合

> df 
    userId  date steps 
1 user0 2016-03-24 794 
2 user0 2016-03-25 562 
3 user0 2016-03-26 682 
4 user0 2016-03-27 722 
5 user0 2016-03-28 883 
6 user1 2016-03-24 3642 
7 user1 2016-03-25 3776 
8 user1 2016-03-26 3585 
9 user1 2016-03-27 3585 
10 user1 2016-03-28 3471 
11 user2 2016-03-24 5959 
12 user2 2016-03-25 5933 
13 user2 2016-03-26 5802 
14 user2 2016-03-27 6094 
15 user2 2016-03-28 5903 
> dput(df) 
structure(list(userId = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("user0", "user1", 
"user2"), class = "factor"), date = structure(c(16884, 16885, 
16886, 16887, 16888, 16884, 16885, 16886, 16887, 16888, 16884, 
16885, 16886, 16887, 16888), class = "Date"), steps = c(794L, 
562L, 682L, 722L, 883L, 3642L, 3776L, 3585L, 3585L, 3471L, 5959L, 
5933L, 5802L, 6094L, 5903L)), .Names = c("userId", "date", "steps" 
), row.names = c(NA, -15L), class = "data.frame") 

我想情節都用不同的顏色和與日期x軸時間序列(然而,許多存在由用戶id字段標識)。我嘗試了以下方法,但正如您所看到的,日期在x軸上重複。

plot(df$steps, axes=F, xlab="", ylab="Steps", ylim=c(0,max(df$steps))) 
axis(2) 
axis(1, at = seq_along(df$date), labels = df$date, las = 2, cex.axis = 0.70) 
box() 

enter image description here

我看了看其他的帖子,如「Plot multiple lines (data series) each with unique color in R」和「Plotting multiple time series on the same plot using ggplot()」,但他們沒有我的時間變量的問題被混在其他數據。

使用彩色線條有和無ggplot的解決方案將不勝感激。

回答

2

隨着ggplot:

library(ggplot2) 
ggplot(df, aes(x = date, y = steps, colour = userId)) + geom_line() 

plot with 3 lines and date x-axis


的等效(但仍然相當難看)基礎R版本需要更多的工作:

plot(0, type = 'n', axes = FALSE, xlab = 'date', ylab = 'steps', 
    xlim = c(min(df$date), max(df$date)), 
    ylim = c(min(df$steps) - 100, max(df$steps) + 100)) 
axis.Date(1, df$date, format = '%F') # `axis.Date` is helpful here 
axis(2, seq(0, max(df$steps + 500), 500)) 
box() 
lapply(split(df, df$userId), function(x){lines(x$date, x$steps, 
               col = as.numeric(substr(x$userId, 5, 5)) + 1)}) 
# `paste` extra space to align legend correctly...*sigh* 
legend('bottomright', paste(levels(df$userId), ' '), col = 1:3, lty = 1) 

base R multi-line plot

請注意,它需要一點點微調。

3

這裏是一個基礎R版本:

plot(0, 0, type = "n", xlim = range(df$date), ylim = c(0, max(df$step)), axes = FALSE, xlab = "", ylab = "steps") 
axis(2, las = 1) 
axis(1, at = df$date, labels = df$date, las = 2, cex.axis = 0.70) 
box() 

cols <- c("red", "green", "blue") 
for (i in 1:length(unique(df$userId))) 
    with(df[df$userId == unique(df$userId)[i], ], lines(date, steps, col = cols[i])) 

enter image description here

相關問題