3D plot in R and find self-intersection - r

What is the easiest way in Rstudio to plot the 3D parametrization
g(t)=(cos(t)^2-0.5,sin(t)*cos(t),sin(t))
After I want to find values t1 and t2, for which g(t1)=g(t2) (hence I want to find self intersection)
Also how can i make a 2D of this parametrization
g(t)=((1+2*cos(t))*cos(t),(1+2*cos(t))*sin(t))
Regards,
s

Here is a solution for your 3D parametrization problem:
t <- seq(0, 2*pi, length.out=200)
gt <- data.frame(x=cos(t)^2-0.5, y=sin(t)*cos(t), z=sin(t))
library(plotly)
plot_ly(x=~x, y=~y, z=~z, data=gt, type="scatter3d", mode="lines")
For your 2D parametrization:
t <- seq(0, 2*pi, length.out=200)
gt <- data.frame(x=(1+2*cos(t))*cos(t),y=(1+2*cos(t))*sin(t))
plot(gt$x, gt$y, type="l", asp=1, xlab="x", ylab="y")

Related

How to align a map plot and connection plot with a rasterized image in r?

How do I make this map and its points align with my image?
[1]: https://i.stack.imgur.com/d6WRc.png
earth <- file.choose()
earth <- readJPEG(earth, native=TRUE)
par(mar=c(360,360,360,360))
grid.raster(earth)
maps:: map(add=TRUE)
points(x=cldrd$longitude, y=cldrd$latitude, col=c("magenta"), cex=(.7), pch=16)
points(x=outlrd$longitude, y=outlrd$latitude, col=c("black"), cex=(.2), pch=16)
inter <- gcIntermediate(c(10.451526,51.165691), c(-96.8410503,32.8143702), n=100, addStartEnd=TRUE, breakAtDateLine=F)
lines(inter, col=c("#00ffbf"), lwd=.05)
Read the image and georeference it (it would be preferable to start with georeferenced data, there is plenty to go around)
library(terra)
f <- "https://i.stack.imgur.com/d6WRc.png"
earth <- rast(f)
# eyeballing
ext(earth) <- c(-180, 180, -145, 145)
plotRGB(earth)
The image is so dark, that I figured I should add some lines for orientation
w <- geodata::world(path=".")
lines(w, col="gray")
Now your coordinates
crds <- rbind(c(10.451526,51.165691), c(-96.8410503,32.8143702))
points(crds, pch=20, col="red", cex=2)
library(geosphere)
inter <- gcIntermediate(crds[1,], crds[2,], n=100, addStartEnd=TRUE)
lines(inter, col="#00ffbf", lwd=2)

Contour plots on a sphere surface

I have acquired some data at a fixed distance R and for various theta (from the vertical axis) and phi (from the x axis) angles so to obtain a 3D representation of the quantity of interest. Please note that while phi spans 360°, theta only spans from 70° to 90°.
I know how to generate a 3D plot with the plot3D package (namely, the persp3D function) or a contour plot, but I would like to draw such contours over a sphere using the theta and phi angles information.
Would you please point me to the appropriate online resource where I can find a suitable solution?
Many thanks and kind regards
Nicola
This isn't exactly a 3d representation (e.g. in rgl), but maybe it gets you started:
library(maps)
library(mapproj)
library(akima)
set.seed(11)
n <- 500
x <- runif(n, min=-180,max=180)
y <- runif(n, min=-90,max=90)
z <- x^2+y^3
PARAM <- NULL
PROJ <- "orthographic"
ORIENT <- c(45,15,0)
XLIM <- c(-180, 180)
YLIM <- c(-90, 90)
nlevels=20
pal <- colorRampPalette(
c("purple4", "blue", "cyan", "yellow", "red", "pink"))
map("world", col=NA, param=PARAM, proj=PROJ, orient=ORIENT, xlim=XLIM, ylim=YLIM)
P <- mapproject(x,y)
incl <- which(!is.na(P$x))
Field <- interp(P$x[incl],P$y[incl],z[incl],
xo=seq(min(P$x[incl]), max(P$x[incl]), length = 100),
yo=seq(min(P$y[incl]), max(P$y[incl]), length = 100)
)
image(Field, add=TRUE, col=pal(nlevels))
points(P$x, P$y, pch=".", cex=2, col=4)
Cont <- contour(Field, add=TRUE, n=nlevels, col="white")
lines(sin(seq(0,2*pi,,100)), cos(seq(0,2*pi,,100)), lwd=3)

Kernel density scatter plot in R

I saw a beautiful plot and I'd like to recreate it. Here's an example showing what I've got so far:
# kernel density scatterplot
library(RColorBrewer)
library(MASS)
greyscale <- rev(brewer.pal(4, "Greys"))
x <- rnorm(20000, mean=5, sd=4.5); x <- x[x>0]
y <- x + rnorm(length(x), mean=.2, sd=.4)
z <- kde2d(x, y, n=100)
plot(x, y, pch=".", col="hotpink")
contour(z, drawlabels=FALSE, nlevels=4, col=greyscale, add=T)
abline(c(0,1), lty=1, lwd=2)
abline(lm(y~x), lty=2, lwd=2)
I'm struggling to fill the contours with colour. Is this a job for smoothScatter or another package? I suspect it might be down to my use of kde2d and, if so, can someone please explain this function or link me to a good tutorial?
Many thanks!
P.S. the final image should be greyscale
Seems like you want a filled contour rather than jus a contour. Perhaps
library(RColorBrewer)
library(MASS)
greyscale <-brewer.pal(5, "Greys")
x <- rnorm(20000, mean=5, sd=4.5); x <- x[x>0]
y <- x + rnorm(length(x), mean=.2, sd=.4)
z <- kde2d(x, y, n=100)
filled.contour(z, nlevels=4, col=greyscale, plot.axes = {
axis(1); axis(2)
#points(x, y, pch=".", col="hotpink")
abline(c(0,1), lty=1, lwd=2)
abline(lm(y~x), lty=2, lwd=2)
})
which gives

Texture in barplot for 7 bars in R?

I have 7 different categories per each value in X. I am using barplot to plot these categories. Such graph looks fine in colors printer, but what if I want it to be fine in black & white. You can check the graph below. I want to have different colors texture, so the graph looks good in color and black & white printer.
I used densities = c(10,30,40,50,100,60,80) for density parameter in barplot function. Are there any other ways to do different texture in barplot?
Note: I tried the angle value in barplot. However, it isn't a good solution in that case, since not all bars have high values (i.e height of the bar).
Along the lines of my comment, you might find the following helpful:
# data generation ---------------------------------------------------------
set.seed(1)
mat <- matrix(runif(4*7, min=0, max=10), 7, 4)
rownames(mat) <- 1:7
colnames(mat) <- LETTERS[1:4]
# plotting settings -------------------------------------------------------
ylim <- range(mat)*c(1,1.5)
angle1 <- rep(c(45,45,135), length.out=7)
angle2 <- rep(c(45,135,135), length.out=7)
density1 <- seq(5,35,length.out=7)
density2 <- seq(5,35,length.out=7)
col <- 1 # rainbow(7)
# plot --------------------------------------------------------------------
op <- par(mar=c(3,3,1,1))
barplot(mat, beside=TRUE, ylim=ylim, col=col, angle=angle1, density=density1)
barplot(mat, add=TRUE, beside=TRUE, ylim=ylim, col=col, angle=angle2, density=density2)
legend("top", legend=1:7, ncol=7, fill=TRUE, col=col, angle=angle1, density=density1)
par(bg="transparent")
legend("top", legend=1:7, ncol=7, fill=TRUE, col=col, angle=angle2, density=density2)
par(op)

Ploting 3D graphics with R

I have 3 data ranges using to plot in R:
x <- c(1,2,3,4,5)
y <- c(2,4,6,8,10)
z <- c(100,240,480,580,880)
How to plot a 3D graphic with those data in R (a 3d scatterplot) ?
There are many examples of this available with a bit of searching.
Some ideas:
install.packages("scatterplot3d")
library(scatterplot3d)
s3d <-scatterplot3d(x,y,z, pch=16, highlight.3d=TRUE,
type="h", main="3D Scatterplot")
Sometimes it is nice if you can rotate it:
install.packages("rgl")
library(rgl)
plot3d(x, y, z, col="red", size=3)
If you're looking for another option, you could use the plotly package for R.
library(plotly)
x <- c(1,2,3,4,5)
y <- c(2,4,6,8,10)
z <- c(100,240,480,580,880)
plot_ly(x = x, y = y, z = z, type="scatter3d", mode="markers")

Resources