我正在编写一个返回绘图对象的函数。我已经设法控制了颜色。但是我在控制线型时遇到麻烦。目前,我使用类似:
plot_ly(colors=c(rep(c("#CD0C18","#1660A7"),each=3),'#9467bd'),linetypes = c(rep(c("dot","dash","solid"),2),"dot")) %>%
add_trace(data=long_data,x=~month,y=~temperature,color=~measure,linetype=~measure,type="scatter",mode="lines",line=list(width=4)) %>%
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months", categoryorder="array", categoryarray=month),
yaxis = list (title = "Temperature (degrees F)"))
向我返回警告:
警告消息:
plotly.js仅支持6种不同的线型
警告是有道理的,因为measure
有七个级别。 但是,每当我绘制6条以上的迹线时,我都希望在没有警告的情况下控制linetype
-有办法吗?
我的样本数据:
month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
mid_2000 <-apply(rbind(high_2000,low_2000),2,mean)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2000, low_2000,mid_2000, high_2007, low_2007, high_2014, low_2014)
long_data<-tidyr::gather(data,measure,temperature,-month)
答案 0 :(得分:2)
可以看到here,警告出现在
validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}
因此,如果您想单独禁用此警告,则只能做两件事:覆盖整个功能或手动扩展Schema$traces$scatter$attributes$line$dash$values
。后者的侵入性较小,可以通过以下方式完成
tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")
在这里,我们将NA
添加100次,这样现在最多106种线型就不会发出警告。在Schema
程序包环境中,最后一行用tmp
覆盖plotly
变量。
Schema$traces$scatter$attributes$line$dash$values
向量validLinetypes
仅here使用过四次,并且看这些欺骗似乎没有任何可能的副作用。