5

Dataframe (borrowed from here):

df.test <- data.frame(id = rep(1:6, each = 50), x = rnorm(50*6, mean = 10, sd = 5), 
                 y = rnorm(50*6, mean = 20, sd = 10), 
                 z = rnorm(50*6, mean = 30, sd = 15))

Plot:

library(ggplot2)
ggplot(df.test, aes(x)) + geom_histogram() + facet_wrap(~id)

Request for assistance: I'd like to superimpose onto each of the facets the histogram of the entire data, to provide immediate comparison of each facet with the total dataset, if possible I'd like the whole dataset shown as a freq_poly():

ggplot(df.test, aes(x)) + geom_freqpoly()

enter image description here

SorenK
  • 155
  • 9

2 Answers2

6

You can exclude the facetting variable from the call to geom_freqpoly

ggplot(df.test, aes(x)) + geom_histogram() + facet_wrap(~id) +
  geom_freqpoly(data = df.test[, "x", drop = FALSE], col = "red")

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
3

The following just removes the id column from the data of geom_freqpoly.

ggplot(df.test, aes(x)) + geom_histogram() + facet_wrap(~id) + 
  geom_freqpoly(data=df.test[-1])

This makes it appear in all facets:

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
tdanker
  • 41
  • 2
  • Oddly enough, while this code works beautifully with the test dataset, when applying it to my dataset, the `geom_freqpoly` did not include the full dataset but instead the facets, while, the code by @markus did. – SorenK Apr 30 '19 at 12:50