[R-SIG-Mac] Question about line type in contour() function (R 2.11.1)

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 6 02:20:16 CEST 2010


On 05/08/2010 7:18 PM, David B. Thompson, Ph.D., P.E., D.WRE, CFM wrote:
> I'm running R 2.11.1 (MacBook Pro and OS X 10.6.4) and am trying to set a line type in the contour() function. What I did was:
> 
>> u<-seq(0.005,0.995,0.005)
>> v<-u
>> p<-rep(0,length(u)*length(u))
>> dim(p)<-c(length(v),length(v))
>> for (i in 1:length(u)) for (j in 1:length(v)) p[i,j]<-u[i]^2+v[j]^2
>> contour(u,v,p)
> 
> This produces a nice contour plot, as expected. However, if I execute:
> 
>> contour(u,v,p,lty=2)
> 
> I still get a solid line drawn.
> 
> I also tried:
> 
>> contour(u,v,p,lty="dashed")
> 
> with the same result (solid line).
> 
> Interestingly, if I load the volcano data from the datasets package and run
> 
>> contour(volcano,lty=2)
> 
> R draws a nice dashed line for the contours.
> 
> I tried the usual suspects (Googled plenty) and have not found a solution to my issue. has anyone else encountered this problem? Any clues for me? I don't mind doing the work, but am at a lack for direction.
> 
> Thanks...
> 

I can reproduce the problem in current R-devel.  It has something to do 
with the grid size; if I change the step size for u to 0.1 instead of 
0.005, p becomes 10 by 10 instead of 199 by 199, and things look fine. 
The volcano dataset is 87 by 61, and changing the code to give a 
rectangle of that size also gives dashes, but not quite so nice ones.

I don't know where this comes from.  Possibilities I'd guess are:

  - The graphics device starts the dashing again in every cell of the 
grid, so you're only seeing the solid parts.  (But I see this in several 
graphics devices, so it's not at the lowest level, and R graphs have no 
problem with curved dashed lines in other contexts.  Still, they might 
use different ways to draw things.)

  - There's some array-overwrite bug, and it only shows up for big 
arrays.  (But it seems like a pretty strangely consistent manifestation 
for that).

If you want to look for a problem in the contour function, it's in 
src/main/plot3d.c.  If you want to look at the lower level graphics 
stuff, try src/main/engine.c for CScliplines or individual device code, 
e.g. GA_Polyline in src/library/grDevices/src/devWindows.c for the 
windows() device.

I don't really know where to start on this, so I'm going to bail out. 
Here's a partial workaround.  Instead of contour(u, v, p, lty=2), do it as

x <- contourLines(u, v, p)
plot(range(u), range(v), type="n")
lapply(x, function(c) lines(c, lty=2))

If you need labelled contour lines, I don't know what to suggest.

Duncan Murdoch



More information about the R-SIG-Mac mailing list