[daily-r]factor->characteristic->number conversion

I want to plot line graphs to show the y changes along with time series, just like in this post: http://stackoverflow.com/questions/12500218/ggplot2-line-plotting-with-time-series-and-multi-spline/12500368#comment16830065_12500368

However, the x-axis treat the time points as five groups rather than numbers, as in the following figure. 

 

This one is really what I am talking about: 

==

Test raw data and R scripts to draw the first wrong figure are followed. 

1 a   4.17125 41.33875    29.674375   8.551875    5.5
2 b   4.101875    29.49875    50.191875   13.780625   4.90375
3 c   3.1575  29.621875   78.411875   25.174375   7.8012
1 df <- read.delim("~/Desktop/df.b", header=F)
2 colnames(df)<-c("sample",0,15,30,60,120)
3 df2<-melt(df,id="sample")
4 ggplot(data = df2, aes(x=variable, y= value, group = sample, colour=sample)) + geom_line() + geom_point()

After the following conversion, the R script is right. 

df2$variable <- as.numeric(as.character(df2$variable))

Factors look like character vectors but they are not! They are integer codes and a set of labels that go with the codes. Hence, as.numeric will simply return the integer codes. That's why I converted to character first. A more cryptic way to do it would be as.numeric(levels(df2$Time)[df2$Time])

posted @ 2012-09-20 21:00  Puriney  阅读(235)  评论(0编辑  收藏  举报