سؤال

I am using rgl to create a scatterplot of points from an imported .csv dataset. The colors that I'd like the points to be is set in the dataset. Everything works fine, except that when the scatterplot is displayed the colors of the points do not match the colors defined in the data. E.g., all the points that are designated as "blue" might actually be green, and all the points designated as "yellow" might actually show up red.

data=read.csv("ExpLayout.csv", header = TRUE)
x=data$x
y=data$y
z=data$z
color=data$color
plot3d(x=x, y=y, z=z, type="s", col=color)
هل كانت مفيدة؟

المحلول

This is almost certainly due to read.csv converting strings to factors

See the difference in this reproducible example

library(rgl)
x<-1:5
y=1:5
z <- 1:5
colors <- c('red','green','blue','orange','purple')
plot3d(x=x,y=y,z=z,col=colors, type = 's')
colorsf <- factor(c('red','green','blue','orange','purple'))
plot3d(x=x,y=y,z=z,col=colorsf, type = 's')

So, either read in color as a character column using stringsAsFactors=FALSE or coerce to character using as.character() or levels(colors)[colors]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top