mountain_sn
mountain_sn

Reputation: 53

how to plot with factor variables in r

I have a data frame df, with each row containing several variables. And I want to plot with x representing time.

 time <- df[,1]
 plot(0,0,xlim=c(time[1],time[length(time)]),ylim = range(rowSums(df[,2:3])), t="n")

But it doesn't work because the first column has factors, rather than variables. How to get the range of factor variables? Thanks.

df:
   date      v1  v2  v3  v4 ...
01/09/2012   12  13  11
01/12/2012   13  15  10
01/20/2012   14  16  20
02/11/2012   10  17  18

Upvotes: 0

Views: 706

Answers (1)

eipi10
eipi10

Reputation: 93821

Convert date to date format. There are a number of ways to do this, but the lubridate package makes it easy:

library(lubridate)
df$date = mdy(df$date)

range(df$date)

[1] "2012-01-09 UTC" "2012-02-11 UTC"

plot(df$date, df$v1)

enter image description here

Upvotes: 2

Related Questions