用ggplot在一个tibble内绘制多个时间序列。

3
I've seen similar answers, but not exactly the same. Sorry if I missed something! If possible, I would prefer a ggplot solution.
I have a tibble with four columns and many rows. The first column is called "id" and each row has a unique id. The second column is "doy.series" and the third column is "smooth.series". Each entry in the "doy.series" and "smooth.series" columns are lists. The last column is an integer called "doy".
What I want to do is plot the "doy.series" against the "smooth.series" for each row, and plot all of those as lines on the same plot. I also want the lines to be colored by the "doy" values, with the highest values being red and the lowest values being blue.
I tried using this code: ggplot(df, aes(x = doy.series, y = smooth.series, colour = doy)) + geom_line(), but I received an error message: Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, :unimplemented type 'list' in 'listgreater'.
Here's part of the data:
df =structure(list(id = c("431", "432", "337"), smooth.series = list(
    c(0.43777430245429, 0.426019133975921, 0.415206313498503, 
    0.405280883116371, 0.396187884923862, 0.387872361015312, 
    0.38031119938733, 0.373592663283192, 0.367828857667235, 0.363131887503796, 
    0.359613857757211, 0.356607134781236, 0.353704035469539, 
    0.35140659059632, 0.350216830935782, 0.350636787262128, 0.35316849034956, 
    0.358517061158787, 0.366484449461701, 0.376217929249277, 
    0.386864774512491, 0.39757225924232, 0.409883801658293, 0.425118711223808, 
    0.442088098795645, 0.459603075230582, 0.4764747513854, 0.491514238116876, 
    0.506146464703748, 0.521890696446379, 0.537699548233988, 
    0.552525634955796, 0.565321571501024, 0.576605881716668, 
    0.587480851125326, 0.597774737129616, 0.60731579713216, 0.615932288535578, 
    0.623452468742488, 0.629552270021279, 0.63428577220539, 0.638048454045027, 
    0.641235794290393, 0.644243271691694, 0.647354596944719, 
    0.650361974787858, 0.652923625035218, 0.6546977675009, 0.655342621999009, 
    0.65451640834365, 0.652487512684198, 0.649700033698352, 0.646075759992703, 
    0.64153648017384, 0.636003982848354, 0.629488244830451, 0.622014048803483, 
    0.613525392177759, 0.603966272363588, 0.593280686771281, 
    0.581412632811147, 0.567266510394374, 0.550635047828052, 
    0.532794735116347, 0.515022062263421, 0.498593519273441, 
    0.483883609978582, 0.46991423121304, 0.455932962484007, 0.44118738329868, 
    0.424925073164253, 0.40639361158792, 0.383767843946444, 0.357314491283398, 
    0.329418946606192, 0.302466602922238, 0.278842853238944, 
    0.255899788147355, 0.230850391635965, 0.205871396808358, 
    0.183139536768122, 0.164831544618842, 0.149568001323752, 
    0.134962706326001, 0.121682220449047, 0.110393104516348, 
    0.101761919351363, 0.0981488455530686, 0.100045680674816, 
    0.10531641097162, 0.111825022698496, 0.11743550211046, 0.120011835462528, 
    0.120333320859239, 0.120618157926921, 0.120888685203272, 
    0.121167241225986, 0.12147616453276, 0.121505910782356, 0.121126366388396, 
    0.120629016020871, 0.120305344349771, 0.120446836045087, 
    0.121344975776808, 0.123390133852811, 0.126410803293804, 
    0.129855652839032, 0.133173351227739, 0.135812567199168, 
    0.138057473114274, 0.140495834481054, 0.143031709759322, 
    0.145569157408891, 0.148012235889573, 0.150265003661183, 
    0.152290006630133, 0.154126926530363, 0.155795525562979, 
    0.157315565929083, 0.158706809829782, 0.160250815875207, 
    0.162078771150788, 0.163984880570575, 0.165763349048621, 
    0.167208381498974, 0.168114182835688, 0.16850832706102, 0.168567502729141, 
    0.168309586581966, 0.16775245536141, 0.16691398580939, 0.165203276117212, 
    0.162385787113404, 0.159013939495705, 0.155640153961858, 
    0.152816851209604, 0.151096451936684, 0.150114294258756, 
    0.149209308831738, 0.148489305163764, 0.148062092762967, 
    0.148035481137483, 0.148749174278873, 0.150258581427476, 
    0.152245069964593, 0.154390007271523, 0.156374760729566, 
    0.157880697720023, 0.159221858871935, 0.160811179021899, 
    0.162478366862545, 0.164053131086502, 0.1653651803864, 0.166423570261033, 
    0.167358821255243, 0.168182839630726, 0.168907531649179, 
    0.169544803572298, 0.170106561661781, 0.171006085382971, 
    0.172297499704099, 0.173453979141192, 0.173948698210276, 
    0.17325483142738, 0.171654728251965, 0.169714589410648, 0.16733336632019, 
    0.164410010397352, 0.160843473058894, 0.156569065531064, 
    0.15162033080968, 0.146043567965858, 0.139885076070716, 0.133191154195369
    ), c(0.457739384166388, 0.446283951227014, 0.435458731659352, 
    0.425174728440119, 0.415342944546034, 0.405874382953813, 
    0.396721863270565, 0.388014493526017, 0.379927709330819, 
    0.372636946295622, 0.366317640031077, 0.360178421838646, 
    0.353702781615852, 0.347478442867435, 0.342093129098133, 
    0.338134563812685, 0.336190470515832, 0.336041945239129, 
    0.337140469384323, 0.339579344511437, 0.343451872180496, 
    0.348851353951523, 0.357307167516573, 0.369360100304132, 
    0.383620568478867, 0.398698988205442, 0.413205775648526, 
    0.425751346972783, 0.438198108716855, 0.452357405811074, 
    0.466763062007647, 0.479948901058777, 0.490448746716669, 
    0.499269968838016, 0.508060504095011, 0.516315026463113, 
    0.523528209917781, 0.529194728434472, 0.532809255988646, 
    0.532560415067928, 0.528443894744577, 0.522664968871, 0.517428911299606, 
    0.514940995882802, 0.514237611010666, 0.513096947813341, 
    0.51183698425333, 0.510775698293136, 0.510231067895263, 0.510521071022212, 
    0.513660749308306, 0.519899994047127, 0.526909057690073, 
    0.53235819268854, 0.533917651493925, 0.532181861007955, 0.529401757828773, 
    0.525732358952399, 0.521328681374857, 0.516345742092168, 
    0.510938558100352, 0.503456562618317, 0.493099310232067, 
    0.481296999988271, 0.469479830933597, 0.459078002114715, 
    0.451541771202268, 0.446332623697785, 0.441897600394263, 
    0.436683742084698, 0.429138089562085, 0.41770768361942, 0.402085327505209, 
    0.383720408000677, 0.36369484119791, 0.343090543188989, 0.322989430065998, 
    0.300005231528961, 0.271886288912299, 0.241416435759692, 
    0.211379505614818, 0.184559332021355, 0.163739748522983, 
    0.147630103790859, 0.133129608496548, 0.12030688918434, 0.109230572398524, 
    0.0999692846833883, 0.094199063728592, 0.0926170037645956, 
    0.0938233161468934, 0.0964182122309796, 0.0990019033723484, 
    0.100174600926494, 0.100939656974712, 0.102835148042118, 
    0.105263372149938, 0.1076266273194, 0.109327211571729, 0.110659617793555, 
    0.112276556788702, 0.114117653454184, 0.116122532687017, 
    0.118230819384217, 0.1203821384428, 0.122547432824039, 0.124750983614669, 
    0.127012422899894, 0.129351382764921, 0.131787495294954, 
    0.134376519682738, 0.13714379196334, 0.140063309485449, 0.143109069597754, 
    0.146255069648946, 0.149475306987713, 0.153299187306221, 
    0.15787431971218, 0.162602006677591, 0.166883550674454, 0.170120254174771, 
    0.172788929210748, 0.175585494211257, 0.178239911243302, 
    0.180482142373889, 0.182042149670021, 0.182649895198703, 
    0.181405045617177, 0.178312697326645, 0.174460957010051, 
    0.170937931350339, 0.168831727030453, 0.167152106850496, 
    0.164632203670552, 0.161945179263534, 0.159764195402356, 
    0.158762413859935, 0.159612996409183, 0.162977004175573, 
    0.168327634609843, 0.174556253972805, 0.180554228525275, 
    0.185212924528066, 0.190718693570446, 0.198886756126355, 
    0.208051781783614, 0.216548440130045, 0.222711400753468, 
    0.224875333241705, 0.222735845081612, 0.217800784976475, 
    0.211123184338759, 0.203756074580929, 0.196752487115451, 
    0.187581281663291, 0.174488379633314, 0.159692404969832, 
    0.145411981617157, 0.133865733519603, 0.127272284621481, 
    0.124232380130641, 0.122075598245104, 0.121113812394444, 
    0.121658896008234, 0.124022722516045, 0.127934377121999, 
    0.133041703809836, 0.139534714178564, 0.14760341982719, 0.157437832354721, 
    0.16909197009469, 0.182448635194993, 0.197441014976182, 0.214002296758808, 
    0.232065667863423), c(0.439295372199582, 0.439274974986675, 
    0.439428581777065, 0.439769584911677, 0.440311376731435, 
    0.441067349577262, 0.442050682434608, 0.44324523656069, 0.444620427704081, 
    0.446145671613356, 0.447790384037087, 0.449665408397616, 
    0.451847221910088, 0.45425369101228, 0.456802682141973, 0.459412061736945, 
    0.461999696234976, 0.464831683666793, 0.468057954385313, 
    0.471422123311602, 0.474667805366727, 0.477538615471753, 
    0.480053511602743, 0.482425851243634, 0.484690848577301, 
    0.486883717786618, 0.489039673054462, 0.491193928563707, 
    0.493956982558061, 0.497481497979686, 0.501115936059141, 
    0.504208758026984, 0.506108425113776, 0.507280984619562, 
    0.508369165880557, 0.508986453181571, 0.508746330807411, 
    0.507262283042889, 0.504147794172811, 0.496936291357725, 
    0.485461135551454, 0.47278571172872, 0.461973404864246, 0.456087599932756, 
    0.454930941860552, 0.455688514963464, 0.457497363300498, 
    0.459494530930665, 0.460817061912972, 0.460602000306429, 
    0.459562814204646, 0.459004047420431, 0.45894935759158, 0.459422402355893, 
    0.460446839351168, 0.462009766461302, 0.463937197600506, 
    0.466011164452051, 0.468013698699211, 0.469726832025256, 
    0.470932596113459, 0.47296997490044, 0.476459839069827, 0.480107050425403, 
    0.482616470770949, 0.482692961910247, 0.483734851275993, 
    0.48814977919746, 0.493156576823297, 0.495974075302154, 0.493821105782682, 
    0.483916499413531, 0.462612304326225, 0.431585642670544, 
    0.396042956622516, 0.361190688358171, 0.332235280053538, 
    0.30442706641557, 0.271581322127357, 0.236751899329133, 0.20299265016113, 
    0.173357426763582, 0.15090008127672, 0.13494796346089, 0.122349580442357, 
    0.112440520410242, 0.104556371553664, 0.0980327220617412, 
    0.0957763306533094, 0.0994930336492738, 0.10671703564186, 
    0.114982541223294, 0.1218237549858, 0.124774881521606, 0.124717986794219, 
    0.124302397813276, 0.123713210740194, 0.123135521736389, 
    0.122754426963278, 0.121973514647147, 0.120376930722341, 
    0.118420607912366, 0.116560478940727, 0.115252476530928, 
    0.114952533406475, 0.115616528001641, 0.116810285085568, 
    0.118404656884062, 0.12027049562293, 0.122278653527979, 0.12480637140669, 
    0.128127218628048, 0.131956540260109, 0.136009681370931, 
    0.140001987028574, 0.143648802301094, 0.147492847541756, 
    0.151910365858572, 0.156366986451721, 0.16032833852138, 0.163260051267728, 
    0.165524631797851, 0.167755659620206, 0.16982537253703, 0.171606008350561, 
    0.172969804863034, 0.173788999876686, 0.172946930763641, 
    0.170221200699518, 0.166825446191799, 0.163973303747963, 
    0.162878409875491, 0.163154513456275, 0.163651138819633, 
    0.164435585644181, 0.165575153608539, 0.167137142391325, 
    0.169188851671156, 0.173030459047731, 0.178959544638302, 
    0.185539511897822, 0.191333764281243, 0.194905705243516, 
    0.197368161364743, 0.20034276288299, 0.203156118740162, 0.205134837878162, 
    0.205605529238895, 0.203894801764263, 0.198859358795474, 
    0.190828959110254, 0.181339794805284, 0.171928057977248, 
    0.164129940722826, 0.155917106935194, 0.145306319929596, 
    0.133901420935815, 0.123306251183633, 0.115124651902835, 
    0.110960464323204, 0.109903453921255, 0.110038554987629, 
    0.111462361973299, 0.11427146932924, 0.118562471506423, 0.124211946172795, 
    0.131104334512566, 0.139348026251107, 0.149051411113789, 
    0.160322878825983, 0.173179242302362, 0.187543184114871, 
    0.203381898474785, 0.220662579593383, 0.239352421681939)), 
    doy.series = list(c(39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 
    49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 
    64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 
    79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 
    94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 
    107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 
    119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 
    131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 
    143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 
    155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 
    167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 
    179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
    191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 
    203, 204, 205, 206, 207, 208, 209, 210, 211, 212), c(38, 
    39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 
    54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 
    69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
    84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 
    99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 
    111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 
    123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 
    135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 
    147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 
    159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 
    171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 
    183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 
    195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 
    207, 208, 209, 210, 211, 212), c(38, 39, 40, 41, 42, 43, 
    44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 
    59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 
    74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 
    89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 
    103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 
    115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 
    127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
    139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 
    151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 
    163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 
    175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 
    187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 
    199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 
    211, 212)), doy = c(121, 121, 121)), .Names = c("id", "smooth.series", 
"doy.series", "doy"), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))
1个回答

