R - xlim sequential increase - r

Is it possible to increase the values in the X-axis with 1? For example - 1,2,3,4,5 etc.
Right now I use this:
xlim=c(1,16)
And the result is:
Which doesn't look nice, the ideal would be to have a sequential increase with 1 - from 1 to 16, since I have 16 values for the X-axis.

xlim can be finely controlled with axis. To make it clear, I will reproduce one plot without axis control and one instead where we performed a modification on the scale.
x <- rnorm(100, 10, 2)
y <- rnorm(100, 10, 2)
par(mfrow = c(1, 2))
Plot 1 is produced without axis control
plot(x, y, main = "Plot 1")
In Plot 2 we set a demonstrative xlim and ylim that produce a scale from 0 to 20 for both axes. We can more finely tune it with axis: to make an example, I create a scale by 1 for axis x and by 5 for axis y
plot(x, y, xlim = c(0, 20), ylim = c(0, 20), main = "Plot 2")
axis(1, at=seq(0, 20, 1))
axis(2, at=seq(0, 20, 5))
That's not all. axis allow a really fine work on your plot axis with its arguments.
axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)
side
an integer indicating the side of the graph to draw the axis (1=bottom, 2=left, 3=top, 4=right)
at
a numeric vector indicating where tic marks should be drawn
labels
a character vector of labels to be placed at the tickmarks (if NULL, the at values will be used)
pos
the coordinate at which the axis line is to be drawn. (i.e., the value on the other axis where it crosses)
lty
line type
col
the line and tick mark color
las
labels are parallel (=0) or perpendicular(=2) to axis
tck
length of tick mark as fraction of plotting region (negative number is
outside graph, positive number is inside, 0 suppresses ticks, 1 creates gridlines) default is -0.01

Related

R Barplot: Y-axis cut off at the top?

I'm trying to use R to do a barplot. Values I'm plotting range from 0 to 5.0, but are decimal values (such as 4.87) so I don't want to just use the default Y axis, because it just goes up in increments of 1.
I've created a custom Y axis, which works, but if I set the maximum value greater than about 4.5, it cuts off the tickmark at the top of the axis. This looks untidy so I want a way to ensure this tickmark will always appear, but I don't want to shorten my axis as it looks stupid if I do this.
My R code is as follows:
# Bar plot of mean SUS question scores
barplot(meanSUSQuestions$Mean,
main="Mean SUS Question Scores",
cex.main="0.8",
cex.axis="0.8",
cex.lab="0.8",
#names=c("q1", "q2", "q3","q4","q5","q6","q7","q8","q9","q10"),
names=c(1:10),
yaxt="n",
col="red")
axis(2, cex.axis="0.8", at=seq(0, 5, 0.5)) # Create custom Y axis
mtext(text="Mean Score", side=2, line=2, cex=0.8)
mtext(text="Question", side=1, line=2, cex=0.8)
The bar plot that this produces looks like this:
As you can see from the picture, the top tickmark is missing.
How can I get this top tickmark to appear?
barplot generates the image height based on the data. The range of your manual y-axis is considerably larger than the plot area and is thus cut off.
The easiest way to solve the issue in your specific case is to add an yaxp = c(0, 5, 11) to barplot instead of yaxt = "n" and axis.
A self-contained example:
# Bad
x <- 1:5
barplot(x, yaxt = "n") #, add = TRUE)
axis(2, at = seq(0, 6, 2)) # Create custom Y axis
# Good
barplot(x, yaxp = c(0, 6, 2))

Align the primary and secondary y-axis on the common base, set points in the center of bars?

I am trying to display barchart overlayed with line plot on secondary y-axis. I was following example here: http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/. I successfully display my data, however the beginning of the y1 and y2 axis do not start on the common base (on the common 0), the y2 is located further up.
How to correctly align y1 and y2 axes on the common basis? Can I extent both of my y1 and y2 axis in the same size? And, how can I adjust the position of the points in the middle of the bars?
My dummy data:
x <- 1:5
y1 <- c(10,53,430,80,214)
y2 <- c(0.2,1.2,3.3, 3.5, 4.2)
# create new window
windows()
# set margins
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
barplot(y1, ylim= c(0,500))
mtext("y1",side=2,line=3)
# add plot with secondary (y2) axis
par(new=TRUE)
plot(x, y2,,type="b",col="red",xaxt="n",yaxt="n",xlab="",ylab="", ylim= c(0,10), lwd = 2, lty = 2, pch = 18)
axis(4)
mtext("y2",side=4,line=3)
When you check the documentation for par() you will find the options xaxsand yaxs with which you can control the interval calculation for both axes. Calling par(yaxs = 'i') prior to your plot() command or using the option directly as an argument to plot() will change the interval calculation in the following way:
Style "i" (internal) just finds an axis with pretty labels that fits
within the original data range.
Additional information for the TO concerning his comment:
In order to center the points of the line go with lines instead and you can use the x-axis created by barplot:
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
par(xpd = F)
ps <- barplot(y1, ylim= c(0,500), xpd = F)
axis(4, at = 0:5 * 100, labels = 0:5 * 2) # transform values
mtext('y1',side = 2, line = 3)
lines(x = ps, y = y2 * 50, type = 'b', col = 'red') # transform values

Rotate axis label upside down in R base plot

