From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: [babel] R questions Date: Fri, 04 Dec 2009 19:45:41 -0500 Message-ID: <87aaxy8czu.fsf@stats.ox.ac.uk> References: <87tyw674n2.fsf@mundaneum.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NGimV-00057c-Tu for emacs-orgmode@gnu.org; Fri, 04 Dec 2009 19:45:51 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NGimR-0004xP-56 for emacs-orgmode@gnu.org; Fri, 04 Dec 2009 19:45:51 -0500 Received: from [199.232.76.173] (port=38790 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NGimR-0004x9-0e for emacs-orgmode@gnu.org; Fri, 04 Dec 2009 19:45:47 -0500 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:43193) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NGimQ-0007F2-HN for emacs-orgmode@gnu.org; Fri, 04 Dec 2009 19:45:46 -0500 In-Reply-To: <87tyw674n2.fsf@mundaneum.com> (=?utf-8?Q?=22S=C3=A9bastien?= Vauban"'s message of "Fri, 04 Dec 2009 23:31:29 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: =?utf-8?Q?S=C3=A9bastien?= Vauban Cc: emacs-orgmode@gnu.org S=C3=A9bastien Vauban writes: > Hello, > > One of this questions is a bit border-line, but I'm still trying=C2=A0;-) > > I have this table generated by a script: > > #+results: abc2008 > | "2008/1" | -78.59 | 1627.24 | > | "2008/2" | -80.17 | 700.33 | > | "2008/3" | -80.17 | 879.8 | > | "2008/4" | -80.17 | -25823.17 | > | "2008/5" | -80.17 | 3570.75 | > | "2008/6" | -81.77 | 2377.8 | > | "2008/7" | -81.77 | 2889.4 | > | "2008/8" | -81.77 | 2612.92 | > | "2008/9" | -81.77 | 1585.21 | > | "2008/10" | -83.4 | 1561.42 | > | "2008/11" | -83.4 | 2189.17 | > | "2008/12" | "" | "" | > > I want to draw the 12 months with the values side by side. > > Problem #1: the "" in the last line hinder the generation of the graph. F= ormat > error. Missing values in R are represented by the value NA. If you change the last line of your table to | "2008/12" | NA | NA | then it works [1], [2], [3]. > > #+srcname: expenses-bar-plot(abc =3D abc2008) > #+begin_src R :results file :file abc2008.pdf > barplot(abc[,3], col =3D "red", main =3D "Profit and Loss 2008", las = =3D 1, xlab > =3D "Months", ylab =3D "EUR") > #+end_src > > Problem #2: I don't know how to ask for drawing the 2 columns. I've tried OK, so one point that is arguably relevant to this mailing list is that when org tables are read into R, the object that is created in R is a *data frame*. Not a matrix. (A data frame can have columns of different types; matrices are all one type). [4] So to solve your problem, you'd need to read the description of the height argument in the help page for barplot (?barplot), noting that it says "either a vector or matrix", and also noting that it says that bars correspond to columns (not rows), thus realising that you need to explicitly convert the relevant columns of the data frame to a matrix and then transpose. However, your two columns have rather different magnitude values and so are not very well suited for plotting on the same scale. Below I rescaled the first column by a factor of 20 so you can at least see the bars. #+srcname: expenses-bar-plot-two-columns(abc =3D abc2008) #+begin_src R :file abc2008.png ## select the two columns, convert to matrix, transpose and rescale top r= ow. x <- t(as.matrix(abc[,2:3])) * c(20,1) barplot(x, col =3D rep(c("red","blue"), ncol(x)), main =3D "Profit and Lo= ss 2008", las =3D 1, xlab=3D "Months", ylab =3D "EUR", beside=3DTRUE) #+end_src Dan Footnotes: [1] Note no quotes around NA here. You asked a good question about quoting in org-babel; it will be answered. [2] I guess one could potentially think about dealing with missing values more explicitly in org-babel. E.g. there could be a header arg specifying what values are to be treatyed as missing. Nothing like that exists currently. [3] You might think that an alternative would be to do something like this in R abc[abc =3D=3D "\"\""] <- NA but the trouble is that with those double quotes present, R will interpret the column as containing character data rather than numeric, and things will not be pretty. [4] org-babel uses orgtbl-to-tsv followed by read.table() to convert the org table into a data.frame in R. A source of much confusion with R-beginners is that by default, read.table converts character columns into the *factor* data type. Note that org-babel currently uses 'as.is=3DTRUE' when calling read.table and therefore does *not* convert to factor. This may avoid some confusion among users but is memory-inefficient and misses out on other advantages of factors.