简体   繁体   English

如何使用facet_grid(ggplot2)在每个构面上绘制多条线

[英]How to plot multiple lines per facet using facet_grid (in ggplot2)

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. 理想情况下,我想使用ggplot2创建一个具有6个小平面(角度和组的每个唯一组合为3 x 2,请参见下面的数据)和每个小平面有4条线(并排为2 x 2,请参见下面的数据)的图),其中每行的x和y值为旋转和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))

lines in single facet 单面线

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)

facets but no lines 多面但无线条

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))

single facet, correct lines 单面,正确的线条

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, ie: 对facet_grid使用新的表示法,即:
facet_grid(row = vars(angle), cols = vars(age))
  • Combining side and view into a single variable and use that for grouping, eg: 将side和view合并为一个变量,并将其用于分组,例如:
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? 是否可以直接在ggplot2中执行此操作?
  • If yes: how? 如果是:如何?
  • If no: what would be the best workaround? 如果不是,最好的解决方法是什么? Is there one within ggplot2? ggplot2中有一个吗? Or do I need to switch to par(mfrow)/layout/split.screen? 还是我需要切换到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")

There's a small typo in the middle code, where you mean to use tmp.fig = tmp.fig +... but are missing the tmp.fig = . 中间代码中有一个小错字,您的意思是使用tmp.fig = tmp.fig +...但缺少tmp.fig = This means the + geom_line() in the 2nd line isn't included in the third line, hence no lines in the plot. 这意味着第二行中的+ 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)) 

Option 1: Add tmp.fig = ... to revise the plot object incrementally 选项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)) 

Option 2: Build plot in one connected stream 选项2:在一条连接的流中构建图

ggplot(sub.y, aes(x = rotation, y = predval)) + 
geom_line(aes(group = view:side, colour = side, linetype = view)) + 
facet_grid(rows = angle ~ age)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM