在每个 facet_wrap 中包含所有数据的图表

include plot of all data within each facet_wrap

Dataframe(借自):

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))

剧情:

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

请求帮助: 我想将整个数据的直方图叠加到每个方面,以提供每个方面与总数据集的即时比较,如果可能的话,我希望整个数据集显示为 freq_poly():

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

您可以从对 geom_freqpoly

的调用中排除分面变量
ggplot(df.test, aes(x)) + geom_histogram() + facet_wrap(~id) +
  geom_freqpoly(data = df.test[, "x", drop = FALSE], col = "red")

下面只是从geom_freqpoly的数据中去掉了id列。

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

这使得它出现在所有方面: