Colour-coded 3D Plot in R - r

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.

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)

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/

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.

Related

How to create a simples (ternary) plot with color-coded triangles in R?

I have a matrix with 4 variables whereas 3 variables are parameters and the 4th variable gives the mean sum of squares for simulation results with the corresponding variables. Now I'd like to create a ternary plot with R where the triangle corresponding to the 3 parameter values should be colored by the mean sum of squares value. Alternatively, I'd like to plot interpolated mean sum of squares in the whole simplex triangle.
I was already looking for some functions or code that does what I'm looking for. But I didn't succeed.
Nevertheless, here's an example code of how my data set looks like (for which I'd like to create the ternary plot):
grid <- as.matrix(expand.grid(seq(0,0.5,0.025), seq(0,0.5,0.025), seq(-0.25,0.25,0.025)))
data <- cbind (grid, runif(9261,0,2))
I'd be very thankful if you'd provide R code that can create the plot I'd like to get. Maybe there's even a pre-implemented function in a package that I haven't found?!
Thanks a lot in advance for your help!

How to get the color coded plotted areas in images using R?

Hi R expert of the world,
Assume I have a point pattern that generate an intensity map and that this map is color coded in 3 region in an pixeled image.... how could I get the color-coded area?
here it is an example using spatstat:
library(spatstat)
japanesepines
Z<-density(japanesepines); plot(dens) # ---> I create a density map
b <- quantile(Z, probs = (0:3)/3) # ---> I "reduce it" to 3 color-ceded zones
Zcut <- cut(Z, breaks = b, labels = 1:3); plot(Zcut)
class(Zcut) # ---> and Zcut is my resultant image ("im")
Thank you in advance
Sacc
In your specific example it is very easy to calculate the area because you used quantile to cut the image: This effectively divides the image into areas of equal size, so there should be three areas of size 1/3 since the window is a unit square. In general to calculate areas from a factor valued image you could use as.tess and tile.areas (continuing your example):
Ztess <- as.tess(Zcut)
tile.areas(Ztess)
In this case the areas are 0.333313, which must be due to discretization.
I'm not exactly sure what you're after, but you can count up the number of pixels in each color using the table() function.
table(Zcut[[1]])

Gnuplot: plot with circles of a defined radius

I know on gnuplot you can plot some data with circles as the plot points:
plot 'data.txt' using 1:2 ls 1 with circles
How do I then set the size of the circles? I want to plot several sets of data but with different size circles for each data set.
If you have a third column in your data, the third column specifies the size of the circles. In your case, you could have the third column have the same value for all the points in each data set. For example:
plot '-' with circles
1 1 0.2
e
will plot a circle at (1,1) with radius 0.2. Note that the radius is in the same units as the data. (The special file name '-' lets you input data directly; typing 'e' ends the input. Type help special at the gnuplot console for more info.)
You can look here for more ideas of how to use circles.
I used:
plot "file" using 1:2:($2*0+10) with circles
This will fake a the third column specifying the sizes - it is probably possible to write it simpler, but this worked for me.

Make a 3D rendered plot of time-series

I have a set of 3D coordinates (below - just for a single point, in 3D space):
x <- c(-521.531433, -521.511658, -521.515259, -521.518127, -521.563416, -521.558044, -521.571228, -521.607178, -521.631165, -521.659973)
y <- c(154.499557, 154.479568, 154.438705, 154.398682, 154.580688, 154.365189, 154.3564, 154.559189, 154.341309, 154.344223)
z <- c(864.379272, 864.354675, 864.365479, 864.363831, 864.495667, 864.35498, 864.358582, 864.50415, 864.35553, 864.359863)
xyz <- data.frame(x,y,z)
I need to make a time-series plot of this point with a 3D rendering (so I can rotate the plot, etc.). The plot will visualize a trajectory of the point above in time (for example in the form of solid line). I used 'rgl' package with plot3d method, but I can't make it to plot time-series (below, just plot a single point from first frame in time-series):
require(rgl)
plot3d(xyz[1,1],xyz[1,2],xyz[1,3],axes=F,xlab="",ylab="",zlab="")
I found this post, but it doesn't really deal with a real-time rendered 3D plots. I would appreciate any suggestions. Thank you.
If you read help(plot3d) you can see how to draw lines:
require(rgl)
plot3d(xyz$x,xyz$y,xyz$z,type="l")
Is that what you want?
How about this? It uses rgl.pop() to remove a point and a line and draw them as a trail - change the sleep argument to control the speed:
ts <- function(xyz,sleep=0.3){
plot3d(xyz,type="n")
n = nrow(xyz)
p = points3d(xyz[1,])
l = lines3d(xyz[1,])
for(i in 2:n){
Sys.sleep(sleep)
rgl.pop("shapes",p)
rgl.pop("shapes",l)
p=points3d(xyz[i,])
l=lines3d(xyz[1:i,])
}
}
The solution was simpler than I thought and the problem was that I didn't use as.matrix on my data. I was getting error (list) object cannot be coerced to type 'double' when I was simply trying to plot my entire dataset using plot3d (found a solution for this here). So, if you need to plot time-series of set of coordinates (in my case motion capture data of two actors) here is my complete solution (only works with the data set below!):
download example data set
read the above data into a table:
data <- read.table("Bob12.txt",sep="\t")
extract XYZ coordinates into a separate matrixes:
x <- as.matrix(subset(data,select=seq(1,88,3)))
y <- as.matrix(subset(data,select=seq(2,89,3)))
z <- as.matrix(subset(data,select=seq(3,90,3)))
plot the coordinates on a nice, 3D rendered plot using 'rgl' package:
require(rgl)
plot3d(x[1:nrow(x),],y[1:nrow(y),],z[1:nrow(z),],axes=F,xlab="",ylab="",zlab="")
You should get something like on the image below (but you can rotate it etc.) - hope you can recognise there are joint centers for people there. I still need to tweak it to make it visually better - to have first frame as a points (to clearly see actor's joints), then a visible break, and then the rest of frames as a lines.

R language - heat maps

I'm trying to build a heat map using R. I have a matrix of values (percentage) like:
<p>0 5 0 0 25 30</p>
<p>0 0 0 10 0 0</p>
<p>10 15 65 65 70</p>
and so on.
What I want to get - a heat map where the same values (across the whole matrix) would be represented by the same colour. But I still get a map where the colour for the zero value in the first row differs from the colour for the zero value in the second row etc.
Command that I used to build a heat map is:
my_heatmap <- heatmap(my_heat_matrix, Rowv=NA, Colv=NA, col = colors_01, margins=c(5,10))
UPD: Sorry, I found an answer.
I think you should try the scale="none" argument.
A reproducible example would have been helpful ...
z <- outer(1:10,1:10,"+")
heatmap(z,Rowv=NA,Colv=NA)
heatmap(z,Rowv=NA,Colv=NA,scale="none")
heatmap may qualify as the most annoying R graphics function because of its use of layout, which makes it impossible to arrange the plots on the page in any sensible way ... (filled.contour and the plots from the hexbin package share similar annoyances).

Resources