R图形动态线宽

时间:2017-04-02 04:12:44

标签: r plotly

我的数据中有多条路径,由NA分隔以构建单独的条形数据,我正在尝试根据该观察中的另一个值(下面的z列)实现动态宽度。

可重复的例子:

  index <- c(1,1,NA,2,2)
  value <- c(50,51,NA,51,52)
  width <- c(3,3,NA,5,5)
  data <- data.frame(x = index,
                 y = value,
                 z = width)

  p <- plot_ly(data, x = x, y = y, mode = "lines", 
           marker = list(color = '#ff2626'),
           line = list(width = data$z))

我正在尝试为z参数引用列line = list(width = data$z)

可以在Plotly for R软件包中完成,还是需要修复线宽?

1 个答案:

答案 0 :(得分:1)

index <- c(1,1,NA,2,2)
value <- c(50,51,NA,51,52)
width <- c(3,3,NA,5,5)
data <- data.frame(x = index, y = value, z = width)

# first, create an empty plotly object
p <- plot_ly(type = "scatter", mode = "lines+markers", marker = list(color = '#ff2626'))

# second, every third line a new lines definition begins. Loop through lines in steps of 3 and add lines
for (i in seq(1, nrow(data), by = 3)){
  p <- p %>% add_trace(x=data[i,"x"], y=data[i:(i+1), "y"], line=list(width=data[i,"z"]))
}

enter image description here