包括每个facet_wrap中所有数据的图

时间:2019-04-30 11:52:04

标签: r ggplot2 facet-wrap

数据框(从here借来):

abcabcbb
a is repeated 2 times
b is repeated 4 times
c is repeated 2 times
abba
a is repeated 2 times
b is repeated 2 times
uqinntq
q is repeated 2 times
n is repeated 2 times

情节:

<div>

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

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

enter image description here

2 个答案:

答案 0 :(得分:4)

您可以从对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

答案 1 :(得分:3)

以下只是从geom_freqpoly的数据中删除id列。

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

这使它出现在所有方面:

enter image description here