Sébastien Vauban
writes:
> Hello,
>
> For a report I'm writing, I've been helped by a colleague of mine (let's call
> him Albert) for the R graphics generation.
>
> Here's an extract of my doc:
>
> #+TBLNAME: investissement-2010-2013
> #+ATTR_LaTeX: align=l
> || \s{Année 2010} | \s{Année 2011} | \s{Année 2012} |
> \s{Année 2013} |
> |++++|
> | RFO| 2596376.30 | 150.00 | 50.00 |
> 50.00 |
> | RFO réseau structurant | 3804467.00 | 6534066.00 | 3804467.00 |
> 0.00 |
> | Équipements| 100.00 | 15.00 | 5.00 |
> 5.00 |
> |++++|
> | Total (HTVA) | 7400843.30 | 8184066.00 | 4354467.00 |
> 55.00 |
> #+TBLFM:
> @5$2=vsum(@-...@-ii);%.2f::@5$3=vsum(@-...@-ii);%.2f::@5$4=vsum(@-...@-ii);%.2f::@5$5=vsum(@-...@-ii);%.2f
>
> whose graphical representation is:
>
> #+srcname: barplot-investment(ptable = investissement-2010-2013)
> #+begin_src R :file 1-01-investissement-2010-2013.png :exports none :session
> source("mcplot.R", local=TRUE)
> ## select the last row only, exclude first column, scale: unit = 1M
> alldata <- as.matrix(ptable[2:4, -1]) / 100
> axisLabels <- c("Année", "Montant HTVA (M€)")
> mcStackedBarplot(alldata, "Investissements", c(2010:2013),
> ptable[-nrow(ptable),1], legend.location="topright")
> #+end_src
>
> That works perfectly for him (on Ubuntu 9.04, R 2.7.1, Emacs 22.2.1, Org 6.35)
>
> Not for me... on Ubuntu 10.04, R 2.10.1, Emacs 23.1.1, Org 7.01, ESS 5.10: I
> get the message
>
> *Error in as.matrix(ptable[2:4, -1])/1e+06 :
> non-numeric argument to binary operator*
Hi Seb,
The problem is that Org has not automatically recognised that you want
the first row to be treated as column names. The reason is that your
table contains multiple horizontal separator lines. So for this table,
you must use ':colnames yes' to tell Org that your first row contains
column names. See sections 14.8.2.12, 14.8.2.13 and 14.8.2.14 in the
manual.
As Erik pointed out, using str is a very good way to investigate your
data structures. Notice below that your column names have become the
first row of the data frame, and as a consequence the columns of the
data frame have been force to character rather than numeric:
--8<---cut here---start->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none
:session :results output
str(ptable)
#+end_src
#+results: barplot-investment
:
: 'data.frame': 5 obs. of 5 variables:
: $ V1: chr "" "RFO" "RFO réseau structurant" "Équipements" ...
: $ V2: chr "\\s{Année 2010}" "2596376.3" "3804467.0" "100.0" ...
: $ V3: chr "\\s{Année 2011}" "150.0" "6534066.0" "15.0" ...
: $ V4: chr "\\s{Année 2012}" "50.0" "3804467.0" "5.0" ...
: $ V5: chr "\\s{Année 2013}" "50.0" "0.0" "5.0" ...
--8<---cut here---end--->8---
However, if we use :colnames yes then we have
--8<---cut here---start->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none
:session :results output :colnames yes
str(ptable)
#+end_src
#+results: barplot-investment
:
: 'data.frame': 4 obs. of 5 variables:
: $ X : chr "RFO" "RFO réseau structurant" "Équipements" "Total
(HTVA)"
: $ X.s.Année.2010.: num 2596376 3804467 100 7400843
: $ X.s.Année.2011.: num 150 6534066 15 8184066
: $ X.s.Année.2012.: num 50 3804467 5 4354467
: $ X.s.Année.2013.: num 50 0 5 55
--8<---cut here---end--->8---
And in this case you probably want ':rownames yes', which will mean you
no longer need the -1 in the indexing expression in your R code.
--8<---cut here---start->8---
#+srcname: barplot-investment(ptable = investissement-2010-2013)
#+begin_src R :XXX-file 1-01-investissement-2010-2013.png :exports none
:session :results output :colnames yes :rownames yes
str(ptable)
options(width=100)
head(ptable)
#+end_src
#+results: barplot-investment
#+begin_example
'data.frame': 4 obs. of 4 variables:
$ X.s.Année.2010.: num 2596376 3804467 100 7400843
$ X.s.Année.2011.: num 150 6534066 15 8184066
$ X.s.Année.2012.: num 50 3804467 5 4354467
$ X.s.Année.2013.: num 50 0 5 55
X.s.Année.2010. X.s.Année.2011. X.s.Année.2012.
X.s.Année.2013.
RFO2596376 150 50
50
RFO réseau structurant 3804467 6534066