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

伊希施特

我的问题

理想情况下,我想使用ggplot2创建一个具有6个小平面(角度和组的每个唯一组合为3 x 2,请参见下面的数据)和每个小平面有4条线(并排为2 x 2,请参见下面的数据)的图),其中每行的x和y值为旋转和predvalue。但是,将两者都包含在一个图中会使数据消失。

抱歉,由于信誉不佳,我无法发布图片,因此我现在将它们编辑了出来,以后会尝试插入它们-当然,如果有人对其进行编辑也很好。

我的数据如下所示(请参阅本文末尾的完整数据说明):

> 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使用新的表示法,即:
facet_grid(row = vars(angle), cols = vars(age))
  • 将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)
  • 子集可消除角度和年龄的每个唯一组合内的旋转重复:
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)

我的问题

  • 是否可以直接在ggplot2中执行此操作?
  • 如果是:如何?
  • 如果不是,最好的解决方法是什么?ggplot2中有一个吗?还是我需要切换到par(mfrow)/layout/split.screen?

这是完整的数据集:

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")
乔恩·斯普林

中间代码中有一个小的错字,您想使用的地方就是错了,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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用ggplot2 facet_grid注释没有数据的构面?

ggplot2 facet_wrap在每个构面上绘制点,线段和文本

ggplot2:从图中删除未使用的因子级别组合的构面(facet_grid)

使用facet_wrap和ggplot2在每个构面内绘制整个数据

在ggplot2中使用facet_grid()函数时,如何使用labeller()函数获取列总计显示在构面的标签中

在ggplot2中的特定构面之间添加空间(facet_grid)

在ggplot2中旋转切换的构面标签facet_grid

如何在ggplot2中使用facet_grid制作甜甜圈图?

如何在ggplot2的甘特图R中使用facet_grid

在某些图中添加y = 0线facet_grid ggplot2

ggplot2:在 facet_grid 中按组添加水平线

ggplot,跨构面绘制多条线

ggplot2和facet_grid:为每个图添加最大值

使用ggplot2绘制多条曲线

在ggplot2中使用facet_grid()时如何定义常见的y轴限制

如何將 ggplot2 geom_col 與 facet_grid 和 coord_flip 一起使用

r-如何在ggplot2中使用facet_grid获得长标签以适合?

ggplot2和gtable:跨面板绘制多条线

ggplot2:添加用于facet_grid的变量的名称

ggplot2中的facet_grid错误

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

使用Shiny和ggplot2在单个图形上绘制多条线

如何在ggplot2 :: facet_grid中格式化网格标题和条目

如何在ggplot2中按组为facet_grid着色?

R ggplot2 facet_grid与vars():如何处理缺少的参数?

ggplot2:如何使geom_text()与facet_grid()很好地玩?

ggplot2 facet_grid:如何修复geom_col中列之间的不同间距

使用ggplot2中的facet_grid更改中断次数

ggplot2:使用gtable将标签条移动到facet_grid的面板顶部