Menu

[r193]: / trunk / R / generic.scatter.plot.R  Maximize  Restore  History

Download this file

58 lines (55 with data), 1.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
p.hist <- 1/10
p.scatter <- 1-p.hist
space <- 4
hist.breaks <- 10
# Thanks to Paul Murrell for marginal histogram inspiration
# http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78
generic.scatter.plot <- function(x,y,ann=NULL,pch=1,
fit.lty=0,axis.round=2,
lty.x.y=0,one.to.one=F,
marginal=T,
xlab='Label this with the "xlab" argument',
ylab='Label this with the "ylab" argument',
main='Label this with the "main" argument',
xlim=NULL,
ylim=NULL
) {
if(marginal) {
layout(matrix(c(2,0,1,3),ncol=2,byrow=TRUE),widths=c(p.scatter,p.hist),heights=c(p.hist,p.scatter))
} else {
par(mfrow=c(1,1))
}
par(mar=c(space,space,space,space))
if(one.to.one) {
xi <- yi <- range(x,y)
} else {
xi <- x
yi <- y
}
if(!is.null(xlim))xi <- xlim
if(!is.null(ylim))yi <- ylim
plot(xi,yi,type='n',bty='n',xaxt='n',yaxt='n',xlab=xlab,ylab=ylab,main=main)
axis(1,round(quantile(x),axis.round),lty=0,las=3)
axis(2,round(quantile(y),axis.round),lty=0,las=1)
if(fit.lty) {
fit <- lm(y ~ x)
abline(fit,lty=fit.lty)
}
abline(0,1,lty=lty.x.y)
if(!is.null(ann)) {
text(x,y,ann,xpd=NA)
} else {
points(x,y,pch=pch)
}
mtext(paste('n =',length(x)))
if(marginal) {
xhist <- hist(x,seq(min(xi),max(xi),l=hist.breaks),plot=FALSE)
yhist <- hist(y,seq(min(yi),max(yi),l=hist.breaks),plot=FALSE)
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(0,space,1,space))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
par(mar=c(space,0,space,1))
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
}
}
##debug(generic.scatter.plot)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.