Hi R-Help, I would like to make filled contour maps of ocean data overlaid by costlines from the map package. I can draw the filled contours and the coastlines om the same plot, but the filled contour also covers part of the land. To get rid of that I tried to draw a filled coastline map on top of the filled contour, but the filled map only draws the closed contours - so most of the land is missing. Example: map("worldHires",xlim=c(0,15),ylim=c(50,60)) #Draw relevant region (North Sea and Denmark waters) map("worldHires",xlim=c(0,15),ylim=c(50,60),fill=TRUE) # This only draws Denmark and Holland (Sweden, uk, Germany etc. disappears because they are not closed polygons). Any hint on how to fix this problem or a different approach is most welcome. Janus
Hi, Some time ago, Roger Peng posted this solution, which I found very useful: junk.mat <- matrix(rnorm(1600), 16, 100) contour.mat <- ifelse(junk.mat < 2, 0, junk.mat) filled.contour(junk.mat, color = terrain.colors, plot.axes = contour(contour.mat, levels = 1, drawlabels = FALSE, axes = FALSE, frame.plot = FFALSE, add = TRUE)) The 'plot.axes' argument to filled.contour() gives you access to the coordinate system in the actual plotting area. However, you will notice that the axes are missing. You need to add them explicitly, as in: filled.contour(junk.mat, color = terrain.colors, plot.axes = { contour(contour.mat, levels = 1, drawlabels = FALSE, axes = FALSE, frame.plot = FFALSE, add = TRUE); axis(1); axis(2) } ) Cheers, Antonio> -----Mensaje original----- > De: r-help-bounces+arv=ono.com at stat.math.ethz.ch > [mailto:r-help-bounces+arv=ono.com at stat.math.ethz.ch]En nombre de Janus > Larsen > Enviado el: martes, 03 de febrero de 2004 9:36 > Para: R-help at stat.math.ethz.ch > Asunto: [R] filled maps > > > Hi R-Help, > > I would like to make filled contour maps of ocean data overlaid by > costlines from the map package. > I can draw the filled contours and the coastlines om the same plot, but > the filled contour also covers part of the land. To get rid of that I > tried to draw a filled coastline map on top of the filled contour, but > the filled map only draws the closed contours - so most of the land is > missing. > Example: > map("worldHires",xlim=c(0,15),ylim=c(50,60)) #Draw relevant region > (North Sea and Denmark waters) > map("worldHires",xlim=c(0,15),ylim=c(50,60),fill=TRUE) # This only draws > Denmark and Holland (Sweden, uk, Germany etc. disappears because they > are not closed polygons). > > Any hint on how to fix this problem or a different approach is most > welcome. > Janus > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html --- Incoming mail is certified Virus Free. ---
On Tue, 3 Feb 2004, Janus Larsen wrote:> Hi R-Help, > > I would like to make filled contour maps of ocean data overlaid by > costlines from the map package. > I can draw the filled contours and the coastlines om the same plot, but > the filled contour also covers part of the land. To get rid of that I > tried to draw a filled coastline map on top of the filled contour, but > the filled map only draws the closed contours - so most of the land is > missing. > Example: > map("worldHires",xlim=c(0,15),ylim=c(50,60)) #Draw relevant region > (North Sea and Denmark waters) > map("worldHires",xlim=c(0,15),ylim=c(50,60),fill=TRUE) # This only draws > Denmark and Holland (Sweden, uk, Germany etc. disappears because they > are not closed polygons).Looks as though you can use the regions= argument:> res <- map("worldHires",xlim=c(0,15),ylim=c(50,60), plot=FALSE,+ namesonly=TRUE)> res[1] "Denmark" "USSR" [3] "Netherlands" "Netherlands:IJsselmeer" [5] "France" "Czechoslovakia" [7] "Poland" "Germany" [9] "Luxembourg" "Belgium" [11] "Norway" "Sweden" [13] "Germany:Usedom" "Netherlands:South" [15] "Germany:Langeoog" "Denmark:Mors" [17] "Denmark:Mon" "Lake Fjerritslev" [19] "UK:Great Britain" "Denmark:Samso" [21] "Netherlands:Ameland" "Netherlands:Terschelling" [23] "Denmark:Als" "Denmark:Sjaelland" [25] "Denmark:Fyn" "Netherlands:Schiermonnikoog" [27] "Denmark:Laeso" "Sweden:Orust" [29] "Germany:Borkum" "Denmark:Lolland" [31] "Netherlands:Texel" "Netherlands:Flevoland" [33] "Norway:Sunnhordland" "UK:Isle of Sheppey" [35] "Denmark:Langeland" "Denmark:Bornholm" [37] "Germany:Fehmarn" "Germany:Rugen" [39] "Germany:Norderney" "Norway:Karmoy"> map("worldHires",xlim=c(0,15),ylim=c(50,60)) > map("worldHires",regions=res, fill=TRUE, add=TRUE)fixes it for me. Curiously, the longitudes add 2.5 degrees on each side. Roger PS. For film viewers who enjoyed "Good bye, Lenin!", we still provide region="USSR" above!> > Any hint on how to fix this problem or a different approach is most > welcome. > Janus > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Econonic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway, voice: +47-55959355, fax: +47-55959393; Roger.Bivand at nhh.no
Hi Janus, Try this: filled.contour(x,y,corr.map2, color = terrain.colors, plot.axes = { contour(x,y,corr.map2,nlevels=20, drawlabels = T, frame.plot = FFALSE, add = TRUE); axis(1); axis(2); world(col="red",add=T,lwd=3)}, key.title = title(main="values"), xlab="longitude",ylab="latitude" ) Antonio> -----Mensaje original----- > De: Janus Larsen [mailto:janus at ices.dk] > Enviado el: martes, 03 de febrero de 2004 11:36 > Para: antonio rodriguez > Asunto: RE: [R] filled maps > > > Hi Antonio, > > Thanks for your prompt answer - but I can't see how that will solve my > problem (which is overlaying contour plots with filled coastlines from > the mapdata package). > As far as I can see, what you show is how to add contour lines to a > filled contour plot.. Or am I missing something? > > Cheers, > Janus > > -----Original Message----- > From: antonio rodriguez [mailto:arv at ono.com] > Sent: 03 February 2004 11:28 > To: Janus Larsen; R-help at stat.math.ethz.ch > Subject: RE: [R] filled maps > > > Hi, > > Some time ago, Roger Peng posted this solution, which I found very > useful: > > junk.mat <- matrix(rnorm(1600), 16, 100) > contour.mat <- ifelse(junk.mat < 2, 0, junk.mat) > filled.contour(junk.mat, color = terrain.colors, > plot.axes = contour(contour.mat, levels = 1, > drawlabels = FALSE, axes = FALSE, > frame.plot = FFALSE, add = TRUE)) > > The 'plot.axes' argument to filled.contour() gives you access to the > coordinate system in the actual plotting area. However, you will notice > that the axes are missing. You need to add them explicitly, as in: > > filled.contour(junk.mat, color = terrain.colors, > plot.axes = { contour(contour.mat, levels = 1, > drawlabels = FALSE, axes = FALSE, > frame.plot = FFALSE, add = TRUE); > axis(1); axis(2) } ) > > > Cheers, > > Antonio > > > -----Mensaje original----- > > De: r-help-bounces+arv=ono.com at stat.math.ethz.ch > > [mailto:r-help-bounces+arv=ono.com at stat.math.ethz.ch]En nombre de > > Janus Larsen Enviado el: martes, 03 de febrero de 2004 9:36 > > Para: R-help at stat.math.ethz.ch > > Asunto: [R] filled maps > > > > > > Hi R-Help, > > > > I would like to make filled contour maps of ocean data overlaid by > > costlines from the map package. I can draw the filled contours and the > > > coastlines om the same plot, but the filled contour also covers part > > of the land. To get rid of that I tried to draw a filled coastline map > > > on top of the filled contour, but the filled map only draws the closed > > > contours - so most of the land is missing. > > Example: > > map("worldHires",xlim=c(0,15),ylim=c(50,60)) #Draw relevant region > > (North Sea and Denmark waters) > > map("worldHires",xlim=c(0,15),ylim=c(50,60),fill=TRUE) # This only > draws > > Denmark and Holland (Sweden, uk, Germany etc. disappears because they > > are not closed polygons). > > > > Any hint on how to fix this problem or a different approach is most > > welcome. Janus > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.571 / Virus Database: 361 - Release Date: 26/01/2004 > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.571 / Virus Database: 361 - Release Date: 26/01/2004 > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.571 / Virus Database: 361 - Release Date: 26/01/2004 >---
> Date: Tue, 3 Feb 2004 11:34:06 +0100 (CET) > From: Roger Bivand <Roger.Bivand at nhh.no> > > On Tue, 3 Feb 2004, Janus Larsen wrote: > > > Hi R-Help, > > > > I would like to make filled contour maps of ocean data overlaid by > > costlines from the map package. > > I can draw the filled contours and the coastlines om the same plot, but > > the filled contour also covers part of the land. To get rid of that I > > tried to draw a filled coastline map on top of the filled contour, but > > the filled map only draws the closed contours - so most of the land is > > missing. > > Example: > > map("worldHires",xlim=c(0,15),ylim=c(50,60)) #Draw relevant region > > (North Sea and Denmark waters) > > map("worldHires",xlim=c(0,15),ylim=c(50,60),fill=TRUE) # This only draws > > Denmark and Holland (Sweden, uk, Germany etc. disappears because they > > are not closed polygons). > > Looks as though you can use the regions= argument: > > > res <- map("worldHires",xlim=c(0,15),ylim=c(50,60), plot=FALSE, > + namesonly=TRUE) > > map("worldHires",xlim=c(0,15),ylim=c(50,60)) > > map("worldHires",regions=res, fill=TRUE, add=TRUE) >Thanks Roger, I hadn't got around to figuring out that workaround. Certainly the mapgetg code explicitly excludes incomplete polygons if fill=TRUE, but since the polygon() function is happy to clip what it is filling, your workaround takes advantage of that. I'll have a look at getting mapgetg to do this itself - it would have to always provide complete polygons if fill=TRUE instead of just 'visible' polylines.> fixes it for me. Curiously, the longitudes add 2.5 degrees on each side. >Actually this is to preserve aspect ratio. An earlier version of the maps package adjusted the shape of the figure to conform to both aspect ratio and imposed limits. I didn't deliberately change this, but I think I know why it has happened, so I'll look at this also. Ray Brownrigg