Plot 3D orography of a grid - r

I'd like to replicate something like the following plot using R, possibly using ggplot (though I doubt it is possible as, AFAIK, it has no 3D capabilities). The data I have is usually a raster file from the raster package, but I can transform it in the most suitable format.
The plot is taken from:
"Climate Change 2013: The Physical Science Basis. Contribution of Working Group I to the Fifth Assessment Report of the Intergovernmental Panel on Climate Change", figure 1.14. I have no idea which software has produced that plot.
I guess the only is to use lattice::cloud (wireframe) or something like this? I can't seem to find any way to force wireframe to have a ind of barplot instead of a surface plot; additionally, the coloring based on the height over the sea, where the grid is kept level, is probably impossible..

I found some time to look at this. In the end I took the following approach (linked R code):
http://pastebin.com/dA2nNNS0
Thanks to those in the comments who pointed me in the right direction.
I still have problems saving the file but that's material for another Question.
Code from pastebin:
library(raster)
library(rgl)
r <- raster("topo12.nc", varname="topo") #Read file
cols <- terrain.colors(3100) #Define colors
binplot.3d <- function(x,y,z,alpha=1,topcol="#ff0000",sidecol="#aaaaaa"){ #Binplotting function
save <- par3d(skipRedraw=TRUE)
on.exit(par3d(save))
x1<-c(rep(c(x[1],x[2],x[2],x[1]),3),rep(x[1],4),rep(x[2],4))
z1<-c(rep(0,4),rep(c(0,0,z,z),4))
y1<-c(y[1],y[1],y[2],y[2],rep(y[1],4),rep(y[2],4),rep(c(y[1],y[2],y[2],y[1]),2))
x2<-c(rep(c(x[1],x[1],x[2],x[2]),2),rep(c(x[1],x[2],rep(x[1],3),rep(x[2],3)),2))
z2<-c(rep(c(0,z),4),rep(0,8),rep(z,8) )
y2<-c(rep(y[1],4),rep(y[2],4),rep(c(rep(y[1],3),rep(y[2],3),y[1],y[2]),2) )
rgl.quads(x1,z1,y1,col=rep(sidecol,each=4),alpha=alpha)
rgl.quads(c(x[1],x[2],x[2],x[1]),rep(z,4),c(y[1],y[1],y[2],y[2]),
col=rep(topcol,each=4),alpha=1)
rgl.lines(x2,z2,y2,col="#000000")
}
cat("Row ( of", dim(r)[1],"):")
for (row in 1:dim(r)[1]) { #Plotting loop
for (col in 1:dim(r)[2]) {
if (round(r[row, col]) < 1) {
binplot.3d(c(col-1,col), c(row-1,row), r[row, col]/500, alpha=1, topcol="cadetblue3")
} else {
binplot.3d(c(col-1,col), c(row-1,row), r[row, col]/500, alpha=1, topcol=cols[round(r[row, col])])
# cat(round(r[row, col]), "\t")
}
}
cat(row, "")
}

Related

Represent a colored polygon in ggplot2

I am using the statspat package because I am working on spatial patterns.
I would like to do in ggplot and with colors instead of numbers (because it is not too readable),
the following graph, produced with the plot.quadratest function: Polygone
The numbers that interest me for the intensity of the colors are those at the bottom of each box.
The test object contains the following data:
Test object
I have looked at the help of the function, as well as the code of the function but I still cannot manage it.
Ideally I would like my final figure to look like this (maybe not with the same colors haha):
Final object
Thanks in advance for your help.
Please provide a reproducible example in the future.
The package reprex may be very helpful.
To use ggplot2 for this my best bet would be to convert
spatstat objects to sf and do the plotting that way,
but it may take some time. If you are willing to use base
graphics and spatstat you could do something like:
library(spatstat)
# Data (using a built-in dataset):
X <- unmark(chorley)
plot(X, main = "")
# Test:
test <- quadrat.test(X, nx = 4)
# Default plot:
plot(test, main = "")
# Extract the the `quadratcount` object (regions with observed counts):
counts <- attr(test, "quadratcount")
# Convert to `tess` (raw regions with no numbers)
regions <- as.tess(counts)
# Add residuals as marks to the tessellation:
marks(regions) <- test$residuals
# Plot regions with marks as colors:
plot(regions, do.col = TRUE, main = "")

How to make multiple plots with a for loop?

