Set zero axes in R - r

How can one force the visualisation of the zero axes y=0, x=0 in R, removing the external ones siding the plot? I want to obtain the same effect that one can have for example in Gnuplot with set xzeroaxis, set yzeroaxis.

You can do this using by suppressing the default axes by using axes=FALSE and then using axis to draw horizontal and vertical lines to represent the axes.
# example plot
plot(-2:2, -2:2, axes=FALSE)
# add yaxis at position zero and rotate labels 45deg
axis(side=2, pos=0, las=1, lty="dashed")
# x axis
axis(side=1, at=c(-2,-1,1,2), pos=0, lty="dashed")
This produces

Actually, I have solved my own question as follows:
f<-function(x) x^3-2*x
# take the axes off first
plot(f,-2,2, axes=FALSE)
# re-set the axes at a given point (pos=0)
axis(1, pos=0, at=c(-2,-1,0,1,2))
axis(2, pos=0, at=c(-4,-2,2,4))
produces the below, which is what I had in mind (labels and the rest can be then adjusted at will).

Related

R: two axes and grid lines

I would like to plot two graphs in the same plotting region with horizontal grid lines. Each side of the grid lines should give the value for one graph or the other. There should be no y-axis.
The grid() function allows me to simply set the number of bins using the ny= argument. How do I get the corresponding labels to the grid lines? Usually, I would use axis(..., lwd=0) to get the labels. However, the function requires label positions with at=c() and does not feature a ny= argument. Is there a way to automatically set the locations from the number of bins?
Based on Miff's hint below, this should solve the problem.
plot(1:10, axes=FALSE, ylim=c(0,10), ylab="")
par(yaxp=c(0, 10, 5))
axis(2, lwd=0, col.axis="gray")
par(new=TRUE)
plot(60:50, axes=FALSE, ylim=c(50,60), ylab="")
par(yaxp=c(50, 60, 5))
axis(4, lwd=0, col.axis="gray")
grid(NA, NULL)
grid() gets its locations for gridlines from axTicks(), which in turn uses numbers from par("yaxp"). If you modify this parameter (rather than explicitly passing it to grid), the result will then apply to both the grid drawn and the axis. For example:
plot(1:10, axes=FALSE)
axis(2) #Default 4 sections between ticks
par(yaxp=c(par("yaxp")[1:2], 7)) #Lets have seven instead
axis(4)
grid() #Grid now matches with right rather than left
Obviously similar works for the x axis.

How to lower step of x-axis in plot function of R?

I'm plotting a curve with 253 pairs of points in R using plot().Below perf contains these pairs of point. The X axis is between 0 and 1 with step equal to 0.2 when it's plotted. Even with increasing pch, lwd and lty the plotted points don't get more separated.
I want to lower step (fore example to 0.05) so that the points would be plotted farther from each other and the user can understand them better. How can I do it in plot() function of R? Is it possible to lower step of X axis or should use another function other than plot()?
plot(perf, add=F,col="black", lty=6, lwd=3, pch=19)
The plotted curve:
One approach is to suppress the drawing of the x axis during the plot, and then add your own custom axis. This can be accomplished by adding xaxt="n" in the call to plot():
plot(perf, add=F,col="black", lty=6, lwd=3, pch=19, xaxt="n")
Next, you can define a vector containing the number of ticks you want, and labels for those ticks. Assuming a step size of 0.05, you would have 20 points:
stepSize <- 0.05
xMax <- 1.0
numTicks <- xMax / stepSize
v1 <- c(0:numTicks)*stepSize
Finally, make a call to axis() to draw the x axis using the tick positions and labels:
axis(side=1, at=v1, labels=v1)
You can use asp aspect ratio parameter to plot function, which is equal to y/x, to adjust the plot ratio.
If you like to control the axis labels you could use
axis(side=1, at=seq(0, 1, by=0.05))
axis(side=2, at=seq(0, 1, by=0.1))

minor.tick() position in R

I've extensively searched for a solution without luck. I'd like to plot functions in the usual way (axes cross at (0,0)) using the default R plotting facilities.
Setting the axes position to 0 as in the example below solve partially the problem. However when adding minor ticks the default position is retained resulting in the weird plot below.
plot.new()
x <- seq(-10,10, 0.1)
f <- ((x+2)*(x-5))/((x-3)*(x+1))
plot.window(xlim=c(-10,10), ylim=c(-30,30))
axis(side=1, at=seq(-10,10,2), pos=0, las=0)
axis(2)
library(Hmisc)
minor.tick(nx=5, ny=10, tick.ratio=0.5)
One solution would be to still use the axis function, and just specify the locations and size (tck) of the minor ticks.
plot.new()
x <- seq(-10,10, 0.1)
f <- ((x+2)*(x-5))/((x-3)*(x+1))
plot.window(xlim=c(-10,10), ylim=c(-30,30))
axis(side=1, at=seq(-10,10,2), pos=0, las=0)
axis(side=1, at=seq(-10,10,0.5), pos=0, las=0, tck=-0.01, labels=FALSE)
axis(2)
This allows you to retain complete control over the plot.

R: Two axis chart adjustments

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()

How to set the origin of coordinates properly in R?

How can i set the origin of coordinates properly in R?
i want the origin of coordinates in my graph,
where x=1 and y=-1 not in common x=0 and y=0 ,please see the attachment.
First of all you should note that R, by default, puts the axes at the side of the plot, not necessarily crossing at [0;0].
What you can do is plotting the graph without axes and add them up later at the desired position using the axis function
x <- -5:5
y <- x
plot(x,y, t="l", xaxt="n", yaxt="n")
axis(1, pos=-1)
axis(2, pos=1, las=1)
See ?axis for more information (you may find the at and labels parameters of axis particularly useful).

Resources