注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Puriney's Notes

Puriney=purine+Y, my Wonderland

 
 
 

日志

 
 

【Daily-R】factor->characteristic->number 转换  

2012-09-20 21:04:34|  分类: BioDraw |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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 treats the time points as five groups rather than numbers, as in the following figure. 

【Daily-R】factor-characteristic-number 转换 - Puriney - Purineys Notes
This one is really what I am talking about: 
【Daily-R】factor-characteristic-number 转换 - Puriney - Purineys Notes

====

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  conversion below , 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])

 
 == end && reference

http://stackoverflow.com/questions/12500218/ggplot2-line-plotting-with-time-series-and-multi-spline/12500368#comment16830065_12500368

  评论这张
 
阅读(904)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017