monksy
monksy

Reputation: 14234

Plotting Multiple Graphs using R

I currently have a dataset which has a format of: (x, y, type)

I've used the code that is found on the example of plotting with Postgres through R.

My question is: How would I get R to generate multiple graphs for each unique "type" column?

I'm new to R, so my appologies if this is something that is extremely easy and I just lack the understanding of loops with R.

So lets say we have this data:

(1,1,T), (1,2,T), (1,3,T), (1,4,T), (1,5,T), (1,6,T), 
(1,1,A), (1,2,B), (1,3,B), (1,4,B), (1,5,A), (1,6,A), 
(1,1,B), (1,2,B), (1,3,C), (1,4,C), (1,5,C), (1,6,C), 

It would plot 4 individual graphs on the page. One for each of the types T, A, B, and C. [Ploting x,y]

How would I do that with R when the data coming in may look like the data above?

Upvotes: 2

Views: 6298

Answers (3)

Señor O
Señor O

Reputation: 17412

While the other post has some good info, there's a faster way to do all that. So assuming your data frame or matrix is called DF and is in the form above (where each (1,2,B) or whatever is a row), then:

by(DF, DF[,3], function(x) plot(x[,1], x[,2], main=unique(x[,3])))

And that's it.

If you'd like all the four plots to be on the same page, you can first change the graphing paramter option:

par(mfrow=c(2,2))

And back to default par(mfrow=c(1,1) when you're done.

Upvotes: 6

Drew Steen
Drew Steen

Reputation: 16607

I'm quite fond of the ggplot2 package, which does the same thing that user1717913 suggests, but with slightly different syntax (it does a lot of other things very nicely, which is why I like it.)

test <- data.frame(x=rep(1,18),y=rep(1:6,3),type=c("T","T","T","T","T","T","A","B","B","B","A","A","B","B","C","C","C","C"))

require(ggplot2)
ggplot(test, aes(x=x, y=y)) + #define the data that the plot will use, and which variables go where
  geom_point() + #plot it with points
  facet_wrap(~type) #facet it by the type variable

enter image description here

Upvotes: 4

forestman
forestman

Reputation: 189

R is really cool in that there's a bazillion (that's a technical term) different ways to do most things. The way I would do is is to split the data along the groups, and then plot by group.
To do that, the split command is what you want (I'll assume your data is in an object called data):

data.splitted <- split(data, data$type)

Now the data will have this form (let's assume you have 3 types, A, B, and C):

data.splitted  
 L A
 | L x y type
 |   1 4  A
 |   3 6  A
 L B
 | L x y type
 |   3 3  B
 |   2 1  B
 L C
   L x y type
     4 5  C
     5 2  C

and so on. You would reference the "4" in the y column of group A like so:

data.splitted$A$y[1] or data.splitted[[1]][[2]][1] Hopefully seeing them both together makes enough sense.

Now that we have the data split, we're getting closer.
We still need to tell R that we want to plot a bunch of graphs to the same window. Now, this is just one way to go about it. You could also tell it to write each graph to a image file, or a pdf, or whatever you want.
groups <- names(data.splitted) puts your different types into a variable for reference later.

par(mfcol=c(length(groups),1))

Using mfcol fills the graphs in vertically. the mfrow option fills in horizontally. The c() just combines input. The length(groups) returns the total number of groups.
Now we can work on the for-loop.

for(i in 1:length(data.splitted)){   #  This tells it what i is iterating from and to.
                                     #    It can start and stop wherever, or be a 
                                     #    sequence, ascending or descending,
                                     #    the sky is the limit.
tempx <- data.splitted[[i]][[x]]     #  This just saves us
tempy <- data.splitted[[i]][[y]]     #    a bunch of typing.
plot(tempx, tempy, main=groups[i])   #  Plot it and make the title the type.
rm(tempx, tempy)                     #  Remove our temporary variables for the next run through.    
}

So you see, it's not too bad when you break it down into its components. You can do pretty much anything this way. I have a project I'm working on right now, where I'm doing this for 18 lidar metrics that I calculated using another for loop. Commands to read up on:

split, plot, data.frame, "[", 
par(mfrow=___) and par(mfcol=___)

Here's a few helpful links to get you started. The most helpful one of all is built right in to R though. a ? followed by a command will bring up the html help for that command in your browser.
Good luck!

Upvotes: 1

Related Questions