我的数据框中有一个名为date
的列,另一列名为values
。我想根据一周的日子绘制值。
我知道我们可以使用函数weekdays()
从一天开始一周的几天。但我不确定如何使用values
(y轴)绘制图表与星期几(x轴)
Date Values
12-03-2006 0.5
14-03-2006 0.6
18-03-2006 0.9
23-03-2006 1.1
02-04-2006 2.1
我试过这个
plot(weekdays(df$Date),df$values)
我收到了这个错误:
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
有人可以帮我解决这个错误吗?
答案 0 :(得分:3)
在绘制之前,您必须将weekdays(df$date)
更改为一个因子。
df$Date <- as.factor(weekdays(df$Date))
会使它成为一个因素,因此你现在可以使用它来绘制。 plot(df$Date,df$Values)