emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Hendy <jw.hendy@gmail.com>
To: Loris Bennett <loris.bennett@fu-berlin.de>
Cc: emacs-orgmode@gnu.org
Subject: Re: Best way of plotting duration (hms) data with R?
Date: Fri, 8 Mar 2013 22:36:00 -0600	[thread overview]
Message-ID: <CA+M2ft9kyWUzCpTt=8-GUPQao7Mu+yruTP61WF9Op2Y3K6mPLg@mail.gmail.com> (raw)
In-Reply-To: <87ip52b6gt.fsf@slate.zedat.fu-berlin.de>

On Fri, Mar 8, 2013 at 3:12 AM, Loris Bennett
<loris.bennett@fu-berlin.de> wrote:
> Hi,
>
> I have some data representing durations in an Org file:
>
> | *Run* | *reference* | *test30*    | *test31*    | *test32*    |
> |-------+-------------+-------------+-------------+-------------+
> | Dur 2 | 00h 00' 32" | 00h 00' 44" | 00h 00' 39" | 00h 01' 05" |
> | Dur 3 | 00h 00' 31" | 00h 00' 41" | 00h 00' 45" | 00h 01' 13" |
> | Dur 4 | 00h 05' 46" | 00h 21' 54" | 00h 40' 10" | 00h 55' 02" |
> | Dur 5 | 00h 03' 51" | 00h 13' 37" | 00h 23' 07" | 00h 28' 54" |
> | Dur 7 | 00h 06' 49" | 00h 28' 48" | 00h 35' 23" | 01h 03' 17" |
> | Dur 8 | 00h 06' 47" | 00h 22' 54" | 00h 47' 42" | 01h 02' 08" |
>
> I would like to plot these using R and I'm not sure what approach to
> take.
>
> One way would be to do some Org/Calc magic to convert all the data to,
> say seconds, and the pass the data to R.  My problem with this is that I
> can't work out how to convert from HMS in Calc, let alone how this would
> look in Org.
>
> The other way would be to pass the data directly to R.  However, dealing
> with durations in R has always seemed very clumsy to me, everything
> being much more geared towards date-times.

Do you need to use the format =HHh MM' SS"=?

If not, I'd ditch the apostrophe and quotation marks. I think you
should be able to do this in R. I have little experience but did some
date/time plotting in R a bit back which led to a StackOverflow
question because I, like you, found it a bit confusing! [1]

In any case, here's what I did with your data:
- M-x replace-string RET ' RET m
- M-x replace-string RET " RET s
- M-x org-table-export RET "~/Desktop/table.csv" RET RET (to accept
the default csv format)

This gave me times in the format (using Unix date symbols; see `man
date` for the complete list) "%Hh %Mm %Ss"

Then I melted your data and then converted it to POSIXct (for whatever
reason, if I converted first, then melted, it got screwed up).
Finally, I plotted it just fine. Here's the code; let me know what you
think or if you have any questions. I wasn't sure how you wanted to
plot it, so this was a guess:

#+begin_src R :session r :results silent

library(ggplot2)
library(reshape2)
library(scales)

# read the data; change the names
data <- read.csv("~/Desktop/table.csv")
names(data) <- c("Run", "reference", "test30", "test31", "test32")

# melt the data and convert the time strings to POSIXct format
data_melted <- melt(data, id.var = "Run")
data_melted$value <- as.POSIXct(data_melted$value, format = "%Hh %Mm %Ss")

# Plot! You can change the `date_format()` string to whatever you prefer.
p <- ggplot(data_melted, aes(x = Run, y = value, colour = variable)) +
geom_point()
p <- p + scale_y_datetime(labels = date_format("%Hh %Mm %Ss"))
p

#+end_src

If you want to play with the granularity, try something like this
(replacing the above plot command):

#+begin_src R
p <- ggplot(data_melted, aes(x = Run, y = value, colour = variable)) +
geom_point()
p <- p + scale_y_datetime(breaks = date_breaks("10 min"),
  minor_breaks = date_breaks("30 sec"),
  labels = date_format("%Hh %Mm %Ss"))
p
#+end_src

See the =scales= package for the various arguments you can pass to the
date_breaks() command. [2]

Hope that helps a little. Syntax is tricky, but I think R can probably
handle whatever you'd like to toss at it. Just might take some getting
used to. In my opinion, it's easier to learn the syntax than try and
force Org to do something odd via calc or go through even another
middle man (Org -> something -> R) to convert to something like
seconds. Plus, Hadley's added the stuff above (ggplot's scale_datetime
and his scales package) to give you really nice format over the axis
labels which I think you'd have trouble with if converting to pure
seconds.

Let me know if you need more help! It's a learning exercise for me, too :)


Best regards,
John

[1] The question:
http://stackoverflow.com/questions/10770698/understanding-dates-and-plotting-a-histogram-with-ggplot2-in-r
. You'll note it's still not completely answered as I never did figure
out why the two answer-ers solutions didn't produce the same result!

[2] Skip to the date_breaks() section:
http://cran.r-project.org/web/packages/scales/scales.pdf

>
> I'd be grateful for any pointers on this.
>
> Cheers
>
> Loris
>
> --
> no sig is good sig
>
>
>
>
>

      reply	other threads:[~2013-03-09  4:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-08  9:12 Best way of plotting duration (hms) data with R? Loris Bennett
2013-03-09  4:36 ` John Hendy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+M2ft9kyWUzCpTt=8-GUPQao7Mu+yruTP61WF9Op2Y3K6mPLg@mail.gmail.com' \
    --to=jw.hendy@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=loris.bennett@fu-berlin.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).