0

I have a table containing three columns: two types of time (planned time vs really spent time) and a number marking a month (but for each month there is a ton of entries). It looks like that

month planned spent
1        40     30
1        20     20
2        10      NA

etc.

I'm interesting in the percent of spent regarding the planned (and to check if there is a correlation between the quantity of planned time and the percent of spent time). Of course I can count it for each month using:

100*sum(final$spent[final$month == 1], na.rm = T)/sum(final$planned[final$month == 1])

Now I want to build a plot. Planned time is one axis, percent is another one, while the month would be specified with a colour.

I'm trying to do it in lattice with

with(final, xyplot(sum(planned) ~ 100*sum(spent, na.rm = T)/sum(planned), group=month))

but I get only one point on my plot.

I need advice as to how to do it. Thank you.

DenisK
  • 140
  • 1
  • 8
  • this is not a reproducible example (see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And you also have some typos in your script (e.e Month vs month) – MLavoie Jan 09 '16 at 15:55
  • What is `final$fact`? – ytk Jan 09 '16 at 15:58
  • @TejaK , sorry, I edited the post. Now I hope everything is clear – DenisK Jan 09 '16 at 16:10

1 Answers1

1

Hadley Wickham has created several R packages that are very handy for doing this sort of thing. I like plyr for the data summarizing you're talking about and ggplot2 for plotting. Here's how I'd do that with your data.

 final <- data.frame(month = c(1, 1, 2),
               planned = c(40, 20, 10),
               spent = c(30, 20, NA))

 library(plyr)
 summary <- ddply(final, "month", function (x) c(
  sumplanned = sum(x$planned, na.rm = T),
  sumspent = sum(x$spent, na.rm = T),
  percent = sum(x$spent, na.rm = T)/sum(x$planned, na.rm = T)
))

library(ggplot2)
ggplot(summary, aes(x = sumplanned, y = percent, color = as.factor(month))) + 
  geom_point()
ytk
  • 2,787
  • 4
  • 27
  • 42
shirewoman2
  • 1,842
  • 4
  • 19
  • 31
  • thank you so much! But I need actually one more thing: how to give different colours to my 12 points (according to the month they reflect)? – DenisK Jan 09 '16 at 17:31
  • 1
    Edited my answer for that. Just add the "color = month" as noted above. There are lots of resources online for how to use ggplot2. – shirewoman2 Jan 09 '16 at 17:39