我的问题
理想情况下,我想使用ggplot2创建一个具有6个小平面(角度和组的每个唯一组合-3 x 2,请参见下面的数据)和每个小平面4条线(并排为2 x 2, (请参见下面的数据),其中每行的x和y值分别是rotation和predvalue。但是,将两者都包含在一个图形中会使数据消失。
抱歉,由于信誉不好,我无法发布图片,因此我已经将它们编辑了出来,以后会尝试插入它们-当然,如果有人对其进行编辑也很好。 em>
我的数据如下所示(请参阅本文末尾的完整数据说明):
> 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
我可以在单个面上生成(荒谬的,因为它们包含重复的旋转)线条:
tmp.fig = ggplot(sub.y, aes(x = rotation, y = predval))
tmp.fig + geom_line(aes(group = view:side, colour = side, linetype = view))
我也可以创建构面,但是如果将其添加到图中,所有线条都会消失:
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)
如果我通过将每次交互减少到一个单一因素(不包括图像)简化绘图,也会发生同样的情况:
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))
我最接近期望结果的是按角度和年龄划分子集,并产生一个单一方面:
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))
我尝试过的其他事情
facet_grid(row = vars(angle), cols = vars(age))
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)
sub.lpalm = subset(sub.y,comb == "lpalm")
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)
我的问题
这是完整的数据集:
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, 4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L, 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")
答案 0 :(得分:0)
中间代码中有一个小的错字,您的意思是使用tmp.fig = tmp.fig +...
但缺少tmp.fig =
。这意味着第二行中的+ geom_line()
不包含在第三行中,因此图中没有任何行。
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))
选项1:添加tmp.fig = ...
以逐步修改绘图对象
sub.y2 = droplevels(subset(sub.y,age == "young" & side == "r"))
tmp.fig = ggplot(sub.y2, aes(x = rotation, y = predval))
tmp.fig = tmp.fig + geom_line(aes(group = view))
tmp.fig = tmp.fig + facet_grid(rows = vars(angle))
选项2:在一条连接的流中构建图
ggplot(sub.y, aes(x = rotation, y = predval)) +
geom_line(aes(group = view:side, colour = side, linetype = view)) +
facet_grid(rows = angle ~ age)