绘制灰度回归线

问题描述 投票:1回答:1

我正在尝试针对四种物种绘制蒸腾量相对于采样日的回归线,但是我正在使用指定彩色线的代码。我想使用以下代码,但要使用黑色,白色和灰色。

plotly_interaction <- function(data, x, y, category, colors = col2rgb(viridis(nlevels(as.factor(data[[category]])))), ...) {

 require(plotly)
 require(viridis)
 require(broom)

 groups <- unique(data[[category]])

 p <- plot_ly(...)

 for (i in 1:length(groups)) {
   groupData = data[which(data[[category]]==groups[[i]]), ]
   p <- add_lines(p, data = groupData,
                  y = fitted(lm(data = groupData, groupData[[y]] ~ groupData[[x]])),
                  x = groupData[[x]],
                  line = list(color = paste('rgb', '(', paste(colors[, i], collapse = ", "), ')')),
                  name = groups[[i]],
                  showlegend = FALSE)
   p <- add_ribbons(p, data = augment(lm(data = groupData, groupData[[y]] ~ groupData[[x]])),
                    y = groupData[[y]],
                    x = groupData[[x]],
                    ymin = ~.fitted - 1.96 * .se.fit,
                    ymax = ~.fitted + 1.96 * .se.fit,
                    line = list(color = paste('rgb','(', paste(colors[, i], collapse = ", "), ', 0.05)')), 
                    fillcolor = paste('rgba', '(', paste(colors[, i], collapse = ", "), ', 0.1)'),
                    showlegend = FALSE)
   p <- add_markers(p, data = groupData, 
                    x = groupData[[x]], 
                    y = groupData[[y]],
                    symbol = groupData[[category]],
                    marker = list(color=paste('rgb','(', paste(colors[, i], collapse = ", "))))
 }
 p <- layout(p, xaxis = list(title = x), yaxis = list(title = y))
 return(p)
}
r regression plotly
1个回答
0
投票

尝试一下:

plotly_interaction(iris, "Sepal.Length", "Sepal.Width", "Species", colors = col2rgb(c("black", "white", "grey")))

给出此:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.