0

I have produced a filled contourplot using contourplot function in the Lattice package. I would like to be able to emphasise one of the contour lines produced by this function, e.g. by changing lwd=2, and de-emphasise the other contour lines produced, e.g. by setting changing the remaining contour line colours to grey.

For example, I would like the equivalent of adding

contourplot(
Male ~ Year * Age, 
data=this.ds, region=F, labels=F, lwd=2, at=c(0, 0.01, 1))

To

contourplot(
Male ~ Year * Age, 
data=this.ds, 
region=T, col.regions=rev(heat.colors(200)), 
cuts=50, main="", sep="") 

I recognise that the answer is likely to involve the functions update, panel, and/or layer (if using latticeExtra), but I my understanding of the underlying logic/structure of lattice objects is not good enough to figure out what to do.

Edit

Thanks for your help. I basically used the first answer, but with the following slight modification to be able to point out a couple of features on the contour line:

require(lattice)
require(latticeExtra)

Part1 <- contourplot(Female ~ Year * Age, 
                     data=this.ds,
                     region=T, 
                     col.regions=rev(heat.colors(200)),
                     cuts=50, 
                     main="", 
                     labels=F,
                     col="grey",
                     sep="")

Part2 <- contourplot(Female ~ Year * Age,
                     data=this.ds,
                     region=F,
                     labels=F,
                     lwd=2,
                     at=c(0, 0.01, 1),
                     panel=function(...){
                     panel.contourplot(...)
                      panel.segments(x0=c(1840, 1900, 2000, 1840), 
                                     x1=c(1900, 1900, 2000, 2000), 
                                     y0=c(40.5, 0, 0, 64), 
                                     y1=c(40.5, 40.5, 64, 64), 
                                     lty="dashed")
                     }                    
)


Plot.Final <- Part1+ Part2 

print(Plot.Final)
Community
  • 1
  • 1
JonMinton
  • 1,239
  • 2
  • 8
  • 26
  • Please share some code with us so we can use it to help you (reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – statquant Aug 22 '13 at 12:54
  • sadly that doesn't work. thanks, though – JonMinton Aug 22 '13 at 13:21

2 Answers2

2

Load the latticeExtra library. Then:

Part1<-contourplot(
    Male ~ Year * Age, 
    data=this.ds, 
    region=T, col.regions=rev(heat.colors(200)), 
    cuts=50, main="", sep="")

Part2<-contourplot(
    Male ~ Year * Age, 
    data=this.ds, region=F, labels=F, lwd=2, at=c(0, 0.01, 1))

Plot.Final<-Part1+Part2

That should combine the graphs as you wish. If needed you may need to set xlim, ylim and labels so that everything looks OK.

John Paul
  • 12,196
  • 6
  • 55
  • 75
0

Use contour instead of contourplot.

bar<-matrix(runif(100,nrow=10))
contour(bar)
contour(bar,levels = 0.2, lwd=3, add=TRUE)

Edit: turns out you don't even need to do that. Unlike contourplot, contour allows yo uto specify a vector of line widths (or colors, or whatever), so contour(bar,lwd=c(2,1,1,1,1,)) will suffice.

Edit 2: My apologies: this is in the graphics package.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Thanks. I've already got too much of my code using lattice to able to start from scratch, unfortunately. – JonMinton Aug 22 '13 at 14:19