如何用R中的Plotly包按两个因子分组3D线图?

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

我已经检查了official webpage of Plotly,但是如何绘制以下任务仍然无法解决:

id <- c(rep(1,5), rep(2,5), rep(3,5), rep(4,5))
t <- rep(seq(50,75,length.out=5), 4)
x <- runif(20) + sin(t) 
y <- rnorm(20) + cos(t)
gender <- c(rep("F",10), rep("M",10))
smoke <- c(rep("Y",5), rep("N",10), rep("Y",5))

DATA <- data.frame(ID, t, x, y, gender, smoke)

fig <- plot_ly(DATA, t = ~t, x = ~x, y = ~y, .......)

假设我有4组患者(按2个因素分组,分别为女性/男性和吸烟者/非吸烟者),每个患者沿时间戳$ t_i $具有5个观察值$(x_i,y_i)$。因此,我需要为每位患者绘制3D线图$$ {(t_i,x_i,y_i)} _ {i = 1} ^ {i = 5} $,但所有步骤都在一个绘图画布中。如果我想用红色/蓝色表示性别,用实线表示吸烟者,用虚线表示不吸烟者,并在图例中指定它们,我应该怎么做(最好使用R)?

r 3d plotly grouping line-plot
1个回答
0
投票
library(plotly) id <- c(rep(1,5), rep(2,5), rep(3,5), rep(4,5)) t <- rep(seq(50,75,length.out=5), 4) x <- runif(20) + sin(t) y <- rnorm(20) + cos(t) gender <- c(rep("F",10), rep("M",10)) smoke <- c(rep("Y",5), rep("N",10), rep("Y",5)) DATA <- data.frame(id, t, x, y, gender, smoke) col_gender <- c(M = "red", F = "blue") lt_smoke <- c(Y = "solid", N = "dash") fig <- plot_ly(DATA, x = ~x, y = ~y, z = ~t, color = ~gender, linetype = ~smoke, type = 'scatter3d', mode = 'lines+markers', line = list(width = 6), marker = list(size = 3.5, cmin = -20, cmax = 50), colors = col_gender, linetypes = lt_smoke) fig
© www.soinside.com 2019 - 2024. All rights reserved.