I am a new R user and I need some help to setup a secondary legend for a map.
Description:
I plotted a map using the image.plot function in the fields Library with x and y axes indicating the coordinates and a color scale with a legend andicating the attitude as describedd by the code line below:
image.plot(x,y,z,col=greyscale,legend.mar=8.5,xlab="",ylab="",main="Lambert2étendu")
Problem:
I added points the the map indicating the locations of two types of recievers with different color and cex for each type. and I want to add a legend under the map to describe each coloration signification
Thank you for help
Use legend for a secondary legend. Increase the bottom margin and add legend with negative inset, i.e. move away from plot:
library(fields)
x<- 1:10
y<- 1:15
z<- outer( x,y,"+")
# plot with extra margin at bottom (7)
par(mar=c(7,4,4,2)+0.1)
image.plot(x,y,z,col=gray.colors(10), xlab='', ylab='')
# create points
xp = sample(1:10,size=5)
yp = sample(1:10,size=5)
points(xp,yp,pch=21,bg=1:2,cex=1:2)
# add legend (might have to change inset if you resize the plot)
legend('bottom', horiz=T, legend=paste('type', 1:2), pt.cex=1:2, pch=21, pt.bg=1:2, xpd=NA, inset=c(0,-1..))
Related
How can I make a Common legend for multiple plots? As I have plotted multiple plots but each plot is showing a single legend for itself as if I want to remove it and show a command legend.
And I also want to rename xlim to lon and ylim = lat. How can it be possible in image.plot?
This is my code
set.panel()
par(oma=c( 0,0,0,4)) # margin of 4 spaces width at right hand side
set.panel( 2,2) # 2X2 matrix of plots
# now draw all your plots using usual image command
for ( k in 1:4){
image.plot(lon, lat, pr2)
plot(shape,add=TRUE)
image.plot(lon, lat, pr1)
plot(shape,add=TRUE)
}
par(oma=c( 0,0,0,1))# reset margin to be much smaller.
image.plot( legend.only=TRUE, zlim=c(0,2000),horizontal = TRUE)
image.plot tricked into plotting in margin of old setting
set.panel() #
This is my image showing the plot:
I have made a piechart in R with the next code:
#make slices
slices <- c(19, 26, 55)
# Define some colors
colors <- c("yellow2","olivedrab3","orangered3")
# Calculate the percentage for each day, rounded to one decimal place
slices_labels <- round(slices/sum(slices) * 100, 1)
# Concatenate a '%' char after each value
slices_labels <- paste(slices_labels, "%", sep="")
# Create a pie chart with defined heading and custom colors and labels
pie(slices, main="Sum", col=colors, labels=slices_labels, cex=0.8)
# Create a legend at the right
legend("topright", c("DH","UT","AM"), cex=0.7, fill=colors)
But I want the legend next to my piechart. I have also tried the following code: legend("centreright", c("DH","UT","AM"), cex=0.7, fill=colors).
But this does not give me a legend next to my pie chart.
Which code do I have to use to make a legend next to my pie chart in the middle?
You can play with the x and y argument from legend (cf ?legend):
legend(.9, .1, c("DH","UT","AM"), cex = 0.7, fill = colors)
However, a pie chart may not be the best way to represent your data, because our eye is not very good in assessing angles. The only usecase where a pie chart seems reasonable to me is when comparing 2 categories, because due to watches we can assess these proportions rather easily.
I have a series of plots that I want on a single page. I first use the command layout to specify my plot layout:
layout(matrix(c(1,1,2,2,1,1,2,2,3,4,5,6),3,4,byrow=TRUE))
For plot 1, I have something like:
plot(Easting,Northing, pch=16, col=grey(cex.size)) #The cex.size colours my dots according to some value
I now want to draw an inset plot on plot 1, but not move to plot 2 yet.
I tried following the code :
par(fig=c(0.75, 1, 0, 0.25), new = T)
plot(spp.tmp[,1:2], col=cols[spp.tmp[,3]+1], pch=16)
par(fig=c(0,1,0,1))
But this doesnt work, as par(fig()) command overwrites my layout, and the inset plot appears on the bottom corner of my overall figure, not just in the bottom corner of plot 1.
Two options,
You coul try and include the inset within your layout command (if you were to stick with layout
Here is a case where the first plot spans two rows and column, the second is an inset in the bottom right corner of the first. The third plot is below, the same size as the first, but without an inset.
layout( matrix(c(1,1,1,2,3,3,3,3), 4, 2, byrow = TRUE) )
## show the regions that have been allocated to each plot
layout.show(3)
An alternative is to use subplot from the TeachingDemos package
library(TeachingDemos)
layout(matrix(c(1,1,0,2),2,2,TRUE))
plot(1)
subplot(plot(1), x = c(1.2),y=0.8)
plot(2)
This is my hatchet approach using base graphics. Because your messing around with par(), why dont you change around the order in matrix and plot the tricky one last. This way the par settings do not affect any more plots in your layout, if you were to plot the tricky one first. It seems simplistic in this example, but when you have lots of plots and you want an inset in only 1, it works.
##generate some data
x<-rnorm(50)
y<-rnorm(50)
##set the layout
##so your first plot is plotted last
layout(matrix(c(2,2,0,1), 2,2, byrow=T))
#plot 1 is on the bottom right
plot(x,y, col="grey30", xlab="", ylab="")
#plot 2 is across the top
plot(x,y, col="grey30", xlab="", ylab="")
##set par to place the next plot in the existing plotting area
## and use fig to position it
par(fig=c(.65, .95, .55, .85), new = TRUE)
#inset 3rd plot int top plot, this effectively gives you a blank plot to populate
plot(x,y, col="white", xlab="", ylab="")
#and make the background white
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")
##then just add your points afterwards
points(x,y,col="tomato")
When I create the following plot I get unwanted space between the plotting area and the axis (i.e. the white space between the blue box and the x axis. How can I remove this space and make the plotting area flush with the plot axes? Thanks.
xleft<-c(1,2,2.5)
xright<-c(2,2.5,2.75)
ybottom<-c(1,2,2.5)
ytop<-c(2,2.5,2.75)
par(mar = c(15,15,2.75,2.75) + 0.1)
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
#Label position along axes
x.label.position<-(xleft+xright)/2
y.label.position<-(ybottom+ytop)/2
#Labels
x.label<-c("Long species Name1","Long species Name2","Long species Name3")
y.label<-c("Long species Name4","Long species Name5","Long species Name5")
text(par()$usr[1]-0.5,y.label.position,y.label,xpd=TRUE,adj=1)
text(y=par()$usr[3]-0.5,x=x.label.position,x.label,xpd=TRUE,adj=1,srt=90)
par(xpd=TRUE)
legend(-0.1,0,legend=c("Species A","Species B","Species C"),fill=c("blue", "red", "green"))
UPDATE
I tried the suggestion from plannapus with my actual data but can only get the y-axis to behave is there some else in this chunk of code that is adding space to the sides of the plotting area?
quartz("colour.plot")
par(mar=c(15,15,4,2)+0.1)#sets margins of plotting area
#create the data plot
plot(c(0,100), c(0,100), type = "n", main = paste(x,"vs",y," -",depth),xlab=paste("Species composition in remainder ",x),ylab=paste("Species composition in remainder ",y),asp=1,xaxs="i",yaxs="i")
#Add the rectangles
rect(mdf$xleft,mdf$ybottom,mdf$xright,mdf$ytop,col=mdf$colour)
produces
There is an argument in function plot that handles that: xaxs (and yaxs for the y-axis).
As default it is set to xaxs="r" meaning that 4% of the axis value is left on each side. To set this to 0: xaxs="i". See the xaxs section in ?par for more information.
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab", xaxs="i", yaxs="i")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab",axes=F) # ann
axis(1,pos=1)
axis(2,pos=1)
All axes are remove then you can add new axes to the pos you want.
I have a plot that has data that runs into the area I'd like to use for a legend. Is there a way to have the plot automatically put in something like a header space above the highest data points to fit the legend into?
I can get it to work if I manually enter the ylim() arguments to expand the size and then give the exact coordinates of where I want the legend located, but I'd prefer to have a more flexible means of doing this as it's a front end for a data base query and the data levels could have very different levels.
Edit 2017:
use ggplot and theme(legend.position = ""):
library(ggplot2)
library(reshape2)
set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)
df = data.frame(number = 1:5,a,b,c)
df_long <- melt(df,id.vars = "number")
ggplot(data=df_long,aes(x = number,y=value, colour=variable)) +geom_line() +
theme(legend.position="bottom")
Original answer 2012:
Put the legend on the bottom:
set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)
dev.off()
layout(rbind(1,2), heights=c(7,1)) # put legend on bottom 1/8th of the chart
plot(a,type='l',ylim=c(min(c(a,b,c)),max(c(a,b,c))))
lines(b,lty=2)
lines(c,lty=3,col='blue')
# setup for no margins on the legend
par(mar=c(0, 0, 0, 0))
# c(bottom, left, top, right)
plot.new()
legend('center','groups',c("A","B","C"), lty = c(1,2,3),
col=c('black','black','blue'),ncol=3,bty ="n")
You have to add the size of the legend box to the ylim range
#Plot an empty graph and legend to get the size of the legend
x <-1:10
y <-11:20
plot(x,y,type="n", xaxt="n", yaxt="n")
my.legend.size <-legend("topright",c("Series1","Series2","Series3"),plot = FALSE)
#custom ylim. Add the height of legend to upper bound of the range
my.range <- range(y)
my.range[2] <- 1.04*(my.range[2]+my.legend.size$rect$h)
#draw the plot with custom ylim
plot(x,y,ylim=my.range, type="l")
my.legend.size <-legend("topright",c("Series1","Series2","Series3"))
Building on #P-Lapointe solution, but making it extremely easy, you could use the maximum values from your data using max() and then you re-use those maximum values to set the legend xy coordinates. To make sure you don't get beyond the borders, you set up ylim slightly over the maximum values.
a=c(rnorm(1000))
b=c(rnorm(1000))
par(mfrow=c(1,2))
plot(a,ylim=c(0,max(a)+1))
legend(x=max(a)+0.5,legend="a",pch=1)
plot(a,b,ylim=c(0,max(b)+1),pch=2)
legend(x=max(b)-1.5,y=max(b)+1,legend="b",pch=2)
?legend will tell you:
Arguments
x, y
the x and y co-ordinates to be used to position the legend. They can be specified by keyword or in any way which is accepted by xy.coords: See ‘Details’.
Details:
Arguments x, y, legend are interpreted in a non-standard way to allow the coordinates to be specified via one or two arguments. If legend is missing and y is not numeric, it is assumed that the second argument is intended to be legend and that the first argument specifies the coordinates.
The coordinates can be specified in any way which is accepted by xy.coords. If this gives the coordinates of one point, it is used as the top-left coordinate of the rectangle containing the legend. If it gives the coordinates of two points, these specify opposite corners of the rectangle (either pair of corners, in any order).
The location may also be specified by setting x to a single keyword from the list bottomright, bottom, bottomleft, left, topleft, top, topright, right and center. This places the legend on the inside of the plot frame at the given location. Partial argument matching is used. The optional inset argument specifies how far the legend is inset from the plot margins. If a single value is given, it is used for both margins; if two values are given, the first is used for x- distance, the second for y-distance.