Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include plot of all data within each facet_wrap

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

like image 795
SorenK Avatar asked Jan 26 '23 09:01

SorenK


2 Answers

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

like image 50
markus Avatar answered Feb 08 '23 07:02

markus


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

like image 20
tdanker Avatar answered Feb 08 '23 07:02

tdanker