gpt4 book ai didi

r - 情节(r): Unable to apply correct colors to 3D scatter and show legend at the same time

转载 作者:行者123 更新时间:2023-12-04 18:19:50 25 4
gpt4 key购买 nike

已给出答案: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))))

这给出了以下图(注意与评论中发布的颜色不同但仍然错误的颜色):
enter image description here

我也被告知这个已解决的问题: https://github.com/ropensci/plotly/issues/790

我希望这有助于查明问题。

24.07.2018:情节更新到 4.8.0。问题仍然存在,但是现在散点图看起来完全是白色的。

最佳答案

我想这就是你要找的吗?

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

enter image description here
/image/pTQj4.png

关于r - 情节(r): Unable to apply correct colors to 3D scatter and show legend at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51309447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com