绘制多条迹线的颜色调色板和色标

时间:2018-08-12 09:57:48

标签: r plotly r-plotly

我正在尝试绘制不同月份的数据,用不同的轨迹表示dataframe之类的时间序列中的数据。我期望的输出是下面的图形的交互式版本,给定长data.frame。 (数据和代码如下)

enter image description here

数据的视觉检查-

          y year       time
1  15.70525 2005 0.41666667
2  15.82955 2005 0.50000000
3  17.55470 2005 0.58333333
4  18.10086 2005 0.66666667
5  17.49667 2005 0.75000000
6  19.34727 2005 0.83333333
7  20.03129 2005 0.91666667
8  23.48669 2006 0.00000000
9  12.53699 2006 0.08333333
10 15.46702 2006 0.16666667

有一个ggplotly解决方案,但我想避免。因此,在原生plotly中,如何-

  1. 在长data.frame中绘制多个迹线,并使用分组变量定义的每个迹线的数据。
  2. colorscale上定义自定义colorbar

我有一个针对1的有效解决方案,但对于plotly中的惯用语方式,我没有提出起诉;但是,我完全坚持使用2。

通过将数据从长到宽转换并为每列添加迹线,我达到了第1点。对于某些预定义的颜色,我的方法如下-

library(plotly)
yr.names <- as.character(unique(data[['year']]))
colours <- c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")
data <- stats::reshape(
  data = data, direction = 'wide', idvar = c('time'), 
  timevar = 'year', v.names = 'y'
)
colnames(data)[2:ncol(data)] <- yr.names
data <- data[order(data[['time']]), ]
p <- plot_ly(data = data)
for (i in seq_along(yr.names)) {
  p <- add_trace(
    p = p, y = data[[yr.names[i]]], x = ~time, name = yr.names[i], 
    type = 'scatter', mode = 'lines', line = list(color = colours[i])
  )
}
p

这产生- enter image description here

接下来,如何告诉plotly我的情况下year是连续变量?换句话说,如何手动定义将执行我想要的操作的colorbar / colorscale

这是我的无效尝试-

nCol <- length(colours)
colourscale <- data.frame(
  y = seq(0, 1, length.out = nCol), 
  col = as.character(colours)
)
p <- plot_ly(
  x = seq(0, 1, length.out = nCol), type = 'scatter', mode = 'markers', 
  y = mean(data[[yr.names[1]]], na.rm = TRUE), hoverinfo = 'none', marker = list(
    size = rep(0, nCol), color = as.numeric(yr.names), colorscale = colourscale,
    colors = colours, 
    colorbar = list(
      title = 'Year', nticks = nCol
    )
  )
)
for (i in seq_along(yr.names)) {
  p <- add_trace(
    p = p, y = data[[yr.names[i]]], x = data[['time']], name = yr.names[i], 
    type = 'scatter', mode = 'lines+markers', line = list(color = colours[i]), 
    showlegend = FALSE
  )
}
p

这产生-

enter image description here

这显然是不正确的。有指针吗?

问题数据

data <- data.frame(
  y = c(
    15.705248, 15.82955, 17.554701, 18.100864, 17.496668, 19.347265, 
    20.031291, 23.486694, 12.536987, 15.467018, 14.233539, 17.783058, 
    16.291602, 16.980282, 18.612189, 16.623343, 21.430241, 23.575517, 
    23.334206, 28.038383, 16.763869, 19.792754, 16.427305, 21.000742, 
    20.681002, 21.83489, 23.93020353, 22.93035694, 23.26333992, 
    25.25003022, 25.80609, 29.665356, 21.654285, 18.264945, 23.107677, 
    22.91251, 19.43174
  ), year = c(
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 
    2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 
    2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2008, 2008, 
    2008, 2008, 2008, 2008
  ), time = c(
    0.416666666666667, 0.5, 0.583333333333333, 
    0.666666666666667, 0.75, 0.833333333333333, 0.916666666666667, 
    0, 0.0833333333333333, 0.166666666666667, 0.25, 0.333333333333333, 
    0.416666666666667, 0.5, 0.583333333333333, 0.666666666666667, 
    0.75, 0.833333333333333, 0.916666666666667, 0, 0.0833333333333333, 
    0.166666666666667, 0.25, 0.333333333333333, 0.416666666666667, 
    0.5, 0.583333333333333, 0.666666666666667, 0.75, 0.833333333333333, 
    0.916666666666667, 0, 0.0833333333333333, 0.166666666666667, 
    0.25, 0.333333333333333, 0.416666666666667
  )
)

1 个答案:

答案 0 :(得分:0)

这是一个可以解决的解决方案,但仍然可以解决。

library(plotly)

p1 <- data %>% plot_ly(x = ~time, 
                 y = ~y, 
                 type = 'scatter', 
                 mode = 'lines', 
                 color = ~year,
                 marker = list(size = 1),
                 transforms = list(
                   list(
                     type = 'groupby',
                     groups = ~year,
                     styles = list(
                      list(target = 2005, value = list(marker =list(color = "#440154FF"))),
                      list(target = 2006, value = list(marker =list(color = "#3B528BFF"))),
                      list(target = 2007, value = list(marker =list(color = "#1FA187FF"))),
                      list(target = 2008, value = list(marker =list(color = "#9FDA3AFF")))
                    )
                   )
                 ))
p1

enter image description here

尽管您说过要避免使用ggplotly,但我认为它更直接

library(ggplot2)

p2 <- data %>% 
    ggplot(aes(time, y, group=year)) + 
    geom_line(aes(color = year)) +
    scale_color_viridis() + 
    theme_classic()
ggplotly(p2)

enter image description here