
[英]How do I map categorical variables to color the outline of points in a 3D scatter plot in R Plotly?
[英]How do I change the coulour of the data points in a 3D Scatter Plot in R
我在R中的3D散点图上遇到了一些麻烦; 自从我大量使用它以来已经有一段时间了,这是我第一次使用3D图。
我的问题是尝试向定义绘图数据点的两个变量添加颜色-正在从.csv文件读取数据。 在下面的代码中,我可以用蓝色的所有195个数据点生成一个完美的3d散点图:
> 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图-图区域和轴标签,但没有绘制数据点。
代码有理由无法识别文本中的变量吗? 当我尝试在数据中使用数字替换时,为什么会得到空白图?
任何帮助是极大的赞赏!
将“ sample”用作所使用数据帧的名称与R将Sample作为函数使用时发生冲突。 使用与R已使用的单词不冲突的其他名称,可以按编写的方式成功读取和执行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.