使用R plotly为散点图添加单行

时间:2018-04-11 19:40:50

标签: r plotly scatter-plot

我有一个包含4个变量的数据集。我想在两个轴上绘制两个变量,使用第三个(分类为4个级别)作为标记颜色,并使用最后一个剩余变量来定义要动画的帧(分类为3个级别)。

我希望绘图是线条和标记的组合,但是,如此指定它会创建一个包含多条线条的图(对应于每种颜色),而我只想要一条线连接所有标记({{1}很容易)。

数据

ggplot

代码

> dput(dat[1:100], .(tme, err, rhstate, avgrh)])
structure(list(tme = structure(c(1465225200, 1465225200, 1465226400, 
1465227600, 1465228800, 1465230000, 1465231200, 1465232400, 1465233600, 
1465234800, 1465236000, 1465237200, 1465238400, 1465239600, 1465240800, 
1465242000, 1465243200, 1465244400, 1465245600, 1465246800, 1465248000, 
1465249200, 1465250400, 1465251660, 1465252860, 1465254060, 1465254060, 
1465255260, 1465256460, 1465257660, 1465258860, 1465260060, 1465261260, 
1465262460, 1465263660, 1465264860, 1465266060, 1465267260, 1465268460, 
1465269660, 1465270860, 1465272060, 1465273260, 1465274460, 1465275660, 
1465276860, 1465278060, 1465279260, 1465280460, 1465281660, 1465282860, 
1465282860, 1465284060, 1465285260, 1465286520, 1465287720, 1465288920, 
1465290120, 1465291320, 1465292520, 1465293720, 1465294920, 1465296120, 
1465297320, 1465298520, 1465299720, 1465300920, 1465302120, 1465303320, 
1465304520, 1465305720, 1465306920, 1465308120, 1465309320, 1465310520, 
1465311720, 1465311720, 1465312920, 1465314120, 1465315320, 1465316520, 
1465317720, 1465318920, 1465320180, 1465321380, 1465322580, 1465323780, 
1465324980, 1465326180, 1465327380, 1465328580, 1465329780, 1465330980, 
1465332180, 1465333380, 1465334580, 1465335780, 1465336980, 1465338180, 
1465339380, 1465340580), class = c("POSIXct", "POSIXt"), tzone = "America/Montreal"), 
    err = c(0.0321295814688971, 0.0258970346867303, -0.00521306194377552, 
    0.0242244349272305, 0.0185532902633201, 0.021652577485796, 
    0.0168988796968234, 0.0137794774960532, 0.014185112629704, 
    0.0134670989020071, 0.0128617764332387, 0.00968566692132371, 
    0.0109494843012271, 0.00729315868170971, 0.0118099136567953, 
    0.0125706426145261, 0.0386341452417785, 0.0311917161988425, 
    0.00651705953022401, 0.00812646139133542, 0.00617930149316948, 
    0.00441738338954978, 0.011277897938479, 0.0326854016428239, 
    0.00416255401011651, 0.00951378079129821, 0.0005598182566009, 
    0.0248692510142184, 0.0259965360705294, 0.00604794156880293, 
    0.0126714495305339, 0.0131901362597901, 0.013272025126147, 
    0.0135729548274825, 0.0199760402609557, 0.0209199710309039, 
    0.0217128168162313, 0.0250863709442256, 0.0300662327552579, 
    0.0302297911694611, 0.00319300332208453, 0.00151107265296471, 
    0.00296807852442163, 0.00250820089181706, 0.0311425518792544, 
    0.0315294910715092, 0.0340665895763144, 0.032360100403627, 
    0.0306485596156455, 0.0342048795151559, 0.0296090664127726, 
    0.0162262380117414, -0.00636919810186187, 0.0149732253947384, 
    0.0149294379129953, 0.0159937792584027, -0.0150745166190797, 
    0.0148037037868561, 0.0117746059073284, -0.0122939234719799, 
    -0.0121354130454219, -0.0099319346246827, -0.0139902189269146, 
    -0.0124549079966898, -0.0154947129243504, -0.0164860332206128, 
    -0.0185026965671351, -0.0136829936062086, -0.0179622162946781, 
    -0.0195691319337098, -0.0218429799490088, -0.0293327433991984, 
    -0.0312739247517495, -0.0309347697013604, -0.0381133521680837, 
    -0.0117624046962287, -0.00372091333168544, -0.00590822699141356, 
    -0.0060670165633279, -0.00895150810970304, -0.0108951436984084, 
    -0.0102845645562768, -0.00838560206009898, -0.00870999423496627, 
    -0.00953099508321077, -0.011741402771774, -0.00978598943043631, 
    -0.0110763987291388, -0.0176648546435892, -0.0145201653325585, 
    0.0117335945719424, 0.0114652567770258, 0.00594487115413547, 
    0.00840646301688352, 0.00733372062156731, 0.00579898677189856, 
    0.00700007721127538, 0.00474346968794474, 0.00551079519973678, 
    0.00601139417380159, 0.00349836520473813), rhstate = c("decreasing", 
    "decreasing", "decreasing", "decreasing", "stable", "stable", 
    "increasing", "stable", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "stable", "increasing", "stable", "stable", 
    "stable", "decreasing", "decreasing", "decreasing", "stable", 
    "stable", "increasing", "increasing", "increasing", "increasing", 
    "increasing", "increasing", "increasing", "increasing", "increasing", 
    "increasing", "increasing", "increasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "stable", "stable", "increasing", 
    "increasing", "increasing", "increasing", "increasing", "stable", 
    "stable", "stable", "stable", "stable", "stable", "stable", 
    "stable", "stable", "stable", "decreasing", "decreasing", 
    "stable", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "decreasing", "decreasing", 
    "decreasing", "decreasing", "decreasing", "stable", "stable", 
    "stable", "stable", "stable", "stable", "stable", "stable", 
    "stable", "stable", "stable"), avgrh = structure(c(4L, 4L, 
    4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("(85,90]", 
    "(90,95]", "(0,80]", "(80,85]", "(95,100]"), class = "factor")), .Names = c("tme", 
"err", "rhstate", "avgrh"), row.names = c(NA, -101L), class = c("data.table", "data.frame"))

动画效果很好。

当前输出

Current

想要输出

这是我希望它看起来像(使用dat[1:100] %>% plot_ly(x = ~tme, y = ~err, type = 'scatter', frame = ~as.character(rhstate), mode = 'markers', color = ~avgrh, showlegend = F) %>% add_trace(y = ~err, mode = 'lines') ):

ggplot

Wanted

如果可能,我想让每个线段与终止点颜色相同,即如果线连接蓝色标记和红色标记(终止点),我希望连接它们的线段为红色。哪一点是终止&#39; point是根据x值确定的(在这种情况下为plt<- ggplot(dat[1:100], aes(x = tme, y = err, frame = rhstate)) + geom_line() + geom_point(aes(color = avgrh), size = 2.5) gganimate(plt, filename = "ggplot_sample.gif") )。

我可以使用可以解决多行问题的解决方案,即使它不适用于动画。

0 个答案:

没有答案