0

So I have a data.frame:

Plan  Person X       mm  mean_mm
1  1 95 0.323000 0.400303
1  2 275 0.341818 0.400303
1  3  2 0.618000 0.400303
1  4 75 0.320000 0.400303
1  5 13 0.399000 0.400303
1  6 20 0.400000 0.400303
2  1 219 0.393000 0.353350
2  2 50 0.060000 0.353350
2  3 213 0.390000 0.353350
2  4 204 0.496100 0.353350
2  5 19 0.393000 0.353350
2  6 201 0.388000 0.353350

etc to Plan == 40

I want to use ggplot2 to plot points mm (colored by X) but have a plot a line for mm_mean from person 1 to person 6 in each Plan. I've used the code to plot mm (sorted by plan):

mc.points <- ggplot(sam,aes(x = person,y = mm, colour = X, size = 3)) +
geom_point() +
labs(x = "Plan/Person",y = "mm") +
scale_colour_gradient2(high="red", mid="green", limits=c(0,1), guide = "colourbar") 

Which is successful. But not sure if adding geom_line() to plot the mm_mean line will be enough given I haven't specified mm_mean in the above code (although it can be plotted against the y axis given its the mean of this). And if so, how can this be done? And will this work if I want to keep adding variables on top of this which are not related to mm or mm_mean?

I can provide more info if needed

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
user2726449
  • 607
  • 4
  • 11
  • 23

2 Answers2

1

Something like this?

ggplot(df, aes(x = Person,y = mm, colour = X)) +
  geom_point(size = 3) +
  geom_hline(aes(yintercept = mean_mm)) +
  facet_wrap(~ Plan)

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • This looks like it would work but I'm getting error: `Error in geom_point() + geom_hline(aes(yintercept = mm_mean)) : non-numeric argument to binary operator` Not sure whether this has to do with the fact `Plan` is not numeric (I do have some letters in this variable ie 3a or 5c etc)? – user2726449 Apr 05 '14 at 20:24
  • If you have strings in a variable, R will treat it is a character, (possibly further converted to factor). You will need to remove these in order for the plotting to work. – Henrik Apr 05 '14 at 20:27
  • Thanks... figured that one out. I've come across another problem though... Every time a new 'Plan' is plotted, the x axis seems to be an accumulated x-axis for all 40 Plans (not a unique separate x-axis for each specific plan, like above). Not sure whether this is because each plan might have a different number of people in it (not all have 1 to 6). – user2726449 Apr 05 '14 at 21:08
  • For an independent scale in each facet, you can use `scales = "free_x"` in `facet_wrap` – Henrik Apr 05 '14 at 21:19
  • Thanks!!! I'm going to ask another stupid question - its to do with layout of all these 40 plots...at the moment its plotted as 5 across and 8 down, but I was wondering how to arrange it as 4 across and 10 down? – user2726449 Apr 05 '14 at 21:55
  • Please read about `nrow` and `ncol` arguments in [**the help text**](http://docs.ggplot2.org/current/facet_wrap.html). – Henrik Apr 05 '14 at 22:00
  • Is is possible to let me know where I can get help on how to manipulate the scale breaks and labels for the independent scales? – user2726449 Apr 05 '14 at 22:36
  • You find the output of all `ggplot` help texts [**here**](http://docs.ggplot2.org/current/) and a good tutorial [**here**](http://www.cookbook-r.com/Graphs/). See for example the chapter on axes. Please also **search SO**. There are tons of Q&A on this topic. – Henrik Apr 05 '14 at 22:40
  • Will continue to look - none have helped so far (my reason for asking again). Thanks. – user2726449 Apr 06 '14 at 01:19
1

to change the layout of the faceted plots,

facet_wrap(~ Plan, ncol = 4) # or however many columns you want so that the layout looks appealing.

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • Thanks! Yep - seem to have worked that one out - now onto solving the problem of manipulating the x axis breaks of all separate x axes (after using scales = free) - a somewhat challenging and frustrating ggplot2 problem :) – user2726449 Apr 05 '14 at 23:22