Plotting Lines Of A Table In R
2
2
Entering edit mode
14.5 years ago
Paulo Nuin ★ 3.7k

I have some files that look like this

...
ALBU_RAT    1    0.97    1.37    0.94    0.80    0.67    0.69
ROA1_RAT    1    N/A    0.84    0.67    0.61    0.70    0.79
HNRPD_RAT    1    0.93    1.03    0.84    0.82    0.94    0.54
SYT1_RAT    1    0.98    1.11    0.64    1.06    0.88    0.95
HPRT_RAT    1    0.53    0.66    0.69    0.73    0.93    0.63
IDH3A_RAT    1    0.86    0.79    0.85    0.63    0.87    0.77
...

what I want to accomplish is to plot (as a line or points) each line of this table in a subplot of a larger plot in R.

Thanks for any help

r visualization graphs • 2.1k views
ADD COMMENT
10
Entering edit mode
14.5 years ago

EDIT: sorry, I didn't read your question correctly the first time.. but my former answer is still valid, you just have to change which columns represent the facets and which the x axis labels (I edited the answer below) with the ggplot2 library:

library(ggplot2)
nuins <- read.table("nuin_plot.txt", na.strings='N/A')
names(nuins) <- c("seq", paste("column_", 2:8, sep=''))
nuins.melt <- melt.data.frame(nuins, id="seq")
qplot(variable, value, data=nuins.melt, facets=~seq) + opts(axis.text.x=theme_text(angle=60, hjust=1.2, ))

explanation: for semplicity, we first melt the dataframe into a new df in which all the V2,3,.. columns are merged into a single one. Do an head(nuins.melt) to see how it looks like.

Then, we use ggplot's basic command qplot to create a facetted plot for every variable; the 'opts' command is just to make the x labels easier to read.

I know it could be seem strange to melt the data frame into another one with only two columns, but that way you can use ggplot2's functions (or even lattice) easier. You reduce the problem to a prototype that you can solve easily.

ggplot2 output: ggplot2 output

http://yfrog.com/5jnuinp


This is another solution, with lattice (remember to melt the data first):

xyplot(value~variable|seq, data=nuins.melt, as.table=T, layout=c(2,3,1))

In this case I recommend you to use the lattice solution, because the xyplot has a parameter called 'layout' in which you can define in how many rows, columns and pages you want your plot to be drawn. for example, if you want the former figure to have 2 columns and 2 rows, and to be splitted into 2 pages, you can pass 'layout=c(2,2,2)' as parameter to the xyplot call.

lattice output http://yfrog.com/ginuinlatticep

ADD COMMENT
2
Entering edit mode

Thanks, great solutions.

ADD REPLY
0
Entering edit mode

Really nice answer. Just a little extra detail: the facet_grid() and facet_wrap() functions in ggplot2 are the rough equivalents of the layout argument in lattice. Though I don't think they can split into pages...

ADD REPLY
0
Entering edit mode

@Matt: thank you for the comment. Actually, I proposed to ggplot2's author (Hadley Wickham) to implement a parameter to split plots into multiple pages. If you want, you can vote the report, but it should be already in his priority list. http://groups.google.com/group/ggplot2/browse_thread/thread/cec806495cab9dd/a2fe740bb55f4bf2

ADD REPLY
1
Entering edit mode
14.5 years ago

I'm not entirely clear on how you want it laid out, but here are two different examples. One with multiple lines all in the same plot:

> df = read.table("yourfile")
> df

  a b c d
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

> plot(1:4,df[1,],ylim=c(1,5))
> for(i in 2:length(df[,1])){
>   points(1:4,df[i,])
>   lines(1:4,df[i,])
> }

Or if you want each one in it's own plot:

> par(mfcol=c(2,2),mar=(1,1,1,1))
> for(i in 2:length(df[,1])){
>   plot(1:4,df[i,],ylim=c(1,10))
>   lines(1:4,df[i,])
> }
ADD COMMENT

Login before adding your answer.

Traffic: 2348 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6