[R] Add sum line to plot of multiple x values

Loris Bennett loris.bennett at fu-berlin.de
Mon Mar 9 14:55:39 CET 2015


Hi,

Here are my data:

> d
   user files       date
1 alice    18 2013-09-15
2   bob     5 2013-09-15
3 carol    21 2013-09-15
4 alice    22 2013-09-08
5   bob     9 2013-09-08
6 carol    14 2013-09-08
7 alice    26 2013-09-01
8   bob     3 2013-09-01
9 carol    22 2013-09-01

I would like to plot the number of files against date for all users, so
I have:

  library(ggplot2)

  people <- c("alice","bob","carol")
  user <- c(rep(people,3))
  files <- c(18,5,21,22,9,14,26,3,22)
  date <- c(rep("2013-09-15",3),rep("2013-09-08",3),rep("2013-09-01",3))
  d <- data.frame(user=user,files=files,date=date)

  p <- ggplot()
  p <- p + geom_line(data=d,aes(x=date,y=files,group=user,colour=user))

I would now like to add a line to show the total number of files as a
function of date.  I tried

  p <- p + geom_line(data=d,aes(x=date,y=sum(files),group=date),colour='black')

I don't get a black line, but the plot is scaled such that I can see
that sum(file) for all values of 'file', rather than those for each
date, is being used.

I would like to know how to do this correctly, but I would rather be
able to work it out for myself.  However, if I decide, say, that I don't
know exactly what the 'group' argument does, how do I find it out?

?geom_line doesn't have it, although the examples there use it. ?ggplot
doesn't mention it. ?group gives me stuff about formatting text
arguments. ??group only leads me to ?ggplot2::add_group, which also does
not seem to help. 

Am I at fault for trying to learn R in an ad hoc manner, to which the
documentation of R does not lend itself, or am I missing something?

Cheers,

Loris

-- 
This signature is currently under construction.



More information about the R-help mailing list