Have names on the spheres built by rgl package plot3d - r

Is there a way to have the names shown when use plot3d(rgl) in R to build a 3d graph, cause it's hard to locate which entry the sphere belongs to when I have many spheres to plot on the same coordinate. For example, I have the data:
x y z
A 0.1 -0.5 3.2
B -1.1 1.2 0.8
C 2.0 2.1 0.6
......
plot3d(data,type="s",radius=0.025)
But, I want to have the name A, B, C shown on the graph as it's easier to observe.
Or to have the name shown only when I put the mouse onto one specific sphere.
I have tried to use different colors, but when I have like 20 spheres, it seems it will run out of colors or colors are too close to distinguish.

There is a function text3d() in library rgl that can be used to plot texts inside plot. This example shows how to plot row names as texts.
plot3d(data,type="s",radius=0.025)
text3d(data$x,data$y,data$z,text=rownames(data))

You can also combine the use of text3d with identify3d() to identify points you want to label interactively with your mouse

Related

R generate points with condition using runifpoint function

I am trying to generate randomly distributed points in a rectangle.
To create 50 random points in a rectangle, I used
i=50
pp<-runifpoint(i, win=owin(c(0,19.5),c(0,3.12))
If I were to add conditions on the coordinates before randomly generating points,
e.g. 0.24 <x<19.26 ,0.24<y<2.64 ,
then generate random points, what code can I imply?
The ultimate goal is to generate points in the rectangle except for the grey shaded area, in the below image
This is a question about the R package spatstat.
The argument win specifies the spatial region in which the points will be generated. In your example you have specified this region to be a rectangle. You just need to replace this rectangle by the region in which you want the points to be generated.
You can construct spatial regions (objects of class owin) in many ways. See help(owin), or help(spatstat) for an overview.
In your example, you could build up the shape by forming the union of several rectangles. For example to make a simple cross shape, I could just write
require(spatstat)
A <- owin(c(-1,1), c(-4, 4))
B <- owin(c(-4,4), c(-1,1))
U <- union.owin(A, B)
plot(U)
Another way would be to specify the corners of the polygon shape and use W <- owin(poly=p) where p = list(x, y) contains the coordinates of the corners, listed in anticlockwise order without repetition. See help(owin).
This is also covered in Section 3.5 of the spatstat book. You can download Chapter 3 for free.

Plotting a line between two points in gnuplot

I have a csv file with a following format having four columns (as a MWE):
xcoord1,ycoord1,xcoord2,ycoord2
0.1,0.2,0.4,0.3
0.5,0.3,0.7,0.5
I want to plot a line from each xcoord1,ycoord1 to xcoord2,ycoord2 using gnuplot. For example in this case, I would draw two lines from 0.1,0.2 to 0.4,0.3 and 0.5,0.3 to 0.7,0.5.
How is it possible?
Plot the lines as vectors without arrow heads:
plot "file" using 1:2:($3-$1):($4-$2) with vectors nohead
one solution would be to preprocess your file and generate a Gnuplot script which would employ the set arrow command. Alternatively one can plot the input file directly with a little preprocessing. The trick is to convert each line in the data file into a separate block, e.g.,
xcoord1,ycoord1,xcoord2,ycoord2
0.1,0.2,0.4,0.3
0.5,0.3,0.7,0.5
would be converted into
0.1 0.2
0.4 0.3
0.5 0.3
0.7 0.5
Gnuplot will then connect only points within one block. To achieve this, you could do:
plotCmd(fname)=sprintf("<gawk -F, 'NR>1{printf \"%%s\\t%%s\\n%%s\\t%%s\\n\\n\",$1,$2,$3,$4}' %s", fname)
plot plotCmd('input.csv') w lp

Gnuplot "vector line"

I am trying to generate a plot which uses arrows as markers in Gnuplot. These arrows I want to turn in a specific angle which I know. So I have value triples of x1 ... xn, y1...yn, alpha1...alphan. Sorry, I wasn't able to include a pic from my hard drive to illustrate what I want to achieve.
Basically, for every (15th or so) x-y pair, the marker should be an arrow which uses a certain angle.
The measured data is tightly packed so I suppose I will have to define an increment between the markers. The length of the arrow can be the same all over.
I would appreciate your ideas.
Gnuplot has a plot mode with vectors that is what you want
Given that your file has the following format, x y angle and assuming that
your angle is in radians, you have to take into account that
with vectors requires 4 parameters, namely x y dx dy where dx
and dy are the projections of the lenght of the arrow.
this draws only the arrows, if you want a line you have to make
two passes on the data.
you want to draw an arrow for a data point over, say, 10 points.
That said, I'd proceed like this
dx(a) = 0.2*cos(a) # 0.2 is an arbitrary scaling factor
dy(a) = 0.2*sin(a)
# this draws the arrows
plot 'mydata.dat' every 10 using 1:2:(dx(a)):(dy(a)) with vectors
# this draws the line
plot 'mydata.dat'
You may want to use help plot to find the detailed explanation of all the parameters that you can apply to a with vectors plot.
Credits: An article on the gnuplotting site

How to change the contours and legend in mathematica contour plot?

The ContourPlot function in Mathematica automatically gets you a legend and contours with colors on the plot which are uniformly distributed ( for example, blue color from 0.1 to 0.2 function values, green from 0.2 to 0.3 and etc.) In my case, function, that I plot, has a large number of values in the 0.1 to 0.2 and only few from 0.2 to 1. If I want to distinguish better values from 0.1 to 0.2 and make several colors for this section, and make the values from 0.2 to 1 by one color, how should I do this?
I would use the Mathematica function Hue[z] to assign a color to your contours. To do this, you're going to use the option ColorFunction, like this:
ContourPlot[myFunction, {x,-10,10}, {y,-10,10}, ColorFunction -> Function[{f},Hue[g[f]]]]
In this code, g[f] is some function that maps the contour level to a hue (a value between 1 and 255). You said you wanted many values between 0 and 0.2, and only a few between 0.2 and 1, so I would use something like
g[f_] := 100*(5*f)^(1/4)
Obviously you can change this to fit. If this doesn't help, you may need to increase the number of contours, using the option Contours->n, where n is how many you want. Hope this helps!

Draw a translucent sphere using rgl in R

I have a set of data, looks like:
x y z
1 1 2 1
2 3 5 7
3 -3 2 4
4 -2 1 1
so each row record the dot coordinate in a 3-D space. I want to plot all the dot as points except for one, say no.15 as a translucent sphere, with radius I can set. Then I can see from the plot that which of those points in the data are included in the sphere. I'm using RGL package right now and did the following:
> open3d()
> plot3d(readin,col=3,type="p")
> plot3d(readin[15,],col=2,add=T,type="s",radius=0.1)
So the first plot command plotted the whole set as scatter plots and the second plot command picked the 15th row of the data and plot it as a sphere and add it to the previous canvas. I just wondering if I can make the sphere translucent so that I can see which dots a included in the sphere which means those dots are very near to the one I select.
Is there a way to do this by RGL Or you can provide me another ways to complete this task?
Thanks!
I think what you are looking for is the argument alpha.
Example
df <- data.frame(x=c(1,3,-3,-2), y=c(2,5,2,1),z=c(1,7,4,1))
library(rgl)
open3d()
plot3d(df,col=3,type="p", radius=0.5)
plot3d(df,col=rgb(1,0,0.3),alpha=0.5, add=T,type="s",radius=1)
You can plot transparent spheres using the alpha argument to spheres3d. You can rotate the plot to move the box line behind the sphere to prove it's transparent.
spheres3d(dat[4,],col=rgb(1,0,0), alpha=0.9) # transparent red.
(I tried to do it with the alpha argument to rgb but it failed.)
If you just want to find out which points are within a certain radius of point 15 then you can calculate the Euclidean distance from each point to point 15 and see which of those distances are less than the radius. No plotting needed (though you could plot those points as a different color to highlight them. The dist function is one way to compute the distances, or it is simple to program yourself.

Resources