tags:

views:

1343

answers:

5

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.000300
10, 0.156986, 0.297926, 0.064509, 0.066297
12, 2.658998, 6.059502, 0.912733, 0.923606
15, 188.023411, 719.463264, 164.111459, 161.687982

I essentially have two questions:

1) How do I plot the first column (x-axis) versus the second column (y-axis)? I'm trying this (from reading this site):

data <- read.table("foo.csv", header=T,sep=",")
attach(data)
scale <- data[1]
serial <- data[2]
plot(scale,serial)

But I get this error back:

Error in stripchart.default(x1, ...) : invalid plotting method

Any idea what I'm doing wrong? A quick Google search reveals someone else with the same problem but no relevant answer. UPDATE: It turns out it works fine if I skip the two assignment statements in the middle. Any idea why this is?

The second question follows pretty easily after the first:

2) How do I plot the first column (x-axis) versus all the other columns on the y-axis? I presume it's pretty easy once I get around the first problem I'm running into, but am just a bit new to R so I'm still wrapping my head around it.

+1  A: 

I am far from being an R expert, but I think you need a data.frame:

plot(data.frame(data[1],data[2]))

It does at least plot something on my R setup!

Following advice in luapyad's answer, I came up with this. I renamed the header "scale":

scaling, serial, spawn, for, worker
5, 0.000178, 0.000288, 0.000292, 0.000300
10, 0.156986, 0.297926, 0.064509, 0.066297
12, 2.658998, 6.059502, 0.912733, 0.923606
15, 188.023411, 719.463264, 164.111459, 161.687982

then:

foo <- read.table("foo.csv", header=T,sep=",")
attach(foo)
plot( scaling, serial );
anon
+3  A: 

You don't need the two lines:

scale <- data[1]
serial <- data[2]

as scale and serial are already set from the headers in the read.table.

Also scale <- data[1] creates an element from a data.frame

  data[1]
1     5
2    10
3    12
4    15

whereas scale from the read.table is a vector

5 10 12 15

and the plot(scale, serial) function expects vector rather than a data.frame, so you just need to do

plot(scale, serial)

One approach to plotting the other columns of data on the y-axis:

plot(scale,serial, ylab="")
par(new=TRUE) 
plot(scale,spawn,axes=F, ylab="", type="b")
par(new=TRUE) 
plot(scale,for., axes=F, ylab="", type="b")
par(new=TRUE) 
plot(scale,worker,axes=F, ylab="", type="b")

There are probably better ways of doing this, but that is beyond my current R knowledge....

luapyad
So what should his plot() call look like?
anon
just use plot(scale,serial)
luapyad
Unfortunately, "scale" seems to be a global of some sort.
anon
I think it should be ok (the above all works fine on my R anyway :)). The scale variable will be set from the read.table, and it should not interfere with the "global" scale() function.
luapyad
Yup, that did it. Thanks for the help!
Chris Bunch
+2  A: 

Try this:

data <- read.csv('foo.csv')
plot(serial ~ scale, data)
dev.new()
plot(spawn ~ scale, data)
dev.new()
plot(for. ~ scale, data)
dev.new()
plot(worker ~ scale, data)
Jouni K. Seppänen
you could combine the 4 plots into one graphic by using either matplot(data[1],data[2,5],type="b") or with the individual plot commands preceded by layout(matrix(c(1,2,3,4),2,2,byrow=TRUE))
bubaker
+3  A: 

Hi! 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.

zzr
+1  A: 

In your example,

plot(scale, serial)

won't work because scale and serial are both data frames, e.g.

class(scale)
[1] "data.frame"

You could try the following and use points(), once the plot has been generated, to plot the remaining columns. Note, I used the ylim parameter in plot to accommodate the range in the third column.

data <- read.csv('foo.csv', header=T)
plot(data$scale, data$serial, ylim=c(0,750))
points(data$scale, data$spawn, col='red')
points(data$scale, data$for., col='green')
points(data$scale, data$worker, col='blue')
andrewj