Image plot function with aspect ratio 1 and ticks in axes - r

Given a squared matrix M, how can we plot it so that the aspect ratio is 1 and the axes are shown correctly?
I'm looking for this:
M <- t(replicate(50,sample(50, replace=TRUE)))
image(1:dim(M)[1], 1:dim(M)[1], M, col= gray((0:32)/32), asp=1)
But with the axes re-scaled to fit the image.

To change values at the axis you can use the axis function and add axes=F or xaxt="n" in the image function to suppress the axis and/or the tick labels first.
image(1:dim(M)[1], 1:dim(M)[1], M, col= gray((0:32)/32), asp=1)
axis(1,1:50,1:50) # add a new x-axis
axis(2,1:50,1:50, pos=0) # add a y-axis with the coordinate zero.

Related

How do I add custom ranges and points in the axes of a Base R plot?

Let's consider a vector and plot it.
s1 <- sample(100:1000,32,replace = T)
plot(s1)
The plot I get has a Y-Axis that ranges from 0-1000 with points in the intervals of 200 (0,200,400,600,800,1000) and this is happening implicitly.
If I use ylim argument, apparently or to be honest, evidently, I can now have a custom range,
plot(s1,ylim = c(0,1500))
The points on Y Axis now are 0-1500 as indicated but with the points in the intervals of 500 (0,500,1000,1500), this is happening without my control.
My question, how can I have custom points with custom intervals on the X or Y axis?
use axis() to set your limits : on either x, y, or both
s1 <- sample(100:1000,32,replace = T)
plot(s1, yaxt = "n") # `yaxt` prevents y-axis labels to be printed
axis(2, yaxp=c(10, 1000, 10), las=2) # 'las' helps to align the tick mark labels along the axis or perpendicular
# 'yaxp' helps to set the break points you desire. Learn more from ?par

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

Set zero axes in 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).

X axis labels begin at the middle and do not match data points

I either seem to be running in circles or there is something wrong with my data. I want to plot some data and use ?axisto modify the labels on the X axis.
I have two issues however:
The axis labels begin in the center of the x-axis instead of at the beginning
The axis labels do not match the data points in the plot
I would like to have X axis labels ranging from 10 to 90 by 5.
This is the code that I use and came up with so far:
values <- cbind(1:180,1)
l <- list(1:10,11:20,21:30,31:40,41:50,51:60,61:70,71:80,81:90,91:100,101:110,111:120,121:130,131:140,141:150,151:160,161:170,171:180)
# compute mean across the intervals in l
meanqual <- sapply(l, function(x) mean(values[x,1]))
meanqual
plot <- plot(meanqual, type="o", xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5))
If you only give one numeric vector to plot it "assumes" you meant to use the position or index of the values in that vector as the x values, so the plot call plotted meanqual against 1:length(meanqual). If you wanted to plot against the seq() argument you latter used in the axis call you should supply it (or rather something similar in scale with the same length as meanqual) to plot:
plot <- plot(x=seq(5,90,5), y=meanqual, type="o",
xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5), labels=seq(10,90,5))

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

Resources