I was experimenting with the waffle package in r, and was trying to use a for loop to make multiple plots at once but was not able to get my code to work. I have a dataset with values for each year of renewables,and since it is over 40 years of data, was looking for a simple way to plot these with a for loop rather than manyally year by year. What am I doing wrong?
I have it from 1:16 as an experiment to see if it would work, although in reality I would do it for all the years in my dataset.
for(i in 1:16){
renperc<-islren$Value[i]
parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"),
title="Iceland Primary Energy Supply",
xlab=islren$TIME)
}
If I get your question correctly you want to plot all the 16 iterations in a same panel? You can parametrise your plot window to be divided into 16 smaller plots using par(mfrow = c(4,4)) (creating a 4 by 4 matrix and plotting into each cells recursively).
## Setting the graphical parameters
par(mfrow = c(4,4))
## Running the loop normally
for(i in 1:16){
renperc<-islren$Value[i]
parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"),
title="Iceland Primary Energy Supply",
xlab=islren$TIME)
}
If you need more plots (e.g. 40) you can increase the numbers in the graphical parameters (e.g. par(mfrow = c(6,7))) but that will create really tiny plots. One solution is to do it in multiple loops (for(i in 1:16); for(i in 17:32); etc.)
UPDATE: The code simply wasn't plotting anything when i tried putting in anything above one value (ex. 1:16) or a letter, both in terms of separate plots or many in one plot window (which I think perhaps waffle does not support in the same way as regular plots). In the end, I managed by making it into a function, although I'm still not sure why my original method wouldn't work if this did. See the code that worked below. I also tweaked it a bit, adding ggsave for example.
#function
waffling <- function(x){
renperc<-islren$Value[x]
parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"), title="",
xlab=islren$TIME[x])
ggsave(file=paste0("plot_", x,".png"))}
for(i in 1:57){
waffling(i)
}

Trouble getting started on my user defined function in R that creates 3 plots.

