I have a data frame created with this code:
require(reshape2) foo <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.8),rnorm(3, mean=.9),rnorm(3, mean=1)))) qux <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.3),rnorm(3, mean=.4),rnorm(1, mean=2)))) bar <- data.frame( abs( cbind(rnorm(3,mean=.4),rnorm(3, mean=.3),rnorm(3, mean=.9),rnorm(3, mean=1)))) colnames(foo) <- c("w","x","y","z") colnames(qux) <- c("w","x","y","z") colnames(bar) <- c("w","x","y","z") rownames(foo) <- c("n","q","r") rownames(qux) <- c("n","q","r") rownames(bar) <- c("n","q","r") foo <- cbind(ID=rownames(foo),foo) bar <- cbind(ID=rownames(bar),qux) qux <- cbind(ID=rownames(bar),qux) foo$fn <- "foo" qux$fn <- "qux" bar$fn <- "bar" alldf<-rbind(foo,qux,bar) alldf.m <- melt(alldf)
What I want to do is to create a ggplot
line curve in facet format, so it creates a graph like this:
The actual graph does not contain upward lines - this is just a sketch so that the line separation is clear.
My current code doesn't work:
library(ggplot2) p <- ggplot(data=alldf.m, aes(x=variable)) + geom_line(aes(colour=ID),alpha=0.4) p <- p + facet_wrap( ~ fn) p
What's the best way to do it?
Faceting is the process that split the chart window in several small parts (a grid), and display a similar chart in each section. Each section usually shows the same graph for a specific group of the dataset. The result is usually called small multiple.
The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package. There are two main functions for faceting : facet_grid()
facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner.
Try this:
ggplot(data=alldf.m, aes(x=variable, y = value, colour = ID, group = ID)) + geom_line() + facet_wrap(~fn)
Even it is a ggplot2 is required by the OP , but I think this example is suitable for lattice
also:
library(lattice) xyplot(data=alldf.m, value~variable|fn, type ='b', groups = ID, auto.key = T)
and using latticeExtra
we can get something colse to ggplot2
solution:
p <- xyplot(data=alldf.m, value~variable|fn, type ='b', groups = ID, auto.key = T) update(p , par.settings = ggplot2like())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With