Adding additional panel plots to an XY lattice plot - r

I'm looking to change the colour of a line and add an additional line in an xy plot
Distance<-c(1,2,3,4,5,6,7,8,9,10,11,12,13)
PAR<-c(1,3,5,9,15,12,11,6,5,4,3,1,0.5)
Function1<-function(Distance,param){
abs(param[1])*Distance*param[2]*exp(1)*exp(-(Distance*param[2]))
}
Function2<-function(Distance,param){
param[1]+param[2]*Distance
}
e<-c(0.05,0.1)
f<-c(4,0.9)
p1<-xyplot(PAR~Distance,data=Control,
xlab="Exponential Model", ylab=expression("C=1"),
xlim=c(0,20),
ylim=c(0,0.1),
col = ("cornsilk3"),
grid=(TRUE),
panel=function(x,y,...){
panel.xyplot(x,y,...)
panel.lines(x,(Function1(x,1,e))))}
So basically i'd like to add an additional panel line for function2 paired with the f list of parameters and have them as 2 different colours so i can differentiate between them.

Simply add another line panel.lines(x,(Function2(x,f)),col="red")
Note also, Function1(x,1,e) should be Function1(x,e) I think.
Your sample data is not in a data.frame Control. And you'll probably need to adjust ylim.
And your final )} should be }).

Related

Changing the titles within a raster stack

I had created a raster stack when plotted looks like this:
I would like to change the titles on each of these graphs, and add a main title to it.
levelplot(rs, main = "MD13U0001", col.regions=rev(terrain.colors(6)), names.attr=c("LoCoH", "Elevation"))
When I try using the function levelplot it gives me this:
It puts both plots into the same scale, but gives me what I want in relation to the titles. Is there a better function that will give me the titles, but not keep the scales separate?
Example data
library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster"))[[1:2]]
Set layer names and default plot
names(s) <- c("Apple", "Mango")
plot(s, cex.main=.8)
Some example customization
par(mar=c(3,3,5,5), mfrow=c(1,2))
for (i in 1:2) {
plot(s[[i]], cex.axis=.75, las=1)
title(names(s)[i], line=0.5, cex.main=0.8)
}
text(-40, 100, "These are my maps", xpd=NA, cex=2)

how to add specnames to rankabuncomp?

I am trying to add the species names to the rankabuncomp plot. I tried to add specnames = T to the rankabundance function but I get an error. Please help me work around this problem. Thanks a lot.
library(vegan)
library(BiodiversityR)
data(dune.env)
data(dune)
RankAbun.1 <- rankabundance(dune)
RankAbun.1
rankabunplot(RankAbun.1,scale='abundance', addit=FALSE, specnames=c(1,2,3))
rankabuncomp(dune, y=dune.env, factor='Management', scale='proportion', legend=FALSE)
#to add specnames
rankabuncomp(dune, y=dune.env, factor='Management', specnames=c(1,2,3), scale='proportion', legend=FALSE, labels=FALSE)
BiodiversityR::rankabuncomp explicitly disallows adding species names. This is hardcoded in the function and cannot be changed without editing the function (look for specnames = NULL in rankabunplot() calls). The help page does not give specnames argument to rankabuncomp so that this is also documented. I can understand this decision: rankabuncomp draws several lines in one graph, and often these are so similar that species names would overwrite each other and the plot becomes unreadable.
To add the lines, you either need edit the function or you must use a series of rankabunplot commands to overlay graphs in one plot. Here is an example how to do that with the dune data:
library(BiodiversityR)
data(dune, dune.env)
## list of models
mods <- with(dune.env, lapply(levels(Management), function(lev)
rankabundance(dune, dune.env, 'Management', lev)))
## level 4 seems to be most extreme: draw it first
rankabunplot(mods[[4]],scale='abundance', addit=FALSE, specnames=c(1,2,3))
## add the rest
## change colour etc to separate the lines if wished
rankabunplot(mods[[3]],scale='abundance', addit=TRUE, specnames=c(1,2,3))
rankabunplot(mods[[2]],scale='abundance', addit=TRUE, specnames=c(1,2,3))
rankabunplot(mods[[1]],scale='abundance', addit=TRUE, specnames=c(1,2,3))
The resulting plot is not very readable. If you use proportional scaling of y-axis, it becomes even less readable. However, you can do it.

How do I exclude parameters from an RDA plot

