, , - fit
geom_density(aes(y = ..scaled..)
fit
0
1
:
d$fit_scaled <- (d$fit - min(d$fit)) / (max(d$fit) - min(d$fit))
fit_scaled
..scaled..
:
ggplot(d, aes(x = measured)) +
geom_density(aes(y = ..scaled..)) +
geom_line(aes(y = fit_scaled), color = "blue")

facet_wrap()
:
d$group <- rep(letters[1:2], 500) #fake group
ggplot(d, aes(x = measured)) +
geom_density(aes(y = ..scaled..)) +
geom_line(aes(y = fit_scaled), color = "blue") +
facet_wrap(~ group, scales = "free")

, :
multiplot()
http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
plots <- c(list(...), plotlist)
numPlots = length(plots)
if (is.null(layout)) {
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
for (i in 1:numPlots) {
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
, :
multiplot(
ggplot(d, aes(x = measured)) +
geom_density() +
facet_wrap(~ group, scales = "free"),
ggplot(d, aes(x = measured)) +
geom_line(aes(y = fit), color = "blue") +
facet_wrap(~ group, scales = "free")
)
:

, facet_grid()
facet_wrap()
cols = 2
multiplot()
:
multiplot(
ggplot(d, aes(x = measured)) +
geom_density() +
facet_grid(group ~ ., scales = "free"),
ggplot(d, aes(x = measured)) +
geom_line(aes(y = fit), color = "blue") +
facet_grid(group ~ ., scales = "free"),
cols = 2
)
:
