我有这段代码:
require(ggplot2)
require(scales)
Dataset = data.frame(Data = rnorm(100), Factor = c(rep("A",10), rep("B",10)))
graf <- ggplot(data = Dataset, aes(x=Data, fill=..x..)) +
geom_histogram(aes(x = Data, y=..density..), binwidth=0.6) +
facet_grid(Factor ~ .) +
geom_density(aes(x = Data, y=..density..), alpha=.2, fill='red', linetype="dotted", size=1.05) +
stat_function(fun = dnorm, arg = list(mean = mean(Dataset$Data), sd = sd(Dataset$Data)), colour = "blue", size=1.07, guide = "Normal") +
scale_fill_gradient(low = "grey25", high = "white")
现在,我想组织图例:删除“x”图例并添加stat_function和geom_density的图例。我怎么能这样做?我尝试使用scale_color_manual,但它不起作用。
谢谢!
编辑:
这样就解决了:
Data = c(rnorm(50), rnorm(50))
Dataset = data.frame(Data = Data, Factor = c(rep("A",50), rep("B",50)), DensNormal = dnorm(Data, mean(Data), sd(Data)))
graf <- ggplot(data = Dataset, aes(Data, fill=..x..)) +
geom_histogram(aes(x = Data, y=..density..), binwidth=0.6) +
facet_grid(Factor ~ .) +
geom_density(aes(x = Data, y=..density.., colour="dens"), alpha=.2, fill='red', linetype="dotted", size=1.05) +
geom_line(aes(y=DensNormal, colour = 'blue'),line = 1, size=1.07) +
scale_fill_gradient(low = "grey25", high = "white") +
scale_colour_manual(values = c('blue','black'),name = 'Density', labels = c('Normal','Nonparametric'))+
guides(fill=FALSE)
现在我只想让传奇的背景在两者上都不是“红色”,因为它只是在非参数线上。任何想法如何?
谢谢!