【问题标题】:Conver 3D scatter plots to 3D linear plots and separate based on the colours将 3D 散点图转换为 3D 线性图并根据颜色进行分离
【发布时间】:2018-09-11 16:48:31
【问题描述】:

一段时间以来,我一直在努力在 R 中绘制 3D 图表。我想我已经非常接近我想要的了。我之前问过question。我现在需要知道的只是如何将带点的散点图转换为线性。我的意思是,如果我能把这些点连接起来,那对我来说很棒。我现在拥有的如下所示: 我需要连接在 3D 中具有更好视图的点。我需要为每种颜色设置一条单独的线,并且我想在图表中添加图例。

我已将我的数据定义为:

df <- data.frame(a1 = c(489.4,  505.8,  525.8,  550.2,  576.6),
a2 = c(197.8,  301,    389.8,  502,    571.2),
b1 = c(546.8,  552.6,  558.4,  566.4,  575),
b2 = c(287.2,  305.8,  305.2,  334.4,  348.6), c1 = c(599.6,  611.4,  
623.6,  658,    657.4), c2 = c(318.8,  423.2,  510.8,  662.4,  656),
d1 = c(616,    606.8,  600.2,  595.6,  595),
  d2 = c(242.4,  292.8,  329.2,  378,    397.2),
e1 = c(582.4,  580,    579,    579,    579),
e2 = c(214,    255.4,  281.8,  303.8,  353.8))

colnames(df) <- rep(c("V1", "V2"), 5)
df.new <- rbind(df[, c(1, 2)],df[, c(3, 4)],df[, c(5, 6)],               
df[, c(7, 8)],df[, c(9, 10)])
df.new$Group <- factor(rep(c("a","b","c","d","e"), each = 5))
df.new$Class <- rep(c(1:5), 5)
x=df.new$Class
y=V1
z=V2

下面是我的代码:

 library(scatterplot3d)  #colors
 colors <- c("#999999", "#E69F00", "#56B4E9","#1B9E77", "#D95F02")
 colors <- colors[as.numeric(df.new$Group)]#Others
 xlabs <- c("[7,9]", "[10,12]", "[16,18]", "[19,21]", "[22,24]")
 scatterplot3d(x,y,z, pch = 16, color=colors,main="Title",xlab 
 ="Intervals",ylab = "", zlab = "Total time",     x.ticklabs=xlabs)
 text(8, 2.4, "c",cex = 1)
 text(9, 2, "c",cex = 1)

如果有人可以帮助我解决我一直在努力的这个问题,我真的很感激。我知道有一个 type=1 但它只制作了一个统一的情节。

【问题讨论】:

  • 可以在 scatterplot3d 中指定type = "l" 获取连接点(more
  • @RobJan 如何添加图例?
  • @RobJan 它连接所有点,但我希望每种颜色都有一条单独的线。有什么办法吗?
  • 这里是图例代码legend("right", legend = levels(df.new$Group), col= levels(as.factor(colors)), pch=16)
  • @RobJan 谢谢,太好了,只是你知道如何根据颜色连接线条吗?如果你知道你可以重播并接受作为答案。

标签: r plot scatter3d


【解决方案1】:

试试这个:

sd<-scatterplot3d(x,y,z, pch = rep(16:12, each=5), color=colors,main="Title",xlab 
                  ="Intervals",ylab = "", zlab = "Total time", x.ticklabs=xlabs)
sd$points3d(x[1:5],y[1:5],z[1:5], col="purple", type="l")
sd$points3d(x[6:10],y[6:10],z[6:10], col="orange", type="l")
sd$points3d(x[11:15],y[11:15],z[11:15], col="blue", type="l")
sd$points3d(x[16:20],y[16:20],z[16:20], col="green", type="l")
sd$points3d(x[21:25],y[21:25],z[21:25], col="red", type="l")
legend("right", legend = levels(df.new$Group), col= levels(as.factor(colors)),pch = rep(16:12, each=1)) 

【讨论】:

  • 谢谢,太好了,只是一件小事,每种颜色可以有不同的形状吗?
  • 在第一次调用中添加类似 pch = rep(16:12, each=5) 的内容
  • sd
  • @questionaskerprogrammer 抱歉,我没有意识到它看起来如此丑陋,我将编辑我的原始答案以反映变化
  • 非常感谢。这太棒了。如果可以编辑就更好了。
猜你喜欢
  • 2019-05-14
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 2022-07-08
  • 2011-07-26
  • 1970-01-01
相关资源
最近更新 更多