5

I have some time series data that is facet wrapped by a variable 'treatment'. One of the levels of this 'treatment' factor the a negative control & I want to include it in every facet.

For example using R dataset 'Theoph':

data("Theoph")


head(Theoph)

Subject   Wt Dose Time  conc
1       1 79.6 4.02 0.00  0.74
2       1 79.6 4.02 0.25  2.84
3       1 79.6 4.02 0.57  6.57
4       1 79.6 4.02 1.12 10.50
5       1 79.6 4.02 2.02  9.66
6       1 79.6 4.02 3.82  8.58

Theoph$Subject <- factor(Theoph$Subject, levels = unique(Theoph$Subject)) # set factor order

ggplot(Theoph, aes(x=Time, y=conc, colour=Subject)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ Subject)

How could I include the data corresponding to Subject '1' (the control) to be included in each facet? (And ideally removing the facet that contains Subject 1's data alone.)

Thank you!

markus
  • 25,843
  • 5
  • 39
  • 58
Cam
  • 87
  • 5
  • Seems I cant edit my own question. Should say "And ideally removing the facet that contains Subject 1's data alone." – Cam Jun 29 '18 at 14:59
  • 2
    You might find these posts helpful [Merge and edit multiple legends when facets and geom_line are plotted separately](https://stackoverflow.com/questions/47327997/merge-and-edit-multiple-legends-when-facets-and-geom-line-are-plotted-separately) or [ggplot2: create a plot using selected facets with part data](https://stackoverflow.com/questions/48467070/ggplot2-create-a-plot-using-selected-facets-with-part-data) and [ggplot2:: Facetting plot with the same reference plot in all panels](https://stackoverflow.com/questions/48464000/ggplot2-facetting-plot-with-the-same-reference-plot-in-all-panels) – markus Jun 29 '18 at 15:12
  • You can certainly edit your own question. There's an "edit" link at the bottom left, right below the tags in the blue boxes. `share edit close flag`. – Gregor Thomas Jun 29 '18 at 15:16
  • Thanks for those links Markus, super helpful - I probably wasn't searching with quite the right words & didnt manage to find those – Cam Jun 29 '18 at 16:33
  • There's some nice info in this blog post on plotting "background data" on all facets that you might find worth the read: https://drsimonj.svbtle.com/plotting-background-data-for-groups-with-ggplot2 – aosmith Jun 29 '18 at 16:42

1 Answers1

4

To have a certain subject appear in every facet, we need to replicate it's data for every facet. We'll create a new column called facet, replicate the Subject 1 data for each other value of Subject, and for Subject != 1, set facet equal to Subject.

every_facet_data = subset(Theoph, Subject == 1)
individual_facet_data = subset(Theoph, Subject != 1)
individual_facet_data$facet = individual_facet_data$Subject

every_facet_data = merge(every_facet_data,
                                                 data.frame(Subject = 1, facet = unique(individual_facet_data$facet)))

plot_data = rbind(every_facet_data, individual_facet_data)

library(ggplot2)
ggplot(plot_data, aes(x=Time, y=conc, colour=Subject)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ facet)

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • This is exactly what I was looking for but I just couldn't get there! Thanks a million – Cam Jun 29 '18 at 16:30