Plotting Simple Data in R

后端 未结 7 1752
旧时难觅i
旧时难觅i 2021-02-01 06:28

I have a comma separated file named foo.csv containing the following data:

scale, serial, spawn, for, worker
5, 0.000178, 0.000288, 0.000292, 0.0003         


        
7条回答
  •  灰色年华
    2021-02-01 06:45

    I'm new in R, but if you want to draw scale vs. all other columns in one plot, easy and with some elegance :) for printing or presentation, you may use Prof. Hadley Wickham's packages ggplot2 & reshape.

    Installation:

    install.packages(“ggplot2”,dep=T)
    install.packages(“reshape”,dep=T)
    

    Drawing your example:

    library(ggplot2)
    library(reshape)
    
    #read data
    data = read.table("foo.csv", header=T,sep=",")
    
    #melt data “scale vs. all”
    data2=melt(data,id=c("scale"))
    data2
    
       scale variable      value
    1      5   serial   0.000178
    2     10   serial   0.156986
    3     12   serial   2.658998
    4     15   serial 188.023411
    5      5    spawn   0.000288
    6     10    spawn   0.297926
    7     12    spawn   6.059502
    8     15    spawn 719.463264
    9      5     for.   0.000292
    10    10     for.   0.064509
    11    12     for.   0.912733
    12    15     for. 164.111459
    13     5   worker   0.000300
    14    10   worker   0.066297
    15    12   worker   0.923606
    16    15   worker 161.687982
    
    #draw all variables at once as line with different linetypes
    qplot(scale,value,data=data2,geom="line",linetype=variable)
    

    You could also use points (geom=”points”), choose different colours or shapes for different variables dots (colours=variable or shape=variable), adjust axis, set individual options for every line etc.

    Link to online documentation.

提交回复
热议问题