1

I have an org file with this content:

#+name: my-table
| *Type*  | *Value* |
|---------+---------|
| Web     |     744 |
| Checks  |     520 |
| Cash    |     105 |
|---------+---------|
| *Total* |    1369 |
#+TBLFM: @5$2=vsum(@I..@II)

I would like to add an R script to draw a bar chart of the result so I tried:

#+NAME: barchart
#+begin_src R :results output graphics file :colnames yes :session :exports none :var data=my-table[0:-2] :file my-table.png
library(tidyverse)
ggplot(data = data) + geom_bar(mapping = aes(x = X.Type., y = X.Value.), stat = "identity")
#+end_src

This results in:

enter image description here

I like the result except the "X.Value." and "X.Type." to label each axis.

  1. Is what I'm doing the standard way to pass a table with column names to R?
  2. Is the R script written appropriately?
  3. Is there a way to use my column names (i.e., "Type" and "Value") instead of "X.Value." and "X.Type."? Alternatively, can I change the axis label in R?
2
  • These are R questions, much more than emacs/Org mode questions. An R expert might stumble on it here, but you might have better luck asking it on SO.
    – NickD
    Feb 14, 2021 at 22:31
  • 1
    yes and now. The way org-mode passes columns names or not to R is very specific to Org I guess. But I will search R documentation to workaround the issue if no one answers here. Feb 15, 2021 at 7:54

1 Answer 1

1

Your problem comes from an interaction between R and org syntax. The strings *Type* and *Value* are fine for org mode columns, but are invalid object names (or column names) in R. Consequently, when you pass them to R, the leading * is converted to X., and the trailing * is converted to ..

You can fix this by removing the asterixes (asterii?) from your table:

#+name: my-table
| Type    | Value   |
|---------+---------|
| Web     |     744 |
| Checks  |     520 |
| Cash    |     105 |
|---------+---------|
| Total   |    1369 |
#+TBLFM: @5$2=vsum(@I..@II)

And then changing the column names in your aes() directive to match:

#+NAME: barchart
#+begin_src R :results output graphics file :colnames yes :session :exports none :var data=my-table[0:-2] :file my-table.png
library(tidyverse)
ggplot(data = data) + geom_bar(mapping = aes(x = Type, y = Value), stat = "identity")
#+end_src

This produces the following figure:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.