I've generated a plot of an RDA using R. When I add my biplot on to the RDA, it adds new axes that I would like to turn off. Here is some example data:
Site<-c(1,2,3,4,5,6)
inv1<-c(34,67,78,45,677,23)
inv2<-c(45,5,55,56,7,8)
inv3<-c(6,7,4,12,7,8)
depth<-c(3,1,6,7,10,57)
exampledata_exp<-data.frame(Site,depth)
exampledata_invs<-data.frame(inv1,inv2,inv3)
dpRDA.out<-capscale(exampledata_invs~depth+Site, data=exampledata_exp, distance="bray")
summary(dpRDA.out)
plot(dpRDA.out, display=c("sites"), type="n", scaling=1, cex=1, family="serif")#this is the way I want the plot borders to look
points(dpRDA.out, display = c("sites"))
points(dpRDA.out, display="bp", col="black", scaling=1, family="serif")
The last bit of code adds on a new axes. I have tried axes=FALSE, xaxt="n", axes(side=3, lwd=0). Any suggestions greatly appreciated.
libraries currently loaded:
library(vegan)
library(reshape)
library(ggplot2)
library(plyr)
library(MASS)
library(tables)
library(matrixStats)
You can switch off the new axes by setting the axis.bp parameter to FALSE in the last line.
Try:
points(dpRDA.out, display="bp", col="black", scaling=1, family="serif", axis.bp = FALSE)
Related
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)
I've tried over and over to solve this issue but I can't get it down. I have estimated a Beta-t-EGARCH model and a GARCH-t model in R and now I need to plot the results over the same plot. The final result is horrible, since the variables don't share the same scale on the y axis. I'm new to R, so please don't blame me :).
Here's the code:
library(quantmod)
library(betategarch)
library(fGarch)
library(ggplot2)
getSymbols("GOOG",src="yahoo")
google_ret <- abs(periodReturn(GOOG, period="daily", subset=NULL, type="log"))-mean(abs(periodReturn(GOOG, period="daily", subset=NULL, type="log")))
googcomp <- tegarch(google_ret, asym=FALSE, skew=FALSE)
goog1stdev <- fitted(googcomp)
#now we try to fit a standard GARCH-t model
googgarch <- garchFit(data=google_ret, cond.dist="sstd")
googgarch2 <- garchFit(data=google_ret, cond.dist="sstd", include.mean = FALSE, include.delta = FALSE, include.skew = FALSE, include.shape = FALSE, leverage = FALSE, trace = TRUE)
volatility <- volatility(googgarch2, type = "sigma")
plot(google_ret)
par(new=TRUE)
plot(googgarch2, which=2)
par(new=TRUE)
plot(goog1stdev, col="red")
The final result is a plot completely out of scale on the y axis, with variables that have lower values plotted above higher ones. Thanks a lot to anybody that wants to help me!
The recommended approach is to plot them as different plots stacked on top of each other:
layout(matrix(1:3,3))
plot(google_ret)
plot(googgarch2, which=2)
plot(goog1stdev, col="red")
You can get rid of the whitespace with calls to par("mar") to adjust margin sizes:
opar=par(mar=par("mar") -c(1,0,3,0)) # opar will then let your restore previous values
..... plotting efforts
par(opar)
I don't know your domain very much but if you cna use shifted y-ordinates then this produces a somewhat cleaned up version with overlayed plots:
png()
plot(google_ret, ylim=c(0,1), ylab="ylab="Google Returns(black); GGarch x10 +0.5 (blue); STD + 0.3(red)" )
par(new=TRUE)
plot(googgarch2#data +.5, type="l", col="blue",axes=FALSE, ylab="", main="",ylim=c(0, 1)) ;abline(h=.5, col="blue")
par(new=TRUE);
plot( 10*coredata(goog1stdev) + .3, col="red", type="l", axes=FALSE, main="",ylim=c(0,1), ylab=""); abline(h=.3, col="red")
dev.off()
I'm really new in R statistics and i just bumped into a problem when it comes to creating a box plot. I have used the following script that i have created:
# Reset R's braingetwd
rm(list=ls())
# Tells R where to look
setwd("/Users/Axel/Desktop/Kandidatarbete/Data")
# Confirms R is looking at the right place
getwd()
# Read data
read.table("migration_test_graph.txt")
# Assign a name to the data
migrationtest5<- read.table("migration_test_graph.txt", ,col.names=c('treatment','migration'), header=TRUE, sep="\t", na.strings="NA", dec=",", strip.white=TRUE)
mzmz=migrationtest5[which(migrationtest5$treatment == "MzMz"),]
mzct=migrationtest5[which(migrationtest5$treatment == "MzCt"),]
ctct=migrationtest5[which(migrationtest5$treatment == "CtCt"),]
ctmz=migrationtest5[which(migrationtest5$treatment == "CtMz"),]
#plot data (migration in function of index)
boxplot(migration~treatment, data=migrationtest5, boxwex=0.5, varwidth=TRUE, staplewex=FALSE, outline=TRUE, outpch=20, cex= 0.3, range=1, title=FALSE, whisklty=19, col=c("darkolivegreen","darkolivegreen","darkolivegreen3", "darkolivegreen3"), xlab="", ylab="Migration index", las=1, ylim=c(0, 1), cex.lab=1, cex.axis=0.75, axes=FALSE, border=NA, medcol="white", whiskcol="black", outcol="black", whisklwd=2, medlwd=4)
axis(1, at=seq(0, 5, by=1), cex.axis=1, labels=c("", "CtCt","MzCt", "CtMz", "MzMz",""), lwd=2)
axis(2, at=seq(0,1, by=0.2), las=2, cex.axis=1, lwd=2)
The problem is that the whole graph looks skew and the data are located on the wrong places. Anyone got a clue what might be the problem? The big box plots are supposed to be located above the CtMz and MzMz and the small ones above the MzCt and CtCt.
Thanks in advance
Axel
I am an absolute beginner in R. so this is probably a stupid question.
I have a table like this (csv format):
,1A+,2A+,3A-,3A+,5A-,5A+,6A-,6A+,7A-,7A+
6,4.530309305,5.520356001,3.437626731,5.146758132,,4.355022819,,4.191337618,,4.076583859
10,8.697814022,9.765817956,,9.636004092,3.725756716,8.600484774,3.457423715,8.358842335,2.246622784,7.244668991
12,,,8.176341701,,,,,,,
17,,,,,6.24785396,,5.077069513,,3.137524578
I want to create a line graph in R plotting all the different Y values (1A+, 2A+, etc) vs the Y values (6,10,12,17).
I am doing:
new_curves <- read.csv("new_curves_R.csv", as.is = TRUE)
g_range <- range(0,new_curves$X)
axis(2, las=1, at=4*0:g_range[2])
plot(new_curves$X1A.,new_curves$X,type="o", col="blue")
legend(1, g_range[2], c("new_curves$X1A."), cex=0.8, col=c("blue"));
title(xlab="Days", col.lab=rgb(0,0.5,0))
title(ylab="Total", col.lab=rgb(0,0.5,0))
However, this (obviously) only plots the first datapoint. (the legend is not working for some reason either). I am guessing I need some sort of for loop to add each Y value to the graph recursively. Likewise, a loop would be needed to make the legend.
thanks
dat <- read.table(text=", 1A+,2A+,3A-,3A+,5A-,5A+,6A-,6A+,7A-,7A+
6,4.530309305,5.520356001,3.437626731,5.146758132,,4.355022819,,4.191337618,,4.076583859
10,8.697814022,9.765817956,,9.636004092,3.725756716,8.600484774,3.457423715,8.358842335,2.246622784,7.244668991
12,,,8.176341701,,,,,,,
17,,,,,6.24785396,,5.077069513,,3.137524578", header=TRUE, sep=",", fill=TRUE)
matplot(dat[1], dat[-1])
I want to create a contour of variable z with the x,y,z data. However, it seems like we need to provide the data in increasing order.
I tried to use some code but it gave me the error.
I tried the following code: Trial 1:
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
x <- age2100$x
y <- age2100$y
z <- age2100$z
contour(x,y,z,add=TRUE,col="black")
I got the following error
Error in contour.default(x, y, z, add = TRUE, col = "black") : increasing 'x' and 'y' values expected
I then tried to use ggplot2 to create the contour. I used the following code:
library("ggplot2")
library("MASS")
library("rgdal")
library("gpclib")
library("maptools")
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
v <- ggplot(age2100, aes(age2100$x, age2100$y,z=age2100$z))+geom_contour()
v
I got the following error:
Warning message:
Not possible to generate contour data
Please find the data on the following location https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv
Can anybody tell me how to create the contour data from the third variable (z) from the temp.csv ? I need to do these many times so I am trying to do on R instead of Arcgis.
Here is an example of how one interpolates using interp from the akimapackage:
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
x <- age2100$x
y <- age2100$y
z <- age2100$z
require(akima)
fld <- interp(x,y,z)
par(mar=c(5,5,1,1))
filled.contour(fld)
Here is an alternate plot using the imagefunction (this allows some flexibility to adding lower level plotting functions (requires the image.scale function, found here):
source("image.scale.R") # http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html
x11(width=5, height=6)
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(4,1), height=6, respect=TRUE)
layout.show(2)
par(mar=c(4,4,1,1))
image(fld)
contour(fld, add=TRUE)
points(age2100$x,age2100$y, pch=".", cex=2)
par(mar=c(4,0,1,4))
image.scale(fld$z, xlab="", ylab="", xaxt="n", yaxt="n", horiz=FALSE)
box()
axis(4)
mtext("text", side=4, line=2.5)