我如何ggplot一个网格,我的数据是我的数据框中的列表?

时间:2017-12-11 20:44:07

标签: r dataframe ggplot2 data-visualization

我有时间相关的数据,由列a,b,c参数化,存储为数据框中的列表。

对于a,b,c的给定组合,有一个数据集。所有列表的长度相同,并且与0,1,2,3...

的时间值对齐

我想要一个图表网格,其中每个图表都由其参数a,b,c“命名”

# My Data

| a | b | c | data             |
|---+---+---+------------------|
| 0 | 0 | 0 | [1,2,3,4,5, ...] |
| 0 | 0 | 1 | [1,2,3,4,5, ...] |
| 0 | 1 | 0 | [1,2,3,4,5, ...] |
| 0 | 1 | 1 | [1,2,3,4,5, ...] |
| 1 | 0 | 0 | [1,2,3,4,5, ...] |
|   |   |   | ...              |

我希望能够如何绘制阴谋的一个例子:

# Grid of plots of x columns, where x=3

|-----+-----+-----|
| 000 | 001 | 010 |
|-----+-----+-----|
| 011 | 100 | 101 |
|-----+-----+-----|
| ... |     |     |
|-----+-----+-----|

我玩过reshape2melt以及各种ggplot函数。好像我可能需要facet s?我特别难以按摩我的数据来完成这项工作。

也许我需要先像这样构建它?:

| time | a | b | c | value |
|------+---+---+---+-------|
|    0 | 0 | 0 | 0 | 1     |
|    0 | 0 | 0 | 1 | 1     |
|    0 | 0 | 1 | 0 | 1     |
|  ... |   |   |   |       |
|    1 |   |   |   |       |
|  ... |   |   |   |       |
|    2 |   |   |   |       |
|  ... |   |   |   |       |

然后绘制图表会更清楚吗?

但尽管我尽最大努力与unlist和其他无数选项一起玩,但我绝望地失去了!更不用说,我有很多数据,需要组织它,每个时间步骤重复参数似乎非常低效。我尝试在数据帧的每一行上映射一个绘图函数,以避免这种情况,但是没有去。它只是闪过每个可能的情节,没有网格。

原谅转储,这里有一些数据:

以下试用参数(例如“a,b,c”)的名称为mu, sigma, alpha,数据位于voltsgates。所以在我的真实数据中,他们是每次试验的2个信息列表,我的moonshot希望是将它们叠加在每个试验的基础上。

