r - place a legend for each facet_wrap grid in ggplot2 -
i have data frame:
date server filesystem percentused 1 12/1/2011 / 60 2 1/2/2012 /var 50 3 2/1/2012 tmp 90 4 2/10/2012 /db 86 5 2/13/2012 /app 90 6 12/1/2011 b c: 67 7 1/2/2012 b d: 67 8 2/1/2012 b f: 34 9 2/10/2012 b /restore 89 10 2/13/2012 b g: 56 11 12/1/2011 c / 90 12 1/2/2012 c /tmp 78 13 2/1/2012 c /data 67 14 2/10/2012 c /storage 34 15 2/13/2012 c /database 12 dput(x) structure(list(date = structure(c(2l, 1l, 3l, 4l, 5l, 2l, 1l, 3l, 4l, 5l, 2l, 1l, 3l, 4l, 5l), .label = c("1/2/2012", "12/1/2011", "2/1/2012", "2/10/2012", "2/13/2012"), class = "factor"), server = structure(c(1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l), .label = c("a", "b", "c"), class = "factor"), filesystem = structure(c(1l, 9l, 14l, 5l, 2l, 10l, 11l, 12l, 6l, 13l, 1l, 8l, 3l, 7l, 4l), .label = c("/", "/app", "/data", "/database", "/db", "/restore", "/storage", "/tmp", "/var", "c:", "d:", "f:", "g:", "tmp"), class = "factor"), percentused = c(60l, 50l, 90l, 86l, 90l, 67l, 67l, 34l, 89l, 56l, 90l, 78l, 67l, 34l, 12l)), .names = c("date", "server", "filesystem", "percentused"), class = "data.frame", row.names = c(na, -15l))
i put legend right next each facet_wrap
grid, own filesystem
:
when this, puts legend on side of plot of filesystem
. possible put filesystem
belong each server next each grid?
ggplot(x, aes(date, percentused, group=1, colour=filesystem)) + geom_jitter(size=0.5) + geom_smooth(method="loess", se=t) + facet_wrap(~server, ncol=1)
meh, @joran beat me (my gridextra
out of date took me 10 minutes realize it). here's similar solution, 1 skins cat generically levels in server
.
library(gridextra) out <- by(data = x, indices = x$server, fun = function(m) { m <- droplevels(m) m <- ggplot(m, aes(date, percentused, group=1, colour = filesystem)) + geom_jitter(size=2) + geom_smooth(method="loess", se=t) }) do.call(grid.arrange, out)
Comments
Post a Comment