My problem
Ideally, I want to use ggplot2 to create a plot that has 6 facets (3 by 2, for each unique combination of angle and group - see data below) and four lines per facet (2 x 2 for side by view, see data below), where the x- and y-values for each line are rotation and predvalue. However, including both in a single figure makes the data disappear.
my apologies, I cannot post images due to lack of reputation, so I've edited them out for now and will try to insert them later - of course it's also fine if someone else edits them in.
My data look like this (see dput of full data at the end of this post):
> head(sub.y)
side view rotation angle age predval
1706 l back 120 0 old 1.322787
1694 l back 120 300 old 1.847914
1739 l back 120 60 old 1.332836
1744 l back 240 0 old 1.157399
1725 l back 240 300 old 1.540411
1713 l back 240 60 old 1.165085
I can produce (nonsensical, because they contain repeats of rotation) lines in a single facet:
tmp.fig = ggplot(sub.y, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(group = view:side, colour = side, linetype = view))
I can also create facets, but if I add them to the plot, all lines disappear:
tmp.fig = ggplot(sub.y, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(group = view:side, colour = side, linetype = view))
tmp.fig + facet_grid(rows = angle ~ age)
The same happens if I simplify the plot by reducing each interaction to a single factor (image not included):
sub.y2 = droplevels(subset(sub.y,age == "young" & side == "r"))
tmp.fig = ggplot(sub.y2, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(group = view))
tmp.fig + facet_grid(rows = vars(angle))
The closest I have come to the desired outcome is to subset by angle and age, and produce a single facet:
sub.single = subset(sub.y, angle =="60" & age == "young")
tmp.fig = ggplot(sub.single, aes(x = rotation, y = predval, group = comb, colour = s))
tmp.fig + geom_line(aes(group = comb, colour = side, linetype = view))
Other things I have tried
- The same with rotation as either a numerical variable or an unordered factor.
- Using the new notation for facet_grid, i.e.:
facet_grid(row = vars(angle), cols = vars(age))
- Combining side and view into a single variable and use that for grouping, e.g.:
sub.y$comb = factor(paste0(sub.y$side, sub.y$view))
tmp.fig = ggplot(sub.y, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(group = view))
tmp.fig + facet_grid(rows = angle ~ age)
- Subsetting to eliminate repeats in rotation within each unique combination of angle and age:
sub.lpalm = subset(sub.y,comb == "lpalm")
- Also in combination with specifying fixed values for colour and linetype:
tmp.fig = ggplot(sub.lpalm, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(x = rotation, y = predval), colour = "red", linetype = 2)
tmp.fig + facet_grid(rows = angle ~ age)
My questions
- Is it possible to do this directly in ggplot2?
- If yes: how?
- If no: what would be the best workaround? Is there one within ggplot2? Or do I need to switch to par(mfrow)/layout/split.screen?
This is the complete dataset:
sub.y = structure(list(side = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("l", "r"), class = "factor"),
view = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("back",
"palm"), class = "factor"), rotation = structure(c(2L, 2L,
2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,
4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,
4L, 1L, 1L, 1L), .Label = c("60", "120", "240", "300"), class = c("ordered",
"factor")), angle = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0",
"300", "60"), class = "factor"), age = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("old", "young"), class = "factor"),
predval = c(1.32278735306685, 1.84791387068062, 1.33283630791921,
1.15739906981041, 1.54041056424184, 1.16508497061474, 1.07521745696964,
1.39817944551224, 1.08184750079453, 1.12479365463523, 1.48318811113428,
1.1320512540569, 1.39380248667788, 1.26377942966172, 1.30745255320685,
1.69059385166861, 1.50302783162468, 1.56520862954539, 1.54867304027266,
1.38979681736988, 1.44279652008074, 1.43351747463353, 1.29634372314341,
1.34233748386223, 1.12608718760191, 1.48543811583713, 1.13336154334054,
1.21240995016786, 1.63941177663727, 1.22084649460943, 1.07662100847547,
1.40055372523695, 1.08326842636178, 0.994888143137882, 1.26532710222997,
1.00056192025627, 1.77947866922719, 1.57287633657913, 1.64110190502596,
1.30305741118218, 1.18871937048205, 1.2272796256123, 1.32008919689553,
1.20287711129925, 1.24237660794742, 1.6083792173394, 1.43769159033688,
1.4944817898592, 1.21942340929136, 1.65226153894706, 1.22795818578818,
1.0774857498948, 1.40201746905525, 1.08414388349372, 1.00591000333728,
1.28320933888409, 1.01171055708072, 1.04917234385345, 1.35445633709304,
1.05548413456299, 1.27952165685907, 1.16910164994662, 1.20637971253239,
1.52534725655376, 1.37098239147552, 1.42253022462099, 1.40885870946257,
1.27614512965268, 1.32069215403839, 1.31291299288743, 1.19691581894265,
1.23601841416658, 1.05029770489711, 1.35633247083872, 1.05662308396455,
1.12500641475433, 1.48355807827798, 1.13226676891647, 1.00713834011054,
1.28520892651887, 1.01295310979221, 0.935262602075179, 1.17042599521691,
0.94027496475633, 1.59733529275344, 1.4288609015962, 1.48494199557954,
1.20263686648971, 1.10457968208415, 1.13779812715232, 1.21713008591452,
1.11679383676805, 1.15076228140728, 1.4580995773513, 1.31641344398338,
1.36386842665239)), row.names = c(1706L, 1694L, 1739L, 1744L,
1725L, 1713L, 1733L, 1720L, 1729L, 1728L, 1730L, 1708L, 1761L,
1763L, 1742L, 1804L, 1787L, 1712L, 1778L, 1722L, 1709L, 1714L,
1747L, 1699L, 1751L, 1737L, 1779L, 1726L, 1935L, 1781L, 1719L,
1905L, 1723L, 1735L, 1760L, 1762L, 1780L, 1836L, 1773L, 1767L,
1768L, 1785L, 1753L, 1701L, 1695L, 1774L, 1783L, 1765L, 5621L,
5618L, 5592L, 5598L, 5596L, 5557L, 5612L, 5565L, 5532L, 5540L,
5562L, 5578L, 5539L, 5567L, 5544L, 5560L, 5704L, 5611L, 5607L,
5599L, 5603L, 5555L, 5561L, 5576L, 5552L, 5604L, 5615L, 5564L,
5693L, 5593L, 5569L, 5556L, 5614L, 5580L, 5610L, 5591L, 5714L,
5589L, 5602L, 5597L, 5590L, 5729L, 5546L, 5617L, 5716L, 5726L,
5720L, 5642L), class = "data.frame")
ggplot(sub.y, aes(x = rotation, y = predval)) + geom_line(aes(group = view:side, colour = side, linetype = view)) + facet_grid(rows = angle ~ age)
– Jon Springtmp.fig + ...
instead oftmp.fig = tmp.fig + ...
. The second change therefore overwrote the first, instead of being added to it. If you post your comment in the form of an answer I'd be happy to accept it. – Ishisht