dput(json[c(10,12,14),])
structure(list(volts = list(c(0, 8, 0.123076923076923, 6.36062485427839, 
-1.35041613841627, 0.0813353361154996, 5.45210004451402, -2.25040704365856, 
-2.2274879747374, -1.77720113538116, -0.772080385713428, 2.65398304980035, 
-1.21693712810785, 0.508819232569825, 3.8702170600239, -2.0805575610709, 
-1.81527038239188, -1.05872174504971, 1.19563653118797, 0.917590667516922, 
1.74079427542903, -0.791785769908641, 1.80978404997858, -1.08582371173704, 
0.370498391076412, 3.93981331973971, -2.68077747282966, -2.93633061286096, 
-2.57276864441069, -1.79630309201414, -0.464737121637317, 4.56288543429376, 
-1.5612294856231, -0.467500079936414, 4.06696433617447, -1.95326133023706, 
-1.52068446504636, -0.395852055402207, 4.39420405288322, -2.05290467145721, 
-1.7475091289631, -0.918202245425282, 1.78088590450835, -0.467429576327802, 
3.84195282652024, -2.12714171638979, -1.91667352153984, -1.24882180652216, 
0.529159505141398, 3.89084568276239, -1.96394337438887, -1.5518252236995, 
-0.478451711191505, 3.97889041640898, -1.96470833835622, -1.54993103341433, 
-0.471340187762272, 4.01756598292737, -1.97193706273154, -1.56551444656163, 
-0.509049074287182, 3.82370801555761, -1.92102163835374, -1.45402664348141, 
-0.225788160940276, 5.09353449263753, -2.17859568668085, -2.05107307654614, 
-1.49292254435967, -0.161954164255822, 5.43954389336899, -2.06367966259611, 
-1.83746082603709, -1.13858489054562, 0.866239396251801, 2.1692802386062, 
-1.16367788164508, 0.420330683419794, 4.04197191117256, -2.37526640499736, 
-2.40019127972549, -1.97009577528976, -1.05841539836617, 1.45013967095377, 
0.456315513273358, 4.22384952145179, -2.05953197004534, -1.76044085738998, 
-0.943737204269714, 1.67049498681116, -0.271056813751855, 4.75349418278906, 
-2.30841505837324, -2.2865570034278, -1.82757402764794, -0.834272409070842, 
2.38661213898463, -0.977070314306571, 1.46748552994815, 0.0972116049958243
), c(0, 1, 0.5, 0.42, 0.280051003060183, 0.197675141896088, 0.126374628547629, 
0.0731445671498873, 0.0355200499050424, 0.0117866864216197, -0.000589755091410904, 
-0.00493013706351775, -0.0047299879785202, -0.00285460251944436, 
-0.00104298349100251, 4.88263421497548e-05, 0.00044624549809813, 
0.000427494737087808, 0.000257937831098842, 9.56060508020551e-05, 
-2.35293361228184e-06, -3.8674097936342e-05, -3.77814733132364e-05, 
-2.30852478513288e-05, -8.72739348123375e-06, 4.54574635710969e-08, 
3.36194315175842e-06, 3.34465801488104e-06, 2.06711973316231e-06, 
7.96156601312603e-07, 1.06547417555447e-08, -2.91884133085674e-07, 
-2.95933020089123e-07, -1.85017051679282e-07, -7.25624507147415e-08, 
-2.25594209979363e-09, 2.5317794527524e-08, 2.61750519037207e-08, 
1.65542899388527e-08, 6.60777066396889e-09, 3.17140758099299e-10, 
-2.19381202093416e-09, -2.3143256200342e-09, -1.48067713645617e-09, 
-6.01233507424581e-10, -3.85762533028355e-11, 1.89892435109584e-10, 
2.04551375837525e-10, 1.32392208307408e-10, 5.46627187958393e-11, 
4.35362856876509e-12, -1.64182001327617e-11, -1.80726544840581e-11, 
-1.18336451748746e-11, -4.96602758914833e-12, -4.69180250206591e-13, 
1.41786582474879e-12, 1.59616764250359e-12, 1.057376408653e-12, 
4.50861570300276e-13, 4.90718576884319e-14, -1.22346577313692e-13, 
-1.40998324127395e-13, -9.45910016980633e-14, -4.10782519111308e-14, 
-5.10702591327572e-15, 1.0547118733939e-14, 1.24344978758018e-14, 
8.43769498715119e-15, 3.66373598126302e-15, 4.44089209850063e-16, 
-8.88178419700125e-16, -1.11022302462516e-15, -6.66133814775094e-16, 
-2.22044604925031e-16, 0, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16, 1.11022302462516e-16, 1.11022302462516e-16, 
1.11022302462516e-16), c(0, 4, 0.235294117647059, 2.27016393442623, 
-0.959390442049972, -0.389207223858196, 1.36627773340817, -0.564281559829579, 
0.555156619027497, 0.793273591601728, -0.0202573359756344, 1.22159026191887, 
-1.16411686313992, -1.53488289463002, -1.59896819339892, -1.08301734294731, 
0.240787671180565, 2.59229337056637, -0.761864660687435, 0.265872467223762, 
1.76035171147165, -1.10070853387346, -0.936849856654988, -0.196979485193142, 
1.87933714408406, -1.01377366189142, -0.637909835291037, 0.617753552026078, 
0.912240739602508, -0.0344709942702068, 1.43094233384997, -1.23870138971192, 
-1.51667484068756, -1.41225280574457, -0.712125442195221, 1.14279364394911, 
0.493959726503923, 1.54051957482802, -0.676811573217234, 0.295318729624464, 
1.48829331938259, -1.0588977731662, -0.982942888178784, -0.431824337231788, 
1.27862766894301, -0.410527238091886, 1.00860015549051, -0.275622431971082, 
1.07581948913433, -0.682933316985524, -0.218066458898729, 1.13209826215213, 
-0.850339136967601, -0.712227504324583, -0.0567195241330971, 
1.54729172576195, -1.23981610132727, -1.42972267138324, -1.22115414745618, 
-0.386233179324656, 1.95291944789325, -0.550138562942412, 0.947481867338949, 
0.193667032925784, 1.5812294855154, -1.20499642123226, -1.3173166886067, 
-1.02837851221032, -0.046100182482482, 2.39215277257968, -0.986820240179852, 
-0.46433112197797, 1.17467428408266, -0.258655446522899, 1.36336754694863, 
-0.888311342008806, -0.56982894518041, 0.551504807576868, 0.815613753858194, 
-0.0589955088247516, 1.2151210212745, -1.13341682882005, -1.45950537111061, 
-1.50174307449222, -0.996231676485811, 0.353190571632303, 2.28057391599714, 
-0.76494921548438, 0.246802047249248, 1.78440161126351, -1.1237242361284, 
-0.990043775248635, -0.310772651461705, 1.69313828854545, -0.80200475824486, 
-0.0456091758966335, 1.81659176346415, -1.22754189585187, -1.25245592177562, 
-0.824371422219647)), mu = c(0.19, 0.38, 0.38), sigma = c(0L, 
0L, 0L), alpha = c(8L, 1L, 4L), gates = list(c(0, 0, -1.52, -1.54338461538462, 
-2.75190333769751, -2.49532427139842, -2.51077798526036, -3.54667699371803, 
-3.1190996554229, -2.69587694022279, -2.35820872450037, -2.21151345121482, 
-2.71577023067689, -2.4845521763364, -2.58122783052467, -3.31656907192921, 
-2.92126313532574, -2.57636176267128, -2.37520463111183, -2.60237557203755, 
-2.77671779886576, -3.10746871119728, -2.95702941491464, -3.30088838441057, 
-3.09458187918053, -3.16497657348505, -3.91354110423559, -3.40419338439796, 
-2.84629056795437, -2.35746452551634, -2.01616693803366, -1.92786688492257, 
-2.79481511743838, -2.49818151516999, -2.40935649998207, -3.18207972385522, 
-2.81096007111018, -2.52203002275137, -2.44681813222495, -3.28171690227277, 
-2.8916650146959, -2.55963828019291, -2.3851798535621, -2.72354817541869, 
-2.63473655591641, -3.36470759295525, -2.96055066684119, -2.59638269774862, 
-2.35910655450941, -2.45964686048628, -3.19890754021113, -2.82575829907725, 
-2.53091150657434, -2.44000568144796, -3.19599486056566, -2.82270027627798, 
-2.52821337992926, -2.43865874425443, -3.20199628101063, -2.82732823909164, 
-2.52988049424493, -2.43316117013036, -3.15966569308631, -2.7946715817991, 
-2.51840651953763, -2.47550676895898, -3.44327832256011, -3.02934514209075, 
-2.63964125754698, -2.35598597411864, -2.32521468291004, -3.35872802265015, 
-2.96662888675689, -2.61751132980984, -2.40118020060617, -2.56576568589401, 
-2.97792893122919, -2.75683013371663, -2.83669296356639, -3.60466762668917, 
-3.15336700973968, -2.69733066659183, -2.32301246928678, -2.1219135435972, 
-2.39744008107842, -2.48414002860036, -3.2866714376762, -2.89536036336758, 
-2.56087660046349, -2.38156653165224, -2.69896057914636, -2.64745978453351, 
-3.55062367926343, -3.11202481817252, -2.67757898752123, -2.33033992226813, 
-2.17182816454467, -2.62528447095175, -2.4396411112335, -2.71846336192365
), c(0, 0, -0.38, -0.57, -0.7296, -0.83601938116287, -0.911135935083383, 
-0.959158293931482, -0.986953229448439, -1.00045084841236, -1.00492978925257, 
-1.00470568231783, -1.0028322302337, -1.00103483480186, -0.999950085844471, 
-0.99955375211789, -0.999572306127907, -0.999741879417184, -0.999904327417278, 
-1.0000023437931, -1.0000386740924, -1.00003777997763, -1.00002308382041, 
-1.00000872686055, -0.999999954466369, -0.999996638056846, -0.999996655330682, 
-0.99999793286908, -0.999999203839126, -0.999999989344624, -1.00000029188413, 
-1.00000029593293, -1.00000018501696, -1.00000007256242, -1.00000000225594, 
-0.999999974682205, -0.999999973824947, -0.999999983445709, -0.999999993392229, 
-0.999999999682859, -1.00000000219381, -1.00000000231433, -1.00000000148068, 
-1.00000000060123, -1.00000000003858, -0.999999999810108, -0.999999999795449, 
-0.999999999867608, -0.999999999945337, -0.999999999995646, -1.00000000001642, 
-1.00000000001807, -1.00000000001183, -1.00000000000497, -1.00000000000047, 
-0.999999999998582, -0.999999999998404, -0.999999999998943, -0.999999999999549, 
-0.999999999999951, -1.00000000000012, -1.00000000000014, -1.00000000000009, 
-1.00000000000004, -1.00000000000001, -0.999999999999989, -0.999999999999988, 
-0.999999999999992, -0.999999999999996, -1, -1, -1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1), c(0, 0, -1.52, -1.60941176470588, 
-2.47207405978785, -2.10750569180886, -1.95960694674275, -2.47879248543785, 
-2.26436549270261, -2.47532500793306, -2.77676897274172, -2.76907118507097, 
-3.23327548460015, -2.79091107660698, -2.20765557664757, -1.60004766315598, 
-1.188501072836, -1.28000038788462, -2.26507186869984, -1.97556329763861, 
-2.07659483518364, -2.74552848554287, -2.32725924267095, -1.97125629714206, 
-1.89640409276866, -2.6105522075206, -2.22531821600187, -1.98291247859127, 
-2.21765882836118, -2.56431030941013, -2.55121133158746, -3.09496941845045, 
-2.62426289035992, -2.04792645089864, -1.51127038471571, -1.24066271668152, 
-1.67492430138218, -1.86262899745368, -2.44802643588832, -2.19083803806577, 
-2.30305915532307, -2.86861061668845, -2.4662294628853, -2.09271116537736, 
-1.92861791722928, -2.41449643142762, -2.25849608095271, -2.6417641400391, 
-2.53702761589009, -2.94583902176114, -2.68632436130664, -2.60345910692512, 
-3.03365644654293, -2.71052757449524, -2.4398811228519, -2.41832770368132, 
-3.00629855947087, -2.5351684409665, -1.99187382584087, -1.52783524980752, 
-1.38106664166415, -2.12317603186359, -1.91412337794547, -2.27416648753427, 
-2.34775996004607, -2.94862716454192, -2.49072852447366, -1.99014818280312, 
-1.59936434816319, -1.58184627881985, -2.49086433240013, -2.11587264113179, 
-1.93942681478016, -2.38580304273157, -2.28751397305287, -2.80559364089335, 
-2.46803533093, -2.25150033176145, -2.46107215864066, -2.77100538510677, 
-2.74858709175336, -3.21033307983767, -2.77963468488606, -2.22502264386402, 
-1.65436027555698, -1.27579223849237, -1.41000465571265, -2.27662274379156, 
-1.9859420419075, -2.07972681986221, -2.75779943214234, -2.33078422241355, 
-1.95456758781907, -1.83647398026362, -2.47986652991089, -2.17510472177785, 
-2.15777323493713, -2.8480781050535, -2.38161218462979, -1.90567893435506
))), .Names = c("volts", "mu", "sigma", "alpha", "gates"), row.names = c(10L, 
12L, 14L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

就我理解的问题而言,这是你如何做到的。我将主要使用“tidyverse”功能提供答案,但当然您也可以使用其他工具。

library(ggplot2)
library(tidyr)
library(dplyr)

df %>% 
  mutate(trial = row_number()) %>% 
  unnest() %>% 
  group_by(trial) %>% 
  mutate(time = row_number()) %>% 
  gather(var, val, volts, gates) %>% 
  ggplot(aes(time, val, color = var)) + 
  geom_line() +
  facet_wrap(~mu+sigma+alpha, ncol = 3) # or       facet_wrap(~trial, ncol = 3)

pic