I have some data in CSV file and want to draw a 4d graphic. The x, y, z axis is respectively one column in the file. and the 4th dimension is a color respective of the value of another column in the file. How can I get a plot with both the x,y,z and color in R?
You will be able to make a 3D plot with color information encoded according to another variable in the data set. It depends on whether you need a surface or a scatterplot. For example, a 3D scatterplot package (install.packages("scatterplot3d")
would result, using the mtcars
dataset, in
library(scatterplot3d)
# create column indicating point color
mtcars$pcolor[mtcars$cyl==4] <- "red"
mtcars$pcolor[mtcars$cyl==6] <- "blue"
mtcars$pcolor[mtcars$cyl==8] <- "darkgreen"
with(mtcars, {
s3d <- scatterplot3d(disp, wt, mpg, # x y and z axis
color=pcolor, pch=19, # circle color indicates no. of cylinders
type="h", lty.hplot=2, # lines to the horizontal plane
scale.y=.75, # scale y axis (reduce by 25%)
main="3-D Scatterplot Example 4",
xlab="Displacement (cu. in.)",
ylab="Weight (lb/1000)",
zlab="Miles/(US) Gallon")
s3d.coords <- s3d$xyz.convert(disp, wt, mpg)
text(s3d.coords$x, s3d.coords$y, # x and y coordinates
labels=row.names(mtcars), # text to plot
pos=4, cex=.5) # shrink text 50% and place to right of points)
# add the legend
legend("topleft", inset=.05, # location and inset
bty="n", cex=.5, # suppress legend box, shrink text 50%
title="Number of Cylinders",
c("4", "6", "8"), fill=c("red", "blue", "darkgreen"))
})
yielding
You can find a list of examples, including the one above, here.