如何更改R中3D散点图中数据点的颜色

时间:2014-08-15 10:26:53

标签: r colors plot 3d scatter-plot

我在R中的3D散点图有些麻烦;它已经有一段时间了,因为我大量使用它,这是我第一次使用3D图。

我的问题是尝试为定义绘制数据点的两个变量添加颜色 - 正在从.csv文件中读取数据。在下面的代码中,我能够生成一个完美的三维散点图,其中所有195个数据点都是蓝色的:

> with(sample, {
+    scatterplot3d(EXTRACT, ARRIVAL, PRICE, # x y and z axis
+                  color="blue", pch=19,    # filled blue circles
+                  type="h", lty.hplot=2,   # lines to the horizontal plane
+                  main="Extraction Dates for ORL",
+                  xlab="Extraction Dates",
+                  ylab="Arrival Dates",
+                  zlab="Price (EUR)")

当我尝试向两个变量添加颜色变化时,我收到以下错误:

> library(scatterplot3d)
> sample$pcolor[sample$COMP==WBSLON] <- "red"
Error in sample$pcolor[sample$COMP == WBSLON] <- "red" : 
  object 'WBSLON' not found
> sample$pcolor[sample$COMP==WBS6] <- "blue"
Error in sample$pcolor[sample$COMP == WBS6] <- "blue" : 
  object 'WBS6' not found
> with(sample, {
+    scatterplot3d(EXTRACT, ARRIVAL, PRICE, # x y and z axis
+                  color=pcolor, pch=19,    # filled blue circles on data points
+                  type="h", lty.hplot=2,   # lines to the horizontal plane; dashed lines
+                  main="Extraction Dates for ORL",
+                  xlab="Extraction Dates",
+                  ylab="Arrival Dates",
+                  zlab="Price (EUR)")
+ })

当我将原始.csv数据列本身的WBSLON和WBS6变量分别更改为1和2时,意味着:

sample$pcolor[sample$COMP==1] <- "red"
sample$pcolor[sample$COMP==2] <- "blue"

我没有收到任何错误,但它只会产生一个空白的3D图 - 绘图区域和轴标签,但没有绘制数据点。

代码是否无法识别文本中的变量?当我尝试在数据中使用数字替换时,为什么我会得到一个空白的图?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

使用“sample”作为正在使用的数据框的名称与R使用Sample作为函数冲突。使用与R已经使用的单词不冲突的不同名称允许代码在写入时成功读取和执行。