Wrong axes in raster converted from matrix - r

I have following code:
library(raster)
library(rasterVis)
library(rgl)
mz <- matrix(5:7, 2040, 10000)
z <- raster(mz, xmn=0, ymn=0, xmx=ncol(mz)-1, ymx=nrow(mz)-1)
plot3D(z)
decorate3d()
This creates following image
As you can see y axis goes from 0 to 12 instead of 0 to 2040. And X axis goes up to 60, instead of 10000.
What shall I do to get the real values on the axis?
And how do I enforce showing 0 on the Z axis?

This result is documented in the examples of the help page of plot3D:
Default: x-axis and y-axis are adjusted with z-values. Therefore,
labels with decorate3d() are useless [...] Set adjust=FALSE to fix it
plot3D(z, adjust=FALSE)
decorate3d()

Related

Draw x-y-z box in r based on min max values of 3 variables

I calculated the niche width (from min to max extend) of a species related to 3 different variables. I now want to draw them in r as a single box showing var1 on the x-axis, var2 on the y-axis and var3 on the z-axis. This should result in a single box that is placed along the 3 axes.
I tried the rgl package in r.
# this is the extent of my scale
x <- c(31.94, 9460.00)
y <- c(28, 14375)
z <- c(0.02, 95.11)
# these are the coordinates of my box
x1<- 42.69
y1<- 4
z1<- 0.03
x2<- 3686.996
y2<- 1997.317
z2<- 5.156709
# my code is:
library(rgl) # load package
open3d() #open plot
plot3d(x, y, z, xlab="EC 1:5 [mS/cm]", ylab = "Na {mg/kg]", zlab="SAR") # plot axes
printBox(x1,y1,z1,x2,y2,z2) #add box
Unfortunately, this code adds the box (x1,y1,z1) at the lower-left corner of the set axes and extents the box to both, the neg. and pos. direction (x2,y2,z2).
Instead, I would like this code to add the box into the existing coordinates (plot3d()) with the lower-left corner at x1,y1,z1 and the upper left corner at x2,y2,z2.
I would be grateful for any help.

How to change Scale in raster plot?

Complete R noob, need help plotting a raster with a good scale.
rast <- raster("accessibility.tif")
pal <- colorRampPalette(c("green","red"))
plot(rast,
col = pal(10),
zlim = c(0,180))
This is what the output is:
See, all the values after 180 are simply not plot.
It's like they're cut out of the scale.
This wont do as there are a good minority of values after 180 that go till 3500.
I don't want the scale to go from 0 - 180,
I need it to go from 0 - 180+
I want all of those points to be plot in Red too.
Thank You for your help
You can do something like this:
library(raster)
rast <- raster("accessibility.tif")
r <- clamp(rast, 0, 180)
pal <- colorRampPalette(c("green","red"))
plot(r, col = pal(10))

R: Point colours determined by X, Y and Z values with scatterplot3d

I have this R script that uses scatterplot3d:
library(scatterplot3d)
attach(mtcars)
rgb <- read.csv(file="rgb-data.csv",head=TRUE,sep=",")
scatterplot3d(rgb$R,rgb$G,rgb$B,
xlim=c(0,255), ylim=c(0,255),zlim=c(0,255),
xlab="R", ylab="G", zlab="B", pch=".",
main=paste("Feature Space"))
Sample data for rgb-data.csv is:
R,G,B
12,48,126
127,12,48
46,127,12
It produces the plot:
I'm looking for 4 changes:
Have the (0,0,0) point being the front most point in the cube, most central, rather than the point in the bottom left of the plot. Is this possible?
Have the colour of each point reflect the R, G and B values of each point. E.g. (12,48,126) in the above dataset, the R value of the point is 12, G is 48 and B is 126. Is this possible?
The X, Y and Z axis range from 0 to 255 in the R script. However, in the plot they range from 0 to 300. I'd like the axis ranges to be 0 to 255 in the generated plot. Is this possible?
The "B" and "G" axis labels are rotated 270 degrees. I'd like them the same orientation as the "R" axis label. Is this possible?
I am not sure if I understood the 1st question correctly. But, here's the code.
Control with xlim, ylim, zlim
Use RGB function (note that you probably need to change your variable name since the function is called rgb)
Control with x.ticklabs, y.ticklabs, z.ticklabs
The code
library(scatterplot3d)
R <- sample(0:255, 100)
G <- sample(0:255, 100)
B <- sample(0:255, 100)
RGB_ <- rgb(R/255, G/255, B/255)
r_ticks <- c(-255, -200, -100, 0, 100, 200, 255)
ticks <- c(0, 50, 100, 150, 200, 250, 255)
scatterplot3d(R, G, B,
xlim=c(-255, 255),
ylim=c(0, 255),
zlim=c(0, 255),
color=RGB_,
x.ticklabs=r_ticks,
y.ticklabs=ticks,
z.ticklabs=ticks)
Have the (0,0,0) point being the front most point in the cube, most central, rather than the point in the bottom left of the plot. Is this possible?
Setting angle=140 in scatterplot3d( ) provided something close the desired plot:
The (0,0,0) position is more central at the bottom of the plot. I was also envisaging the R and G axes to be sloped slightly upwards into the distance from 0 to 255.
Have the colour of each point reflect the R, G and B values of each point. E.g. (12,48,126) in the above dataset, the R value of the point is 12, G is 48 and B is 126. Is this possible?
Based on Mo K's answer, adding
color=rgb(dat$R, dat$G, dat$B, maxColorValue = 255)
to scatterplot3d( ) gives the desired effect, provided renaming the original variable to dat i.e.
dat <- read.csv(file="rgb-data.csv",head=TRUE,sep=",")
The X, Y and Z axis range from 0 to 255 in the R script. However, in the plot they range from 0 to 300. I'd like the axis ranges to be 0 to 255 in the generated plot. Is this possible?
Mo K's answer works. Adding
ticks <- c(0, 50, 100, 150, 200, 250, 255)
Then
x.ticklabs=ticks,
y.ticklabs=ticks,
z.ticklabs=ticks
to scatterplots3d( ).
The "B" and "G" axis labels are rotated 270 degrees. I'd like them the same orientation as the "R" axis label. Is this possible?
I haven't found a solution to this yet.

