给出了答案:https://stackoverflow.com/a/51503530/10071318
我正在尝试创建3D散点图,其中包括两个回归平面。最后,我将提供一个适当的可复制示例。
主图命令:
p <- plot_ly(x=~x1.seq, y=~x2.seq, z=~z, colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
add_trace(inherit=F, x=~x1.seq, y=~x2.seq, z=~z2, colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x, mode="markers", type="scatter3d",
marker = list(opacity=0.6, symbol=105, size=7, color=~color_map)) %>%
layout(legend = list(x = 0.1, y = 0.9), scene = list(
aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
xaxis = list(title = "Litter size", range = c(2,7)),
yaxis = list(title = "Day", range = c(0,20)),
zaxis = list(title = "Weight", range = c(0.5,15))))
这将生成具有正确颜色(为我的数据框中的每个数据点定义)的图,但没有图例。
以前,我只是尝试遵循文档,该文档建议使用color=~treat, colors=c('color1', 'color2', 'color3')
(与我的变量一起使用)。
但是,在绘制时将其完全忽略,并且始终会产生红色,蓝色和绿色的点。但是,这产生了适当的图例。我还尝试将颜色定义为cols1<-c('color1', 'color2', 'color3')
,然后调用colors=cols1
。结果相同(红色,蓝色,绿色散点图)。
我正在寻找一种方法:1)调整散点图的颜色和2)仍然有图例。
谢谢!
可复制的代码:https://pastebin.com/UJBrrTPs
编辑:经过更多测试后,我发现更改迹线的顺序可以保持表面的正确颜色,但是会改变散布的错误颜色。
cols1 <- c("rgb(68, 1, 84)", "rgb(40, 125, 142)", "rgb(253, 231, 37)")
p <- plot_ly(x=~x1.seq, y=~x2.seq, z=~z, colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x, color=~treat, colors=cols1, mode="markers", type="scatter3d",
marker = list(opacity=0.6, symbol=105, size=7)) %>%
add_trace(inherit=F, x=~x1.seq, y=~x2.seq, z=~z2, colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
layout(legend = list(x = 0.1, y = 0.9), scene = list(
aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
xaxis = list(title = "Litter size", range = c(2,7)),
yaxis = list(title = "Day", range = c(0,20)),
zaxis = list(title = "Weight", range = c(0.5,15))))
这给出了以下图(请注意,与注释中发布的颜色相比,颜色有所不同,但仍然是错误的):
我也知道这个已解决的问题:https://github.com/ropensci/plotly/issues/790
我希望这可以帮助您查明问题所在。
24.07.2018:密谋更新到4.8.0。问题仍然存在,但是现在散点图看起来完全是白色的。
答案 0 :(得分:4)
我认为这就是您想要的?
library(plotly)
p <- plot_ly() %>%
add_surface(
x=~x1.seq, y=~x2.seq, z=~z,
colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
opacity=0.35, showlegend=FALSE, showscale = FALSE
) %>%
add_surface(
x=~x1.seq, y=~x2.seq, z=~z2,
colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
opacity=0.35, showlegend=FALSE, showscale = FALSE
) %>%
layout(
legend = list(x = 0.1, y = 0.9), scene = list(
aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
xaxis = list(title = "Litter size", range = c(2,7)),
yaxis = list(title = "Day", range = c(0,20)),
zaxis = list(title = "Weight", range = c(0.5,15)))
)
for (i in unique(.df$treat)) {
d <- .df[.df$treat %in% i, ]
p <- add_markers(
p, data=d, x=~z, y=~y, z=~x, text=~treat, name=~treat,
marker = list(opacity=0.6, symbol=105, size=7, color=~color_map)
)
}
p
答案 1 :(得分:2)
这与您所追求的类似吗?
您超级亲密,我做了一些小调整,以使其正常工作,就像您可能想要的那样。
## Map each value of `treat` to the desired color
Custom_Color_Mappings <- c("AP" = "rgb(40, 125, 142)",
"C" = "rgb(253, 231, 37)",
"PO" = " rgb(68, 1, 84)")
plot_ly(x=~x1.seq, y=~x2.seq, z=~z,
colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
type="surface",
opacity=0.35,
showlegend=F,
showscale = FALSE) %>%
add_trace(inherit=F, x=~x1.seq, y=~x2.seq, z=~z2,
colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
type="surface",
opacity=0.35,
showlegend=F,
showscale = FALSE) %>%
add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x,
mode="markers",
type="scatter3d",
color=~treat, ## Base color off of `treat`
colors = Custom_Color_Mappings, ## Map colors as defined above
marker = list(opacity=0.6, ## Note that the `color` and `colors` arguments are outside of the `marker` definition
symbol=105,
size=7)) %>%
layout(legend = list(x = 0.1, y = 0.9),
scene = list(aspectmode = "manual",
aspectratio = list(x=1, y=1, z=1),
xaxis = list(title = "Litter size", range = c(2,7)),
yaxis = list(title = "Day", range = c(0,20)),
zaxis = list(title = "Weight", range = c(0.5,15))))
结果如下: