Colour gradient on a map in R (using image) - r

I am trying to create a map just to get a concept across, not actually display real data. So far I have the following code:
library(maps)
image(x=-100:10, y = -10:80, z = outer(-360:-250, -10:80), xlab = "lon", ylab = "lat")
map("world", col="gray", fill=TRUE, add=TRUE)
box()
Which in part I pulled together from some other forum posts. It creates this.
The bit I am struggling with is I want the gradational red-yellow-white colours to run N to S (it is just to demonstrate the direction of a trend). They are nearly there, but I cant seem to get the configuration of 'z' correct and I have a feeling I am doing a bad bodge and there is a proper solution. For info, I also want to create the same map with the gradient running E to W, ideally in a different colour palette.
Many thanks in advance.

This seems to work for making the color more even.
x <- -100:10
y <- -10:80
r <- outer(x, y^3, "+")
image(x, y, z = r, col = rev(heat.colors(30)), xlab = "lon", ylab = "lat")
map("world", col = "grey", fill = TRUE, add = TRUE)
And to change the direction of the color, adjust r,
x <- -100:10
y <- -10:80
r <- outer(x^3, y, "+")
image(x, y, z = r, col = heat.colors(30), xlab = "lon", ylab = "lat")
map("world", col = "grey", fill = TRUE, add = TRUE)

Related

display point labels in scatter3d

I plotted a 3d scatter plot in R using the scatter3d function.
Now, I want to plot the labels on every dot in the 3d scatter, such as every point has its ID next to it i.e., "1", "2" etc..
Here is what I tried:
library("car")
library("rgl")
scatter3d(geometry[,1],geometry[,2],geometry[,3] , surface=FALSE, labels = rownames(geometry), id.n=nrow(geometry))
This tutorial says that adding arguments labels=rownames(geometry), id.n=nrow(geometry) should display the labels on every dot but that did not work.
EDIT:
I uploaded the coordinate file here, you can read it like this
geometry = read.csv("geometry.txt",sep = " ")
colnames(geometry) = c("x","y","z")
EDIT:
Actually, even the example from the tutorial does not label the points and does not produce the plot displayed. There is probably something wrong with the package.
scatter3d(x = sep.l, y = pet.l, z = sep.w,
surface=FALSE, labels = rownames(iris), id.n=nrow(iris))
I can give you a quick fix if you want to use any other function other than scatter3d. This can be achieved using plot3d and text3d function. I have provided the basic code block of how it can be implemented. You can customize it to your needs.
plot3d(geometry[,1],geometry[,2],geometry[,3])
text3d(geometry[,1],geometry[,2],geometry[,3],rownames(geometry))
points3d(geometry[,1],geometry[,2],geometry[,3], size = 5)
After much messing around I got it (I also have the method for plot_ly if you,re interested)
test2 <- cbind(dataSet[,paste(d)],set.final$Groups,test)
X <- test2[,1]
Y <- test2[,2]
Z <- test2[,3]
# 3D plot with the regression plane
scatter3d(x = X, y = Y, z = Z, groups = test2$`set.final$Groups`,
grid = FALSE, fit = "linear",ellipsoid = FALSE, surface=FALSE,
surface.col = c("green", "blue", "red"),
#showLabels(x = x, y = y, z = z, labels=test2$test, method="identify",n = nrow(test2), cex=1, col=carPalette()[1], location=c("lr"))
#labels = test2$test,
id=list(method = "mahal", n = length(test2$test), labels = test2$test)
#id.n=nrow(test2$test)
)
#identify3d(x = X, y = Y, z = Z, labels = test2$test, n = length(test2$test), plot = TRUE, adj = c(-0.1, 0.5), tolerance = 20, buttons = c("right"))
rglwidget()

R: help on plot a 3D plot in a matrix file using surface3d

