0
votes

I am new to R, so can someone please help with this?

I have a data frame with 4 columns: x,y,z and freq. One row in this frame represents one point in 3D space (x,y,z are x-,y- and z- coordinates respectively) and it's frequency. I want to plot these points and make these points coloured such that the color is decided by the frequency. For eg: All points with frequency 0 are blue, between 1 and 5 are red, between 5 and 10 are orange, between 10 and 15 are yellow and so on. Some points can have a frequency of 0 also. But I don't know the range of frequency. Max no of colors to be used is 10. Also, there should be a scale explaining the meaning of colors.

I have been trying to correct the following code and make it work, but it`s just not working:

lev <- levels(factor(t$freq)); 
n <- as.numeric(lev); 
n <- n+1; 
plot3d(t$x,t$z,t$z,col=n);

Please help! Thank you.

PS- Please tell the solution using rgl package PPS - I have been trying to manipulate the col arguement in plot3d function of rgl package, but I am unable to get the desired result.

3
Please add an example of your problem (data + code), and what you have tried so far. This makes it easier for people to help you.Paul Hiemstra
I have been trying to correct the following code and make it work, but it`s just not working: lev <- levels(factor(t$freq)); n <- as.numeric(lev); n <- n+1; plot3d(t$x,t$z,t$z,col=n);Joe Roth
I would add these kinds of details to your original post, not as a comment.Paul Hiemstra
Sorry, I am new to stack overflow. I havent posted a lot of questions before, so I didnt know. Thanks for telling me.Joe Roth
Do note that we do not have your data (t), which makes it harder to answer the question.Paul Hiemstra

3 Answers

3
votes

I would load package rgl and do

plot3d(x,y,z, col=colors)

That means that you should prepare a list of color values that is of the same length as x,y,z lists so that you have a color selected for each x,y,z point.

the other part would be to make the list. I would try

givecolor = function(freq){
 if(freq < 1) "red"
 else if ....
}

colors = sapply(mydataframe[,"freq"], givecolor)
1
votes

You just need to build a vector of colors that is the same length as the number of points you are plotting. You then pass this vector as the col argument to the rgl plot3d() function. See this page for a demonstration that uses the iris dataset: http://planspace.org/2013/02/03/pca-3d-visualization-and-clustering-in-r/

0
votes

First you should select a palette you like and pick the number of colors you want, e.g. palette=rainbow(10). Then use a factor you get from splitting your data 10 ways to set your color from the palette.

See 3d scatterplot in R using rgl plot3d - different size for each data point? for how to effectively split a dataframe by a newly created factor. That question is w.r.t. size, but it also works for color.