4

使用的解决方案。关键是您需要将列表列进行unnest处理,以使用ggplot2。此外,您提到要按doy映射颜色,但在您的示例中只有一个doy值。因此,我将您的doy列更改为121:123以进行示例。最后,要绘制渐变色,我们可以使用scale_color_gradient,并指定低和高值为"blue"和"red"。

library(tidyverse)

df2 <- df %>%
  mutate(doy= 121:123) %>%
  unnest() 

ggplot(df2, aes(x = doy.series, y = smooth.series, color = doy, group = doy)) +
  geom_line() +
  scale_color_gradient(low = "blue", high = "red")

enter image description here


1
太棒了!完成了,谢谢。一定要喜欢ggplot和tidyverse的简洁性。 - Ana
出于好奇,对于给定的列,每个项目的列表长度可能略有不同。因此,如果我尝试在完整数据上执行 df2 <- df %>% unnest(),则会出现 Error: All nested columns must have the same number of elements. 我已经尝试了逐个展开一列 df2= df%>% unnest(doy.series) %>% unnest(smooth.series),但仍然遇到相同的错误。有什么解决方法吗? - Ana
@Ana,目前我没有简单的答案。请考虑在SO上搜索,看看是否有关于如何在列表长度不同的情况下扩展列的问题。 - www

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接