I would like to plot a figure similar to this example (see blow).
Here is my dataset example.
z <- data.frame(round(runif(977,500,600)))
z_matrix <- t(matrix(z[0:(as.integer(length(z[,])/10) * 10),],as.integer(length(z[,])/10),10))
I can yield some other 2D or 3D plots using ggplot, image2D, persp, and persp3d, however these plots are not looking great compared with the above 3D plot example.
I've tried using surface3d, but I got errors. I've also tried to convert the matrix format to x.y.z format using grid.to.xyz, but it seems that the format is not correct.
Furthermore, the color gradient changes with the ranges of z in various datasets. I need to "fix" a color pattern of gradient and apply it to other datasets so that they can comparable.
My questions:
how to yield a 3D plot in a matrix dataset using surface3d or plot3d?
how to fix a pattern of color gradient to a specific range of values?
Thanks for your help!
You can try with rgl surface3d for plotting your z_matrix:
library(rgl)
x <- 50*(1:nrow(z_matrix))
y <- 10*(1:ncol(z_matrix))
zlim <- range(z_matrix)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- rainbow(zlen) # height color lookup table
col <- colorlut[ z_matrix - zlim[1] + 1 ] # assign colors to heights for each point
open3d()
surface3d(x, y, z_matrix, color = col, back = "lines")
With grid lines (and without scaling x,y axes):
x <- 1:nrow(z_matrix)
y <- 1:ncol(z_matrix)
zlim <- range(z_matrix)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen) #rainbow(zlen) # height color lookup table
col <- colorlut[ z_matrix - zlim[1] + 1 ] # assign colors to heights for each point
open3d()
persp3d(x, y, z_matrix, col = col)
grid3d(c("x", "y+", "z"))
surface3d(x, y, z_matrix, color = col, back = "lines")
Following my own advice and using the volcano dataset, this is what I thought was the best match to your desired image as far as the background:
library(plot3d)
persp3D(z = volcano, col = "lightblue", shade = 0.5,
ticktype = "detailed", bty = "b2")
And this would be the coloring scheme best fit in the worked examples, but I think you might want to search on "terrain colors" if you needed an more exact fit to that image:
png(); persp3D(z = volcano, clab = c("height", "m"),
colkey = list(length = 0.5, shift = -0.1),
ticktype = "detailed", bty = "b2"); dev.off()
That package is using base graphics, so you would want to review the rotation options by reading the ?persp and ?persp3D help pages. Here's another experiment:
png(); persp3D(z = volcano, col=terrain.colors(100), clab = c("height", "m"), xlab="PPM" , ylab="Col", zlab="Height",
colkey=FALSE, theta=25, lighting="specular",
ticktype = "detailed", bty = "b2"); dev.off()

How to plot multiple datasets with errbar?

This is what I have done so far
library(Hmisc)
m1 <- read.table("mt7.1r1.rp", header = FALSE)
m2 <- read.table("mt7.1r2.rp", header = FALSE)
m3 <- read.table("mt7.2r1.rp", header = FALSE)
m4 <- read.table("mt7.2r2.rp", header = FALSE)
p1=m1[1]
per1=log10(p1)
ixxr=m1[3]
ixxi=m1[4]
p2=m2[1]
per2=log10(p2)
ixyr=m2[3]
ixyi=m2[4]
p3=m3[1]
per3=log10(p3)
iyxr=m3[3]
iyxi=m3[4]
p4=m4[1]
per4=log10(p4)
iyyr=m4[3]
iyyi=m4[4]
erxx=m1[5]
erxy=m2[5]
eryx=m3[5]
eryy=m4[5]
xmin <- floor(min(per1,per2,per3,per4))
xmax <- ceiling(max(per1,per2,per3,per4))
ymin <- floor(min(ixxr,ixxi))
ymax <- ceiling(max(ixxr,ixxi))
per1=unname(per1)
ixxr=unname(ixxr)
ixxi=unname(ixxi)
erxx=unname(erxx)
per1=unlist(per1)
ixxr=unlist(ixxr)
ixxi=unlist(ixxi)
erxx=unlist(erxx)
errbar(per1,ixxr,ixxr+erxx,ixxr-erxx,col='red',xlabel='Per (s)',ylabel='Zxx/Zxy')
par(new = T)
errbar(per1,ixxi,ixxi+erxx,ixxi-erxx,col='green')
But i got image
Y-axis from two datasets are overlapping. How to prevent this?
I want to have a unique axis in min,max range with one single label.
Should I group the data before the plotting or...?
Adding yaxt = 'n' to one of the two plots (I did it for the first one) you do not report the y axis. For having just one y label, use first ylab = NA, then set the y label in the second plot (or viceversa).
errbar(per1,ixxr,ixxr+erxx,ixxr-erxx,col='red', xlab='Per (s)',
yaxt = 'n', ylab = NA)
errbar(per1,ixxi,ixxi+erxx,ixxi-erxx,col='green', ylab = 'ixxr and ixxi')
It would be good practice to compute the common range of the y values and setting it through ylim, so to be sure that everything will be shown on the plot.

