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.
Related
I was trying to plot a climate diagram and ran into the following problem:
After using barplot(...) for precipitation I superimposed another plot for the temperature. It is necessary for climate diagrams that the two y-axes (mm, °C) align at zero and that the precipitation/temperature ratio is 2:1 (e.g. 20mm precipitation corresponds to 10°C).
The problem: barplot(...) draws the axis to the plot's box while plot(...) leaves some space between the box and the axis margins.
Here is a simplified example. From the grid lines you see that the 0-values do not align:
barplot(0:10)
grid(col=1)
par(new=TRUE)
plot(0:10, xlim=c(-2,14), axes=FALSE)
axis(4,at=c(0:10), labels=c(0:10))
How can I get the right position and scaling of the two axes?
Don't use par(new = TRUE):
barplot(0:10)
grid(col=1)
lines(0:10, type = "p")
axis(4,at = c(0:10), labels = seq(0,20,2))
The function lines() is the right one here. The argument type = p is needed to plot points.
You need to adjust the y-values for the temperature, but now the second y-axis is in the right way, I think.
My code:
plot(factor(tData$Interval), tServiceLev, col = "red", type = "l", main =
'Service Level and Call Volume', xlab = "Time Intervals", ylab = "Service Level")
lines(factor(tData$Interval), tServiceLev, type = "l", col = "blue",lwd = 2)
par(new=TRUE)
plot(factor(tData$Interval), tData$Offered, type = "l", axes=FALSE, col = "green")
axis(4)
mtext("Call Volume", 4,1.5)
lines(factor(tData$Interval), tData$Offered, col = "red", lwd=2)
legend("topright", col=c("blue","red"), lty = 1, legend = c("Service Level", "Call Volume"), cex = .85)
When I put on the second plot, it overwrites the left axis completely. Also is there a way to make the actual graph larger and the axis labels closer to the axis range values?
Edit: This is my new graph after I knitted rmd. Call Volume is cut off. I want the time intervals to be as shown, but coloring each data point does not work. I guess part of the confusion is working with factored data which I've never worked with before.
Within the plot() calls, you need to set axes=FALSE. Then you can use axis() subsequently to customize the axes as you wish (using side=1 for bottom, side=2 for left axis).
For the other adjustments, try preceding plot with a call to dev.new(width=8, height=8), for example, launching an 8" by 8" device. Then using par(mai=c(2,2,1,1)) you can set 2" margins at bottom and left, 1" at top and right. Adjust those numbers as needed to get the proper size.
When calling axis, use axis(..., labels=FALSE) to make the axis with ticks, etc. Then print the actual labels using mtext. The lines=.. option within mtext will allow you to set the distance off the axis that the labels plot.
Update: Again, have you tried setting more space for the margin using par(mai=c(bottom,left,top,right)) as I suggested above? Using a larger number for right should give more space for the axis label. Also, try using type='o' to plot makers and lines. Still not understanding the change you'd like to make to the x axis.
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..))
I am trying to plot a chat with two axis, here is the code and attached is the plot,
I have to do two adjustments to it.
I want to plot a line with dots and dots should be middle of the bars
Adjusting right side axis(i.e axis(4)) tick marks should align with left side axix(i.e axis(2))
Code:
Region=c("North","South","East","West")
Sales=sample(500:1000,4)
Change=sample(1:10,4)/10
names(Sales)=Region
names(Change)=Region
barplot(Sales,ylim=c(0,1000))
par(new=T)
plot(Change,type="b",axes=F,ylim=c(0,1))
axis(4)
box()
Regards,
Sivaji
First, save your barplot as some object. So you will get coordinates of the middle points. Then to add line you can use also function lines() and just multiply Change values with 1000.
Then for axis() function supply at= values and labels= the same as at=, just divided by 1000.
x<-barplot(Sales,ylim=c(0,1000))
lines(x,Change*1000,type="b")
axis(4,at=seq(0,800,200),labels=seq(0,800,200)/1000)
You need to play to set the same x-axis in the second plot, you get this info from par("usr").
The xaxs="i" is to set the xlim exactly, by default R increase the xlim a bit to make it better looking.
par(mar=c(5,5,2,5)) # change margins
x = barplot(Sales, ylim=c(0,1000)) # barplot, keep middle points of bars
mtext("Sales", 2, line=3) # first y-axis label
xlim = par("usr")[1:2] # get xlim from plot
par(new=TRUE)
plot.new() # new plot
plot.window(xlim=xlim, ylim=c(0,1), xaxs="i", yaxs="i") # new plot area, same xlim
lines(x,Change,type="b") # the lines in the middle points
axis(4) # secondary y-axis
mtext("Change", 4, line=3) # secondary y-axis label
box()
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")