contours

590 views
Skip to first unread message

Laurie Kell

unread,
Sep 29, 2010, 5:47:01 PM9/29/10
to ggplot2
I am trying to recreate an example in ggplot that was previously done
using interp from Akima, image and contour.

What I would like to do is to specify i) breaks for fill at
predetermined values ii) the fill colour, iii) automatically
interpolate where there are missing values in a grid, iv) plot
contours at predetermined levels which are different from the fill
values and v) label the contour levels within the plot.

I have provided an example of the original plot and my attempt to
replicate it in ggplot2

library(akima)

x<-melt(volcano)
x$value<-x$value-min(x$value)
x$value<-x$value/max(x$value)

x<-x[sample(1:dim(x)[1],100),]

##### plot
t.<-interp(x[,"X1"],x[,"X2"],x[,"value"],
xo=seq(min(x[,"X1"]), max(x[,"X1"]),
length=101),
yo=seq(min(x[,"X2"]),
max(x[,"X2"]),length=101))
image( t.,breaks=c(0,.5,.70,1), col=c("red","yellow","green"))
contour(t.,levels=c(.5,.6,.70,.9), add=T,
col=c("black","grey","black","grey"), lwd=2, method="edge", labcex=1)

#### ggplot
t.<-interp(x[,"X1"],x[,"X2"],x[,"value"],
xo=seq(min(x[,"X1"]), max(x[,"X1"]),
length=101),
yo=seq(min(x[,"X2"]),
max(x[,"X2"]),length=101))
ggplot(data.frame(expand.grid(X1=t.$x,X2=t.$y),z=c(t.$z),value=cut(c(t.
$z),breaks=c(0,.5,.75,1)))) +
geom_tile(aes(X1,X2,fill=value)) +
geom_contour(aes(x=X1,y=X2,z=z))+
scale_fill_manual(values=c("red","yellow","green"),
name="Probability")

Laurie

Andrie de Vries

unread,
Sep 30, 2010, 12:06:33 PM9/30/10
to ggplot2
Laurie

Well done. I have replicated your code, and the two graphs are
virtually identical, except of course the ggplot graph looks
better ;-)

A small observation regarding writing robust code: There is an
interp() function in both the libraries akima and ggplot2. This means
that the code behaves different depending on the order in which the
libraries are loaded. In my case, I loaded ggplot2 after akima, and
the code threw an error. To get round this, I had to modify the call
to interp() to akima::interp()

library(akima)
library(ggplot2)

x<-melt(volcano)
x$value<-x$value-min(x$value)
x$value<-x$value/max(x$value)

x<-x[sample(1:dim(x)[1],100),]

##### plot
t. <- akima::interp(x[,"X1"], x[,"X2"], x[,"value"],
xo=seq(min(x[,"X1"]), max(x[,"X1"]), length=101),
yo=seq(min(x[,"X2"]), max(x[,"X2"]),length=101))
image( t.,breaks=c(0,.5,.70,1), col=c("red","yellow","green"))
contour(t.,levels=c(.5,.6,.70,.9), add=T,
col=c("black","grey","black","grey"), lwd=2, method="edge",
labcex=1)

gt <- data.frame(
expand.grid(X1=t.$x,
X2=t.$y),
z=c(t.$z),
value=cut(c(t.$z),
breaks=c(0,.5,.75,1)))
ggplot(gt) +
geom_tile(aes(X1,X2,fill=value)) +
geom_contour(aes(x=X1,y=X2,z=z))+

scale_fill_manual(values=c("red","yellow","green"),name="Probability")


###

Regards

Andrie

Laurie Kell

unread,
Oct 1, 2010, 5:59:56 PM10/1/10
to ggplot2
Thanks

The problem with interp is something I have also encountered with
ggplot2. This is because ggplot2 exports all functions, even local
ones.. If another package has a function or method of the same name it
will be masked if you load ggplot after the other package, The
solution is for ggplot to use the namespace to export only those
methods that the user needs to see.

What I haven´t worked out how to do is to get ggplot2 to do the
interpolation so that I don´t have to use akima::interp at all. Also
is it possible to specify what contour lines to draw? The default is
uniformily spaced bins but what if you are plotting a likelihood
surface and want to draw the probability levels corresponding to 0.9,
0.99 and 0.999?

Laurie

Jonathan Christensen

unread,
Oct 1, 2010, 7:23:35 PM10/1/10
to Laurie Kell, ggplot2
Laurie,

Re. specifying what contour lines to draw, geom_contour can take a "breaks" option:


ggplot(data.frame(expand.grid(X1=t.$x,X2=t.$y),z=c(t.$z),value=cut(c(t.
$z),breaks=c(0,.5,.75,1)))) +
 geom_tile(aes(X1,X2,fill=value)) +
 geom_contour(aes(x=X1,y=X2,z=z),breaks=c(0,.5,.70,1))+

 scale_fill_manual(values=c("red","yellow","green"),
name="Probability")

Jonathan


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages