How to change Scale in raster plot? - r

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))

Related

plot raster and overlay point values that satisfy criteria

I am plotting a raster like this one:
library(terra)
set.seed(1)
rr <- rast(matrix(rnorm(400, 1.5, 1), nrow=20, ncol=20))
plot(rr)
What I would ideally like is to add points to the plot where the raster values are above a threshold, the points I would like are seen here:
plot(clamp(rr, lower=3))
In my mind, something like this is possible (but in reality, this didn't work)
plot(rr)
points(clamp(rr, lower=3))
or maybe
plot(rr)
points(rr[rr>3])
obviously my examples don't work... anyone got an idea?
thank you,
m
You can do
library(terra)
set.seed(1)
rr <- rast(matrix(rnorm(400, 1.5, 1), nrow=20, ncol=20))
x <- clamp(rr, lower=3, values=FALSE)
plot(rr)
points(as.points(x))
lines(x)
## or polygons
# polys(as.polygons(x), dens=10)
So while lines(x) works, points(x) does not (this has now been fixed in the development version).
For this to work, the cells you do not want points for must be NA. Hence the values=FALSE in clamp.
To show different values (or ranges) for each point you can use plot(..., add=TRUE)
p <- as.points(x)
plot(rr, plg=list(shrink=.3))
plot(p, "lyr.1", add=T, legend=T)

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.

Wrong axes in raster converted from matrix

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()

Concentric circles R

I want to create 50 concentric circles. I did it with python but now I want to do this in R. I have tried the symbols function but with no result. I want my circles to start from x,y coordinates and the radius of each circle to be 3times bigger than the previous.
step=1
for(i in seq(1,50,1)){
symbols (x, y, circles=50, col="grey")
step=step+3
}
From this I get one circle as a result.
I am new in programming so it is probably very simple. Should I use a specific package?
The beauty of R is that many things can be vectorized, including the imput to the 'symbols' function. Here's an example for you:
#vector of radii
#written in a way that's easily changable
n_circles <- 50
my_circles <- seq(1,by=1,length.out = n_circles)
#generate x and y
x <- rep(1,n_circles)
y <- rep(1, n_circles)
#plot
symbols(x,y,1:n_circles)

Better hillshading for map plots in R

I'm working on improved hillshading for some topographical map plots. The basic hillshade workflow documented in image() is:
require(raster)
alt = getData('alt', country='CHE')
slope = terrain(alt, opt='slope')
aspect = terrain(alt, opt='aspect')
hill = hillShade(slope, aspect, 40, 270)
plot(hill, col=grey(0:100/100), legend=FALSE, main='Switzerland')
plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
This image shows plot(hill..) before plot(alt..) is applied:
The method creates a solid grey under-layer of hillshades on which other data layers (e.g. elevation shading) are plotted semi-transparently. The problem with this approach is (a) that the neutral colour for flat terrain (RBG (202,202,202), '#CACACA') severely shades the whole model, which (b) prevents multiple shade layering, such as used by the 'Swiss hillshade' approach.
I can imagine an workaround that converts rasters to matrices and applies hillshading as a numerical multiplier to the brightness of other layers, but this doesn't seem very elegant (although I may be wrong). I wonder if anyone has any ideas or (preferably) experience in this area? Thanks in advance.
No experience with this, but why not give the gray undermap an alpha value that depends on slopes? Here's my try:
# before
require(raster)
alt = getData('alt', country='CHE')
slope = terrain(alt, opt='slope')
aspect = terrain(alt, opt='aspect')
hill = hillShade(slope, aspect, 40, 270)
plot(hill, col=grey(0:100/100), legend=FALSE, main='Switzerland')
plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
As you say, very dark.
# after
grayalphas <- seq(-1,1,by=.01)^2*100
grayalphas[grayalphas==100] <- 99
plot(hill, col=paste0(grey(0:100/100),sprintf("%.2d",grayalphas)), legend=FALSE, main='Switzerland')
plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)
I set the gray alphas to have a parabolic shape, with minimum where the gray value is .5 and max of 99 at gray values of 0 or 1. If you choose something like this, you'll want to tinker with levels, etc, but it is easy to implement. Plus you'll want to put more effort than I did into the alphas, as mine are strictly numeric and not hex.
[Edit] I found a nifty function for adding alphas, addTrans() here in Sacha Epskamp's answer. This keeps the parabola, but it ranges from 0 in the middle to 255 on the extremes.
grayalphas <- seq(-1,1,length=101)^2*255
plot(hill, col=addTrans(grey(0:100/100),grayalphas), legend=FALSE, main='Switzerland')
plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)

Resources