plotly / plotly/plotly.R

adding points to a surface reverses x and y axis

未关闭
#1,103 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

In trying to replicate some 3D marginal plots of a model, I notice that plot_ly swaps axes when adding points.

When using persp, I get a figure which I consider correct. meshout changes with size, but not distance.

mesh

but when mapping size and distance to x and y respectively, axes appear to be switched.

incorrect_plotly

When manually switching them, plot appears fine.

correct_plotly

Is this something you guys can look into? Below is a fully working example.

library(plotly)
library(ggplot2)
library(reshape2)
library(rgl)

N <- 50
B0 <-  1.2
B1 <-  1.5
B2 <- -0.5

set.seed(357)
count <- rpois(N, 10)

distance <- seq(-0.5, 0.5,,length(count))
size <- (log(count) - B0 - B2 * distance) / B1
xy <- data.frame(count, size, distance)

mdl <- glm(count ~ size * distance, family = poisson(link = log), data = xy)
summary(mdl)

ggplot(xy, aes(x = size, y = count, color = distance)) +
  geom_point()

npts <- 50
s.size <- seq(from = min(xy$size), to = max(xy$size), length.out = npts)
s.dist <- seq(from = min(xy$distance), to = max(xy$distance), length.out = npts)
mesh <- expand.grid(
  size = s.size,
  distance = s.dist
)
mesh$count <- predict(mdl, newdata = mesh, type = "response")
head(mesh)

meshout <- as.matrix(
  dcast(mesh, formula = size ~ distance, value.var = "count")[, -1]
)

pmat <- persp(x = s.size, y = s.dist, z = meshout, # size and distance mapped OK
      theta = 0, phi = 30,
      col = "grey90", border = "grey50")
points(trans3d(x = xy$size, y = xy$dist, z = xy$count, pmat = pmat))

plot3d(x = xy$size, y = xy$distance, z = xy$count) # check, distance and size are plotted correctly
# https://stackoverflow.com/questions/5468280/scale-a-series-between-two-points#comment49916403_5468527
range02 <- function(x, newMin, newMax){ (x - min(x))/(max(x)-min(x)) * (newMax - newMin) + newMin }

xy$scaled_size <- range02(xy$size, newMin = 0, newMax = 50)
xy$scaled_distance <- range02(xy$distance, newMin = 0, newMax = 50)

# size and distance swapped
plot_ly(z = ~ meshout) %>% 
  add_surface(name = "Incorrect mapping of x and y") %>%
  add_markers(data = xy, x = ~scaled_size, y = ~scaled_distance, z = ~count,
              marker = list(size = 5, color = "black"), name = "Incorrect mapping of x and y") 

# when switching mapping of x and y, plots fine
plot_ly(z = ~ meshout) %>% 
  add_surface(name = "Incorrect mapping of x and y") %>%
  add_markers(data = xy, x = ~scaled_distance, y = ~scaled_size, z = ~count,
              marker = list(size = 5, color = "black"), name = "Incorrect mapping of x and y")

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从使用 plot_ly()、add_surface() 和 add_markers() 的可复现 R 示例开始,并将其轴映射与 persp() 和 plot3d() 进行比较。跟踪表面坐标是如何相对于 marker 的 x 值和 y 值建立的;完成的标准是表面和添加的点使用相同的大小轴和距离轴,而无需手动交换它们。

由索引模型根据 Issue 内容生成。

评估

技术栈
r
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。