I am using rgl plot3d function to plot 3d points from Multidimensional scaling. So far I can draw the points as spheres using type="s", I can rotate the plots as I need:
plot3d(x,y,z,type = "s", col=rainbow(20), size= 4,xlab="F1",ylab="F2",zlab="F3",main=title.main,box=TRUE,top=TRUE).
I know that I can get this using rgl scatter3d function but this is used scatters/regression ... and I can also do this with scatterplot3d, but I can't rotate this one. I would have imagined that if it is possible to do this using rgl scatter3d function. I would expect that it would be easy in rgl plot3d function... but I haven't figure out how to plot these vertical lines from the points(spheres) using plot3d? If I use type = "h" it only draws vertical lines to the x–y plane and I don't see a separate parameter to draw the spheres.
Try:
> x=1:10
> y=21:30
> z=51:60
> plot3d(x,y,z, type='s')
> plot3d(x,y,z, type='h',add=TRUE)
Related
I use the R openair package to draw a scatterplot with 3 axes.
I want to add a outline to the symbols but scatterplot has some problems with this.
This is my code:
scatterPlot(data, x="NH4", y="NR", z="PM2.5", cols="jet", linear="FALSE",
cex=0.8, fontsize=15,
xlab="NH4+(ug/m3)", ylab="NR",key.footer = "PM2.5(ug/m3)",
labelFontsize=13, alpha=0.7)
I have drawn contour plot using plotly package in R. Additionally, I have used QUIVER function using pracma package to generate velocity vectors. Now I would like to overlay velocity vectors on top of the contour plot. How can I do that? Since I have not found any QUIVER function in r-plotly, so used another package.
plot_ly(x=x,y=y,z=z,type="contour") # contour plot using plotly package
quiver(x,y,u,v) # vector plot using pracma package
Now I would like to show both on the same plot.
I'd like make a plot using cartesian coordinate system, but then have it overlaid on a polar plot background, like that produced by coord_polar. Panel.background from theme.R only has element_rect; Ideally I could use something like element_polar.
Any way to do this?
Simply using coord_polar doesn't work because I'm also plotting various other geoms that map idiosyncratically onto coord_polar (geom_ellispis from the ggforce package, for example).
Reproducible example code:
library(ggplot2)
library(ggforce) # NB this is the github version #install_github("thomasp85/ggforce"). Includes 'geom_ellipsis'
#### Make example data
r<-runif(50,-100,100) # radial coordinates
theta<-runif(50,0,2) # theta
a<-runif(50,1,20)
b<-runif(50,1,20)
# Convert r and theta to cartesian:
x<-r*cos(theta*pi) # x-coordinate of ellipse foci
y<-r*sin(theta*pi) # y-coordinate of ellipse foci
angle.random<-runif(50,min=0,max=2) # random angle for ellipsis rotation
df<-as.data.frame(cbind(r,theta,x,y,a,b,angle.random))
# Make plots
# Plot should look like this:
ggplot(df,aes(x,y))+
geom_point(aes(x,y))+
geom_ellipsis(data=df,aes(x0=x,y0=y,a=a,b=b,angle=angle.random,fill=T))
# But I want the panel background in polar coordinates (and auto-adjusing to scale), like this:
ggplot(df,aes(x,y))+
geom_point(aes(x,y))+
coord_polar()
# However, using geom_ellipsis (among other functions) has idiosyncratic effects in non-cartesian coordinate systems:
ggplot(df,aes(r,theta))+
geom_point(aes(x,y))+
geom_ellipsis(data=df,aes(x0=x,y0=y,a=a,b=b,angle=angle.random,fill=T))+
coord_polar()
I would like the polar background from the 3rd plot, with the un-distorted ellipses from the first plot. Is there any way to do this?
I am using Octave plot() function to plot scatter points on a 2D graph. And then I am using the contour() function to draw a contour on top of the points. But the contour() function is not overlapping on top of the points. What happens is that the scatter plot graph is replaced entirely with the contour, even though I am using the HOLD ON command.
I have something like this:
plot(); %plot the x,y scatter plot
hold on; %hold on to be able to add to the plot
contour(); %Add the contour on top of the scatter plot
I wonder if someone can show some sample code they can show to add a contour to an existing plot.
thanks
Here is an example :
x = [-10:0.1:10];
y = x .^ 2;
z = x' * x;
hold on;
contour(x,y,z);
plot(x,y);
Will yield this graph (in blue you can see the parabol issued by plot).
I have a data.frame of x/y/z points. I know how to make a 3d scatterplot using the rgl package but I would like to connect each point in the scatterplot to make a wireframe or surface plot.
This code returns the scatter plot
library(rgl)
Data <- expand.grid(x=seq(0,10),y=seq(0,10))
Data$z <- Data$x^2+Data$y^2
plot3d(Data)
While this code returns a blank graph:
plot3d(Data,type='wire')
I can make the plot I want with lattice:
library(lattice)
wireframe(z~x+y,Data)
I can even make it rotate:
library(TeachingDemos)
rotate.wireframe(z~x+y,Data)
But I prefer rgl over lattice because it renders much quicker and lets you rotate the plot with the mouse.
Whats the proper way to make a wireframe plot in rgl?
The surface drawing plot function in rgl is persp3d and like base::persp, it needs a matrix as input to the z argument
zmat <- matrix(Data$z, 11,11)
persp3d(x=seq(0,10), y=seq(0,10), z=zmat)
I did spin this graphic a bit before capturing it with a screen grabbing program I use: