Hi all. I'm ploting a raster and I can't find the proper way to move the legend. For example, r = raster(system.file("external/test.grd", package="raster"))plot(r) How can I put the legend at the desired position? Thank in advance,Phil [[alternative HTML version deleted]]
See ?legend . you can add a legend directly to an existing plot. An example: legend('topright',c('hot','cold'),lty=1,col=c('red','green'),bg='white') Now if you're trying to place the legend outside the plot area (i.e. in some other part of the window), you'll need to invoke par(xpd=TRUE) . See the help at ?par . Filoche wrote> Hi all. > I'm ploting a raster and I can't find the proper way to move the legend. > For example, > r = raster(system.file("external/test.grd", package="raster"))plot(r) > How can I put the legend at the desired position? > Thank in advance,Phil > [[alternative HTML version deleted]] > > ______________________________________________> R-help@> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- View this message in context: http://r.789695.n4.nabble.com/legend-position-tp4681489p4681492.html Sent from the R help mailing list archive at Nabble.com.
It occurs to me that perhaps you're referring to the 'color bar' on the right of the plot. AFAIK you cannot get at that from the raster::plot method. However lattice::levelplot does allow you to manipulate or remove that colorbar. -- View this message in context: http://r.789695.n4.nabble.com/legend-position-tp4681489p4681497.html Sent from the R help mailing list archive at Nabble.com.
It is not straightforward unless you want the legend in the right or the bottom margins. To put the legend inside the plot region it is simplest to use image() to plot the raster file and then image.plot(legend.only=TRUE) to add the legend. In addition to reading the help page for plot{raster}, you also need the pages for image{raster} and image.plot{fields}. Here are two simple examples. image(r, col=rev(terrain.colors(255))) plot(r, horizontal=TRUE, smallplot=c(.15, .5, .84, .86), legend.only=TRUE) image(r, col=rev(terrain.colors(255))) plot(r, smallplot=c(.15, .17, .5, .85), legend.only=TRUE) ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of philippe massicotte Sent: Monday, December 2, 2013 1:22 PM To: r-help at r-project.org Subject: [R] legend position Hi all. I'm ploting a raster and I can't find the proper way to move the legend. For example, r = raster(system.file("external/test.grd", package="raster"))plot(r) How can I put the legend at the desired position? Thank in advance,Phil [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.