how to remove/reduce shininess of spheres in rgl plot - r

I have the following dataframe:
df <- data.frame(x=c(1,3,-3,-2), y=c(2,5,2,1),z=c(1,7,4,1))
I use the following rgl code:
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)
and get the following:
Now, I like my figure, but would like to reduce the gloss/shininess (or even eliminate it completely). It takes up too much of my plotting character. How do I do that?
I tried passing shininess as a parameter, and while it somewhat helps, it does not get rid of it completely. Even with shininess 128 (maximum value),it does not get rid of it completely.
library(rgl)
open3d()
plot3d(df,col=3,type="p", radius=0.5)
plot3d(df,col=rgb(1,0,0.3),alpha=.3, add=T,type="s",radius=1, shininess=128)
Then I get:
Can I get rid of the shinyness completely, or at least in a manner that it is almost nonexistent?

If you set the specular material colour to black, you won't see any shininess. A more extreme version is to set lit to FALSE. For example, here the spheres are drawn with specular = "black":
The black dots are the points you drew. And here they are drawn with lit = FALSE:
This removes important cues that people use for depth perception, so I don't recommend it.
Edited to add: One other thing I recommend if you're drawing transparent spheres: add back = "cull". Otherwise you can get weird interactions between the front and back faces of the spheres in certain rotations.

Related

How to Draw the Solution Curve in Julia with Arrow Correctly Using Plots?

I am trying to plot the solution curve from this page:
dynamicalsystem
But, I can't draw the arrow in the curve pointing to the origin, and the curve is not spiraling as well. Is something wrong with my code?
using MTH229, ForwardDiff, Plots, LaTeXStrings, SymPy
gr()
t = range(0, stop=21, length=10000)
x = #. exp(-2t)*cos(t)
y = #. exp(-2t)*sin(t)
plot(x, y, arrow = :closed, label=L"x(t)",
xlims=(-0.1,1), ylims=(-0.1,0.21))
You do not see a spiral, because the plot you have linked is not accurate (i.e. it has a wrong scale - most likely for didactic purposes). You can see that you have a spiral, by e.g. plotting the angle of the point plot(t, angle.(x + im * y)). The problem is that the plot absolute value of the points you plot gets very small very fast so these spirals are not visible.
Now the other issue is how to plot the arrow. The arrow in your case is just not visible because it is plotted at the end of your curve. I think (but maybe there are better ways to do it) that the simplest solution to pick a place to add it separately. For example after doing an initial plot add plot!(x[240:241], y[240:241], arrow=:closed) will add an extra arrow in the segment of your curve and the arrow will be visible (you just need to decide on the color of the arrow).

Error in pie3D (plotrix) in r

Lets make some data:
dat <- data.frame(art=c("Ål", "Gedde", "Brosme"), sum=c(708,3797,1385))
And when i plot this in a 3D plot, like this:
library(plotrix)
pie3D(dat$sum,labels=dat$art,explode=0.1, main="Arter")
This happens:
How can I avoid that red are below green?
Sometimes it helps to read the help:
Due to the somewhat primitive method used to draw sectors, a sector that extends beyond both pi/2 and 3*pi/2 radians in either direction may not display properly. Setting start to pi/2 will often fix this, but the user may have to adjust start and the order of sectors in extreme cases
pie3D(dat$sum,labels=dat$art,explode=0.03, start=pi/2, main="Arter")
(also, the explode=0.03 looks nicer imho)

Plotting straight surface with lattice::wireframe()

Assume I want to plot the following dataframe:
df <- data.frame(expand.grid(1:10,1:10),rep(10,100))
colnames(df) <- c("x","y","z")
with the lattice wireframe() function:
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE)
How do I get it to plot the given coordinates? I would assume it has something to do with having to scale/adjust the z-axis as the automatic scaling within wireframe is probably confused by all z-coordinates being equal.
This is from the help page scales section: "The most common use for this argument is to set arrows=FALSE, which causes tick marks and labels to be used instead of arrows being drawn (the default)." So just add that as a list value to 'scales':
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE,
scales=list(arrows=FALSE), zlim=c(0,10.1))
The failure of wireframe to display anything when the plotted plane is at one of the extremes seems to be at least "unexpected behavior" if not a bug. I suspect you would not see this in real data. Your use of drape doesn't make much sense since the entire data-plane plane gets displayed at the white midpoint. (Again this is probably not a problem if you have something other than this pathological example.)
Just add a zlim argument.
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE, zlim=c(0,20))

shading certain area in R

I met a problem when shading some areas by using function polygon in R. if these areas have common parts, the last shaded one will cover the previous one. But now, I do want do see this phenomenon and want to keep the original boundaries. what should I do?
see the following image as an example:
the codes for drawing this image are:
plot(variance_five[51:60,2],type="n",ylim=c(0,0.1))
for(i in 1:6)
{
polygon(c(1:10,10:1),c(variance_five[(10*i-9):(10*i),1],variance_five[(10*i): (10*i-9),3]),lty=2,col=i)
lines(variance_five[(10*i-9):(10*i),2])
}
we can see that the last shaded area(purple) covers some blue and light blue parts, what shall I do keep all the boundaries of all areas and the colour in the common areas are the sum effect of these colours? Thank you!
You want to use transparency. The function alpha in the scales package will add transparency to any color. I'd recommend setting up a vector of colors before the loop and making them transparent, e.g.
my_colors <- rainbow(6)
require(scales)
my_colors <- alpha(my_colors, alpha = 0.5)
And then inside the loop replacing col = i with col = my_colors[i].
You can, of course, adjust the alpha level (0 is completely transparent, 1 completely opaque) as necessary. The scales package also provides a function show_col which I find useful in picking colors. Try
show_col(rainbow(6))
show_col(heat.colors(6))
show_col(cm.colors(6))
I should note that rainbow et al. take alpha as an argument, so you don't need the alpha function, but it's more general to use the alpha function which will work on other R colors if you've got favorites. (I like dodgerblue2 and firebrick4, personally.)

Drawing circles in R

I'm using plotrix package to draw circles.
And I don't get what is wrong with my code... :-(
I have three points. The first point (1,1) should be the center of the circle. The following two points (1,4) and (4,1) have the same distance/radius to the center.
So the circle in the plot should go through these points, right?
And I don't know why the circle looks wrong. Is there an explanation?
p1 <- c(1,1)
p2 <- c(4,1)
p3 <- c(1,4)
r <- sqrt(sum((p1-p2)^2))
plot(x=c(p1[1], p2[1], p3[1]),
y=c(p1[2], p2[2], p3[2]),
ylim=c(-5,5), xlim=c(-5,5))
draw.circle(x=p1[1], y=p1[2], radius=(r))
abline(v=-5:5, col="#0000FF66")
abline(h=-5:5, col="#0000FF66")
Take a look at the produced output here
As #Baptiste says above, you can use plot(...,asp=1). This will only work if your x and y ranges happen to be the same, though (because it sets the physical aspect ratio of your plot to 1). Otherwise, you probably want to use the eqscplot function from the MASS package. A similar issue arises whenever you try to do careful plots of geometric objects, e.g. Drawing non-intersecting circles
This plot is produced by substituting MASS::eqscplot for plot in your code above:
Note that depending on the details of what R thinks about your monitor configuration etc., the circle may look a bit squashed (even though it goes through the points) when you plot in R's graphics window -- it did for me -- but should look OK in the graphical output.

Resources