I'm still relatively inexperienced manipulating plots in R, and am in need of assistance. I ran a redundancy analysis in R using the rda() function, but now I need to simplify the figure to exclude unnecessary information. The code I'm currently using is:
abio1516<-read.csv("1516 descriptors.csv")
attach(abio1516)
bio1516<-read.csv("1516habund.csv")
attach(bio1516)
rda1516<-rda(bio1516[,2:18],abio1516[,2:6])
anova(rda1516)
RsquareAdj(rda1516)
summary(rda1516)
varpart(bio1516[,2:18],~Distance_to_source,~Depth, ~Veg._cover, ~Surface_area,data=abio1516)
plot(rda1516,bty="n",xaxt="n",yaxt="n",main="1516; P=, R^2=",
ylab="Driven by , Var explained=",xlab="Driven by , Var explained=")
The produced plot looks like this:
Please help me modify my code to: exclude the sites (sit#), all axes, and the internal dashed lines.
I'd also like to either expand the size of the field, or move the vector labels to all fit in the plotting field.
updated as per responses, working code below this point
plot(rda,bty="n",xaxt="n",yaxt="n",type="n",main="xxx",ylab="xxx",xlab="xxx
Overall best:xxx")
abline(h=0,v=0,col="white",lwd=3)
points(rda,display="species",col="blue")
points(rda,display="cn",col="black")
text(rda,display="cn",col="black")
Start by plotting the rda with type = "n" which generates an empty plot to which you can add the things you want. The dotted lines are hard coded into the plot.cca function, so you need either make your own version, or use abline to hide them (then use box to cover up the holes in the axes).
require(vegan)
data(dune, dune.env)
rda1516 <- rda(dune~., data = dune.env)
plot(rda1516, type = "n")
abline(h = 0, v = 0, col = "white", lwd = 3)
box()
points(rda1516, display = "species")
points(rda1516, display = "cn", col = "blue")
text(rda1516, display = "cn", col = "blue")
If the text labels are not in the correct position, you can use the argument pos to move them (make a vector as long as the number of arrows you have with the integers 1 - 4 to move the label down, left, up, or right. (there might be better solutions to this)

R contour levels don't match filled.contour

Hopefully a straightforward question but I made a simple figure in R using filled.contour(). It looks fine, and what it should like given the data. However, I want to add a reference line along a contour for 0 (level = 0), and the plotted line doesn't match the colors on the filled.contour figure. The line is close, but not matching with the figure (and eventually crossing over another contour from the filled.contour plot). Any ideas why this is happening?
aa <- c(0.05843150, 0.11300040, 0.15280030, 0.183524400, 0.20772430, 0.228121000)
bb <- c(0.01561055, 0.06520635, 0.10196237, 0.130127650, 0.15314544, 0.172292410)
cc <- c(-0.02166599, 0.02306650, 0.05619421, 0.082193680, 0.10334837, 0.121156780)
dd <- c(-0.05356592, -0.01432910, 0.01546647, 0.039156660, 0.05858709, 0.074953650)
ee <- c(-0.08071987, -0.04654243, -0.02011676, 0.000977798, 0.01855881, 0.033651089)
ff <- c(-0.10343798, -0.07416114, -0.05111547, -0.032481132, -0.01683215, -0.003636035)
gg <- c(-0.12237798, -0.09753544, -0.07785126, -0.061607548, -0.04788856, -0.036169540)
hh <-rbind(aa,bb,cc,dd,ee,ff,gg)
z <- as.matrix(hh)
y <- seq(0.5,1.75,0.25)
x <- seq(1,2.5,0.25)
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors) #This works
contour(x,y,z, level=0,add=T,lwd=3) #This line doesn't match plot
This is completely answered in the ?filled.contour help page. In the Notes section it states
The output produced by filled.contour is actually a combination of two plots; one is the filled contour and one is the legend. Two separate coordinate systems are set up for these two plots, but they are only used internally – once the function has returned these coordinate systems are lost. If you want to annotate the main contour plot, for example to add points, you can specify graphics commands in the plot.axes argument. See the examples.
And the examples given in that help page show how to annotate on top of the main plot. In this particular case, the correct way would be
filled.contour(x,y,z,
key.title = title(main=expression("log"(lambda))),
color.palette = topo.colors,
plot.axes = {
axis(1)
axis(2)
contour(x,y,z, level=0,add=T,lwd=3)
}
)
which produces

Equivalent of boxplot lwd parameter for bwplot

I want to have the box plotted with thicker lines. In boxplot function I simply put lwd=2, but in the lattice bwplot I can pull my hair out and haven't found a solution!
(with the box I mean the blue thing in the image above)
Sample code to work with:
require(lattice)
set.seed(123)
n <- 300
type <- sample(c("city", "river", "village"), n, replace = TRUE)
month <- sample(c("may", "june"), n, replace = TRUE)
x <- rnorm(n)
df <- data.frame(x, type, month)
bwplot(x ~ type|month, data = df, panel=function(...) {
panel.abline(h=0, col="green")
panel.bwplot(...)
})
As John Paul pointed out, the line widths are controlled by the the box.rectangle and box.umbrella components of lattice's graphical parameter list. (For your future reference, typing names(trellis.par.get()) is a fast way to scan the list of graphical attributes controlled by that list.)
Here's a slightly cleaner way to set those options for one or more particular figures:
thickBoxSettings <- list(box.rectangle=list(lwd=2), box.umbrella=list(lwd=2))
bwplot(x ~ type|month, data = df,
par.settings = thickBoxSettings,
panel = function(...) {
panel.abline(h=0, col="green")
panel.bwplot(...)
})
One thing you can do is get the trellis settings for the box, and change those. Try
rect.settings<-trellis.par.get("box.rectangle") #gets all rectangle settings
rect.settings$lwd<-4 #sets width to 4, you can choose what you like
trellis.par.set("box.rectangle",rect.settings)
Put these above your bwplot call and it should do it.
The box rectangle settings also has color, fill etc.
Edit to add if you get box.umbrella you can edit it to change what the lines above and below the box look like.
There is a further feature of lattice plots that needs mention. They are really objects, so methods exist for modifying their list representations;
myBW <- bwplot(x ~ type|month, data = df, panel=function(...) {
panel.abline(h=0, col="green")
panel.bwplot(...)
})
newBW <- update(myBW, par.settings=list(box.rectangle=list(lwd=4) ))
plot(newBW) # need to print or plot a grid object
You can also use trellis.focus and apply further updating function to overlay new data or text.

Resources