I am new to R. I've been trying to work on my code but I am having trouble understanding which 'parts' go where. This is the code I was given:
df=10
boxplot(rt(1000,df),rnorm(1000),names=c(paste("t,df=",df),"Standard Normal"))
x=seq(0,1,length=150)
plot(qt(x,df),qnorm(x),xlab=paste("t, df=",df),ylab="standard
Normal",main="qq-plot")
abline(0,1)
curve(dnorm(x),-3.5,3.5,main="Density Comparison")
curve(dt(x,df),lty=2,add=TRUE)
legend("topright",legend=c("standard
normal",paste("t,df=",df)),lty=c(1,2))
I am supposed to create a user defined function that takes df as the input and output 3 types of plots. I need to use: df=5,10,25, and 50.
This is what I have so far. Please dumb it down for me since I'm not very familiar with R terminology and I am not sure I am placing things where they are supposed to go..:
my.plot = function(n, df) {
a = rt(n,df)
b=rnorm(1000)
x= seq(0,1,length=150)
qt=qt(x,df)
qn=qnorm(x)
dn=dnorm(x)
ledt=dt(x,df)
n=1000
}
thebox= boxplot(a,b,names=c(paste("t,df=",df),"Stand rd Normal")) #1boxplot.
theplot= plot(qt,qn,xlab=paste("t, df=",df),ylab="standard Normal",main="qq-plot")
abline(0,1)
onecurve= curve(dn,-3.5,3.5,main="Density Comparison") #density curve
twocurve= curve(ledt, lty=2,add=TRUE)
legend("topright",legend=c("standard normal",paste("t,df=",df)),lty=c(1,2)
}
return(thebox)
return(theplot)
return(oneplot)
return(twocurve)
}
par(mfrow=c(1,3))
my.plot(1000,5)
my.plot(1000,10)
my.plot(1000,25)
my.plot(1000,50)
It works like this:
1. Your only input parameter is df. So your function will have only one input variable.
2. Since you already have the remaining code functional (i.e. after defining df) that code can be used as it is.
Below is a simple implementation from your demo code. You can modify it to suite your needs.
my.plot <- function(df) {
par(mfrow=c(1,3))
boxplot(rt(1000,df),rnorm(1000),names=c(paste("t,df=",df),"Standard Normal"))
x=seq(0,1,length=150)
plot(qt(x,df),qnorm(x),xlab=paste("t, df=",df),ylab="standard
Normal",main="qq-plot")
abline(0,1)
curve(dnorm(x),-3.5,3.5,main="Density Comparison")
curve(dt(x,df),lty=2,add=TRUE)
legend("topright",legend=c("standard
normal",paste("t,df=",df)),lty=c(1,2))
par(mfrow=c(1,1))
}
my.plot(5)
for(df in c(5,10,15,25)) my.plot(df)

R, graph of binomial distribution

I have to write own function to draw the density function of binomial distribution and hence draw
appropriate graph when n = 20 and p = 0.1,0.2,...,0.9. Also i need to comments on the graphs.
I tried this ;
graph <- function(n,p){
x <- dbinom(0:n,size=n,prob=p)
return(barplot(x,names.arg=0:n))
}
graph(20,0.1)
graph(20,0.2)
graph(20,0.3)
graph(20,0.4)
graph(20,0.5)
graph(20,0.6)
graph(20,0.7)
graph(20,0.8)
graph(20,0.9)
#OR
graph(20,scan())
My first question : is there any way so that i don't need to write down the line graph(20,p) several times except using scan()?
My second question :
I want to see the graph in one device or want to hit ENTER to see the next graph. I wrote
par(mfcol=c(2,5))
graph(20,0.1)
graph(20,0.2)
graph(20,0.3)
graph(20,0.4)
graph(20,0.5)
graph(20,0.6)
graph(20,0.7)
graph(20,0.8)
graph(20,0.9)
but the graph is too tiny. How can i present the graphs nicely with giving head line n=20 and p=the value which i used to draw the graph?[though it can be done by writing mtext() after calling the function graphbut doing so i have to write a similar line few times. So i want to do this including in function graph. ]
My last question :
About comment. The graphs are showing that as the probability of success ,p is increasing the graph is tending to right, that is , the graph is right skewed.
Is there any way to comment on the graph using program?
Here a job of mapply since you loop over 2 variables.
graph <- function(n,p){
x <- dbinom(0:n,size=n,prob=p)
barplot(x,names.arg=0:n,
main=sprintf(paste('bin. dist. ',n,p,sep=':')))
}
par(mfcol=c(2,5))
mapply(graph,20,seq(0.1,1,0.1))
Plotting base graphics is one of the times you often want to use a for loop. The reason is because most of the plotting functions return an object invisibly, but you're not interested in these; all you want is the side-effect of plotting. A loop ignores the returned obects, whereas the *apply family will waste effort collecting and returning them.
par(mfrow=c(2, 5))
for(p in seq(0.1, 1, len=10))
{
x <- dbinom(0:20, size=20, p=p)
barplot(x, names.arg=0:20, space=0)
}

Clearing plotted points in R

I am trying to use the animation package to generate an "evolving" plot of points on a map. The map is generated from shapefiles (from the readShapeSpatial/readShapeLines functions).
The problem is when it's plotted in a for loop, the result is additive, whereas the ideal result is to have it evolve.
Are there ways of using par() that I am missing?
My question is: is there a way to clear just the points ploted from the points function
and not clearing the entire figure thus not having to regraph the shapefiles?
in case someone wants to see code:
# plotting underlying map
newyork <- readShapeSpatial('nycpolygon.shp')
routes <- readShapeLines('nyc.shp')
par(bg="grey25")
plot(newyork, lwd=2, col ="lightgray")
plot(routes,add=TRUE,lwd=0.1,col="lightslategrey")
# plotting points and save to GIF
ani.options(interval=.05)
saveGIF({
par(bg="grey25")
# Begin loop
for (i in 13:44){
infile <-paste("Week",i,".csv",sep='')
mydata <-read.csv(file = infile, header = TRUE, sep=",")
plotvar <- Var$Para
nclr <- 4
plotclr <-brewer.pal(nclr,"RdPu")
class<- classIntervals(plotvar,nclr,style = "pretty")
colcode <- findColours(class,plotclr)
points(Var$Lon,Var$Lat,col=colcode)
}
})
If you can accept a residual shadow or halo of ink, you can over-plot with color ="white" or == to your background choices. We cannot access your shape file but you can try it out by adding this line:
points(Var$Lon, Var$Lat, col="grey25")
It may leave gaps in other previously plotted figures or boundaries, because it's definitely not object-oriented. The lattice and ggplot2 graphics models are more object oriented, so if you want to post a reproducible example, that might be an alternate path to "moving" forward. I seem to remember that the rgl package has animation options in its repetoire.

Resources