I have the following data frame resulting from simulations of ODEs with different parameter sets, e.g.
df <- data.frame(t = rep(seq(0,4), 4),
x1 = c(1.2*seq(1,5), 1.3*seq(1,5), 1.4*seq(1,5), 1.5*seq(1,5)),
x2 = c(0.2*seq(1,5), 0.3*seq(1,5), 0.4*seq(1,5), 0.5*seq(1,5)),
a = rep(c(rep(1, 5), rep(2,5)), 2),
b = c(rep(1, 10), rep(2,10))
)
I now would like to have a facet_grid
with x1 and x2 on top and a and b on the right where the values of a and b determine the line colour.
I tried
df.1 <- df %>%
gather(x, xval, -t, -a, -b) %>%
gather(p, pval, -t, -x, -xval) %>%
distinct()
df.1$pval <- as.factor(df.1$pval)
ggplot(df.1, aes(t, xval)) +
geom_line(aes(colour = pval)) +
facet_grid(p~x)
and
dm.1 <- melt(df[, c("t", "x1", "x2")], id = 't')
colnames(dm.1) <- c("t", "x", "xval")
dm.2 <- melt(df[, c("t", "a", "b")], id = 't')
colnames(dm.2) <- c("t", "p", "pval")
dm <- merge(dm.1, dm.2)
dm$pval <- as.factor(dm$pval)
ggplot(dm, aes(t, xval)) +
geom_line(aes(colour = pval)) +
facet_grid(p~x)
But both do not give the desired result. Any hint would be greatly appreciated.
Edit: The desired result would be to have two lines in each facet similar to my first solution but the correct ones, i.e. straight lines and not the zig-zag lines that result.