I need some help with axis labels in base R plotting, thanks in advance for any guidance!
What I need:
In R base plot() I would like to rotate my axis(3, ...) label to -90 degrees to get the following output:
(note that I have rotated the pic outside R)
Why I need it (big picture):
I am using labcurve for curve annotation and strangely enough for my data the annotation results are visually waay better if applied to the -90 degree rotated graph. After running labcurve I can rotate the resulting R-generated PDF back 90 degrees in LaTeX.
What I have tried:
#1
I know that this is governed by the las option in par with the following options:
0: always parallel to the axis [default],
1: always horizontal,
2: always perpendicular to the axis,
3: always vertical.
However, these four options available only cover the two angles 0 and 90 degrees as either of the following:
plot(x=c(0,10), y=c(0,1), type='n', xlab='',ylab='', axes=FALSE)
lines(x=c(0,7,7,10), y=c(0,0.33,0.67,1))
axis(2, at=c(0,1), labels=c('',''), las=2)
xlabels <- c('0','10')
axis(3, at=c(0,10), labels=xlabels, las=0)
or
axis(3, at=c(0,10), labels=xlabels, las=1)
axis(3, at=c(0,10), labels=xlabels, las=2)
or
axis(3, at=c(0,10), labels=xlabels, las=3)
#2:
One could think of str but according to the doc:
Note that string/character rotation via argument srt to par does not
affect the axis labels.
Thanks again!
The general procedure for creating rotated axis labels is described in R FAQ 7.27. Here's a modified example which hopefully suits your needs.
# some toy data
x <- c(0, 2, 6, 10)
y <- sample(1:4)
# Increase top margin to make room for rotated labels
par(mar = c(5, 4, 7, 2) + 0.1)
# Create plot without axis or labels
plot(x, y, type = "l", axes = FALSE, xlab = "", ylab = "")
# positions for tick marks
atx <- range(x)
aty <- range(y)
# x axis without labels
axis(side = 3, at = atx, labels = FALSE)
# y axis without labels
axis(side = 2, at = aty, labels = FALSE)
# add -90 rotated x axis labels
text(x = atx, y = par("usr")[4] + 0.25, srt = -90, adj = 1,
labels = atx, xpd = TRUE)

Increase the length of plot tick marks

How can I increase the length of plot tick marks? Here is a small example:
r <- as.POSIXct(round(range(time), "mins"))
plot(time, x, t="l", xaxt = "n")
axis.POSIXct(1, at = seq(r[1], r[2], by = "min"), format = "%H:%M:%S")
which gives
As you can see, all the ticks are the same size. Is there a way to automatically increase the length of those ticks that are signed?
When creating a very specific axis layout, you typically need to add the axis after drawing the plot. Since you didn't have a reproducible example, I've created my own data set.
Create a plot, but don't display the axis
plot(1:10, axes=FALSE, frame=TRUE)
Add in the x-scale. In this example, values 1,2,3, ...., 10. The argument tck specifies the tick length:
##The tck value should be smaller here
axis(1, 1:10, tck=-0.05)
Now add in an additional scale for "in-between" values. I've set labels="", so we don't print any values:
axis(1, seq(0.5, 9.5, 1), labels=rep("", 10), tck=-0.01)
This gives:

How to plot the value of abline in R?

I used this code to make this plot:
plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16,
panel.last=abline(h=67,v=1.89, lty=1,lwd=3))
My plot looks like this:
1.) How can I plot the value of the ablines in a simple plot?
2.) How can I scale my plot so that both lines appear in the middle?
to change scale of plot so lines are in the middle change the axes i.e.
x<-1:10
y<-1:10
plot(x,y)
abline(a=1,b=0,v=1)
changed to:
x<-1:10
y<-1:10
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
by "value" I am assuming you mean where the line cuts the x-axis? Something like text? i.e.:
text((0), min(y), "number", pos=2)
if you want the label on the x axis then try:
abline(a=1,b=0,v=1)
axis(1, at=1,labels=1)
to prevent overlap between labels you could remove the zero i.e.:
plot(x,y,xlim=c(-30,30),yaxt="n")
axis(2, at=c(1.77,5,10,15,20,25))
or before you plot extend the margins and add the labels further from the axis
par(mar = c(6.5, 6.5, 6.5, 6.5))
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
axis(2, at=1.77,labels=1.77,mgp = c(10, 2, 0))
Similar in spirit to the answer proposed by #user1317221, here is my suggestion
# generate some fake points
x <- rnorm(100)
y <- rnorm(100)
# positions of the lines
vert = 0.5
horiz = 1.3
To display the lines at the center of the plot, first compute the horizontal and vertical distances between the data points and the lines, then adjust the limits adequately.
# compute the limits, in order for the lines to be centered
# REM we add a small fraction (here 10%) to leave some empty space,
# available to plot the values inside the frame (useful for one the solutions, see below)
xlim = vert + c(-1.1, 1.1) * max(abs(x-vert))
ylim = horiz + c(-1.1, 1.1) * max(abs(y-horiz))
# do the main plotting
plot(x, y, xlim=xlim, ylim=ylim)
abline(h=horiz, v=vert)
Now, you could plot the 'values of the lines', either on the axes (the lineparameter allows you to control for possible overlapping):
mtext(c(vert, horiz), side=c(1,2))
or alternatively within the plotting frame:
text(x=vert, y=ylim[1], labels=vert, adj=c(1.1,1), col='blue')
text(x=xlim[1], y=horiz, labels=horiz, adj=c(0.9,-0.1), col='blue')
HTH

Resources