R base plot polygon map with specified z range?

I'd like to generate a set of maps with consistent color ramps and am a little bit stuck. I know how to do what I want for rasters, but not for vector data.
Here's the behavior I want for rasters:
require(raster)
r1=raster(matrix(sample(1:50,16),4,4))
r2=raster(matrix(sample(1:100,16),4,4))
plot(r1,col=colorRampPalette(c("red","white","blue"))(10),zlim=c(0,100))
plot(r2,col=colorRampPalette(c("red","white","blue"))(10),zlim=c(0,100))
How do I make similar maps with polygons?
For example:
poly1=rasterToPolygons(r1)
poly2=rasterToPolygons(r2)
# Your previous code...
require(raster)
r1=raster(matrix(sample(1:50,16),4,4))
r2=raster(matrix(sample(1:100,16),4,4))
poly1=rasterToPolygons(r1)
poly2=rasterToPolygons(r2)
#################################################
# Color polygons with specific z range:
# Create a function to generate a continuous color palette to work with
rbPal <- colorRampPalette(c('red','white','blue'))
# Make color scale for polygons 1 & 2 by cutting the continuous
# palette object (rbPal) into 50 discrete blocks.
# Each of these blocks will be defined by the polygon layer
# values in a sequence ranging from 1 to 100
head(poly1); summary(poly1)
poly1$Col <- rbPal(50)[as.numeric(cut(poly1$layer, breaks = c(seq(1, 100, by=2))))]
poly2$Col <- rbPal(50)[as.numeric(cut(poly2$layer, breaks = c(seq(1, 100, by=2))))]
head(poly1); head(poly2)
# Plot
par(mfrow=c(1,2))
plot(poly1, col=poly1$Col)
plot(poly2, col=poly2$Col)

How to specify z axis range and add add circle or ellipse in 3D plot in R

3-D graphing with Google(http://www.r-bloggers.com/3-d-graphing-with-google/)
(mu1=0 mu2=0 sigma1=1 sigma2=1 pho=0)
exp((-1/2)*(x^2+y^2))/(2*pi) from -3 to 3
The rotate plot will be showd from google. The profile was a circle.
Dear Prof. Bolker gave me the R code:
library("emdbook")
library("rgl")
curve3d(dmvnorm(c(x,y),mu=c(0,0),Sigma=diag(2)),
sys3d="rgl",front="line",back="line",
xlim=c(-3,3),ylim=c(-3,3))
How to specify z axis range and get the plot like google's plot?
If pho=0 then the profile parallel to XY plane was circle.If pho<>0 then the profile parallel to XY plane was ellipse. How to add circle or ellipse in 3D plot? Thanks.
I am not sure that I fully understand your question but:
1/ I do not think rgl allow to specify z axis range (and curve3d seems to allow it only for xlim, ylim) so you probably need to do it by hand
2/ You can rescale axis in rgl using rgl.viewpoint : e.g., rgl.viewpoint(scale=c(1,1,0.1))
3/ You can draw circle or ellipse using:
t <- matrix(seq(-pi/2,pi/2, len=50), 50, 50, byrow=TRUE)
p <- matrix(seq(-pi, pi, len=50), 50, 50)
r <- 10
x <- r*cos(t)*cos(p)
y <- r*cos(t)*sin(p)
z <- r*sin(t)
persp3d(x, y, z)

Resources