Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 添加跟踪:在没有警告的情况下控制线型_R_Plotly - Fatal编程技术网

R 添加跟踪:在没有警告的情况下控制线型

R 添加跟踪:在没有警告的情况下控制线型,r,plotly,R,Plotly,我正在写一个函数,它返回一个plotly对象。我已经设法控制了颜色。但是,我无法控制线型。目前我使用的是: 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,linety

我正在写一个函数,它返回一个plotly对象。我已经设法控制了颜色。但是,我无法控制线型。目前我使用的是:

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条以上的记录道要打印时都不会收到警告-有办法吗

我的样本数据:

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) 
month可以看出,警告出现在

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)
}
在这里,我们添加了100次
NA
NA,这样最多106种线型就不会引起警告。最后一行在
plotly
包环境中用
tmp
覆盖
Schema
变量


向量
Schema$traces$scatter$attributes$line$dash$values
只被使用了四次(通过
validLinetypes
),从这些情况来看,这种欺骗似乎没有任何可能的副作用。

我想要一个使用
plotly
的解决方案。我知道我可以从
ggplot2
获取此信息。您是否尝试过
suppressWarnings
?不。我想在函数中使用它。因此,抑制警告会削弱功能-我的意思是,如果我将网格值设置为“虚线”,我希望收到警告:)@5th,有帮助吗?是的,它解决了问题。我决定制作一个版本来解决这个问题,另一个版本使用你的解决方案。
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")