R plot,why is introducing of axis limits creating havoc?

My code
library(Hmisc)
r1 <- read.table("mt7.1r1.rp", header = FALSE)
r2 <- read.table("mt7.1r2.rp", header = FALSE)
r3 <- read.table("mt7.2r1.rp", header = FALSE)
r4 <- read.table("mt7.2r2.rp", header = FALSE)
p1=r1[1]
per1=log10(p1)
p2=r2[1]
per2=log10(p2)
p3=r3[1]
per3=log10(p3)
p4=r4[1]
per4=log10(p4)
m1=nrow(per1)
m2=nrow(per2)
m3=nrow(per3)
m4=nrow(per4)
xmin <- floor( min(per1,per2,per3,per4))
xmax <- ceiling( max(per1,per2,per3,per4))
lxmax=10^(xmax)
lxmin=10^(xmin)
rhoaxy = r2[3]
phaxy = r2[5]
rhoayx = r3[3]
phayx = r3[5]
rhoaxx = r1[3]
phaxx = r1[5]
rhoayy = r4[3]
phayy = r4[5]
per2=unname(per2)
per2=unlist(per2)
per3=unname(per3)
per3=unlist(per3)
rhoaxy=unname(rhoaxy)
rhoaxy=unlist(rhoaxy)
rhoaxy=log10(rhoaxy)
rhoayx=unname(rhoayx)
rhoayx=unlist(rhoayx)
rhoayx=log10(rhoayx)
ymin1=floor(min(rhoaxy)-1)
ymax1=ceiling(max(rhoaxy)+1)
ymin2=floor(min(rhoayx)-1)
ymax2=ceiling(max(rhoayx)+1)
ymin=min(ymin1,ymin2)
ymax=max(ymax1,ymax2)
png("withlim.png")
plot(per2,rhoaxy, col='red', xlab='Per (s)', ylab = 'Rho-xy/yx',ylim=c(ymin, ymax))
par(new=TRUE)
plot(per3,rhoayx, col='green', xaxt='n', xlab= NA, yaxt = 'n', ylab = NA)
dev.off()
The image I got
If I delete ylim
My question is,why are the axis limits changing the image content?The values from the second image correspond to proper data values.The first image is with values that do not represent rhoaxy and rhoayx.
It is difficult to test without the data, but my guess is that, on the second plot, the Y axis is not the same, although the Y axis is not plot.
So you've got the superposition of 2 plot, with a different Y axis.
If you want the same ylim on both plot, add ylim=c(ymin, ymax) on the second plot also.
If it does not work, please provide data example, so we can test.

Break axis in R

Recently I had some problems representing some data with xyplot. Everything appears very nicely but my boss asked me to break the axis (I'm not very fan of break axis). So far I have been able to do this with the function shingle, yet the order of panels is such a mess that read the information is imposible. In addition, I would like strips showing only the variable cs no the st (information specified in my sample data.frame) on the graphic. Then, the real challenge is to fix all this requirements into lattice, which is the internal standard for graphics in the group.
Here is my example data.frame (http://1drv.ms/1JOKSPU) sample of code:
AA<-read.csv("~/example.csv",header = T,sep = ";",dec = ",")
AA$st <-
shingle(Indl.Data$t,
intervals = rbind(c(0, 14),
c(23, 26)))
AA<-AA[with(AA, order(t)), ]
my.panel.1 <- function(x, y, subscripts, col, pch,cex,sd,...) {
low95 <- y-sd[subscripts]
up95 <- y+sd[subscripts]
panel.xyplot(x, y, col=col, pch=pch,cex=cex, ...)
panel.arrows(x, low95, x, up95, angle=90, code=3,lwd=3,
length=0.05, alpha=0.5,col=col)
}
xyplot( logOD~t|cs+st,
data=AA,
strip = T,
sd=0,
groups=cs,
xlab = list("Time ", cex=1.5),
ylab = list("growth", cex=1.5),
type="p",
col=c("red","black"),
scales = list(x = "free"), between = list(x = 0.5),
panel.groups="my.panel.1",
panel="panel.superpose",
par.settings = list(layout.widths = list(panel = c(6, 2))))
This is what I get with this:
In advance, sorry if there is any mistake in the formulation of my question, I do not have a programming background and this is me third question.
Cheers;
You want to go through st and then cs. So try xyplot( logOD~t|st+cs,.
Maybe use layout=c(4,3) if needed. And as.table=TRUE if you want it to start in upper left.

Resources