Increasing axis ranges - r

I have a plot that I have added a second line to using lines() function but the added line does not fit within the default axis span so I want to increase the range of both axis so the whole of the new line fits. The code for the graph is the following:
plot(spa8, ci.type="line", col="black", lwd=2, ci.lty=2, ci.col="black",
ylab = "Species Richness", yaxp = c(0,60,6), xaxp = c(0,200,10),
cex.axis = 0.75, frame.plot=FALSE, xvar = "individuals") +
lines(spa7, ci.type="line", col="grey", lwd=2, ci.lty=2, ci.col="grey",
xvar = "individuals")+
box(bty="L")
I have tried using usr() and xaxp()but with no luck, I have also tried to set my own axis using the axis() function but that hasn't worked either. I'm sure I must be doing something wrong here as it seems like something that should be simple to do!
Can someone help me out?
Thanks

Perhaps you could play around with the xlim and ylim arguments within the plot function. For example, try running the code below (line by line) and look at the difference.
## Default.
plot(1:10)
## Set x-axis to be between -10 and 10.
plot(1:10, xlim = c(-10, 10))
## Set x-axis to be between -10 and 10, and y-axis to be between -5 and 5.
plot(1:10, xlim = c(-10, 10), ylim = c(-5, 5))
Hope that helps :)

Related

How to change the y-axis label horizontal

I am trying to change the y-axis to being horizontal so that it can be read easier for my talk. I am new to R and don't have a lot of experience with it.
This is my code:
first.value = pt(500, 1.127344, 1.561328)
space = (1 - first.value)/648
edges = seq(first.value + space, 1 - space, space)
value.edges = 1.561328*qt(edges, 1.127344)
data = read.table(file.choose())
observed = log(data$V1)
expected = log(value.edges)
plot(observed,expected, xlim =c(4,15), ylim = c(0,14), las =1)
abline(0,1)
I would like the word expected the same orientation as observed. Thank you!
Try this,
par(mar = c(6,6,6,3))
plot(1, las=1, ylab="")
mtext(text="Test", side=2, line=4, las=1)
You can change the values in par to your liking, put your data instead of the 1 in plot, add your x labels etc... Then use mtext to write the yaxis label

How do I hide axes in a plot of means?

I want to omit the x-axis labels for my plot of means, but I just fail (I didn't use packages such as ggplot2):
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="")) # normal code
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",frame=F)) # which gives no response
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",axes=F))
This gives the following error:
Error in plot.default(c(1, n.levs), yrange, type = "n", xlab = xlab, ylab = ylab, :
formal argument "axes" matched by multiple actual arguments
The below code temporarily sets the color of axis tick labels (i.e., for both the x and y-axis) to "transparent" using par, thus preventing the automatically drawn tick labels from being visible. The y-axis tick labels can easily be inserted afterwards by reverting the par-related modifications.
## sample data
library(Rcmdr)
data(Moore)
## create plot with transparent (i.e., non-visible) tick labels
par(col.axis = "transparent")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
## add y-axis tick labels
par(col.axis = "black")
axis(side = 2, at = seq(0, 25, 5), tick = FALSE)
Remember that this is a rather undesired behavior of plotMeans since the help pages explicitly say that ... are arguments to be passed on to plot. Therefore, if your target is to entirely disable the drawing of the frame and axes when using plotMeans, e.g. similar to the visual output of plot(1:10, 1:10, axes = FALSE), you should probably file a bug report.
Update
In case I understood it wrongly and you actually wanted to remove both the tick labels and the ticks, you could simply use
par(xaxt = "n")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
See also ?par for further possible modifications of R base plots.

Adding label to secondary axis in R

I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.

how to align axis in R so that two axes intersects

For example, I have the following simple command:
x<-rnorm(2000, 0, 30)
hist(x)
But the graph shows a gap between the line y=0 and the x-axis. I want it to be shown in the normal format, where the two axes touch on each other at a particular point (x0, y0) which I can specify arbitrarily. What is the option in R to do that?
Thank you.
One way is to plot the x-axis separately and use line to align it with the 0 coordinate.
loc <- hist(x, xaxt="n",bty="l")
axis(1, at=loc$breaks,line=-.75)
I think the easiest way to do this is simply to draw it using box, since plot.histogram skips much of the plotting setup that would allow you to pass the appropriate par settings directly:
x<-rnorm(2000, 0, 30)
hist(x)
box(bty = "l")
See the section in par on bty for the possible options.
You can specify xlim and ylim in hist.
Check
?hist
AND
hist(x, xlim = c(-100, 100), ylim = c(0, 500))

Replace X-axis with own values

I have a question regarding the command plot().
Is there a way to fully eliminate the x-axis and replace it with own values? I know that I can get rid of the axis by doing
plot(x,y, xaxt = 'n')
and then add an axis with
axis(side = 1 etc.)
However, when I add the axis, obviously it still refers to the data plotted as 'x'.
I would only like to plot the 'y'-values and add the x-axis my own in the sense of just "drawing" the x-axis with own values specified. Is there any way to do that?
The background of this question is that my two data frames differ in their length and therefore I cannot plot them.
Not sure if it's what you mean, but you can do this:
plot(1:10, xaxt = "n", xlab='Some Letters')
axis(1, at=1:10, labels=letters[1:10])
which then gives you the graph:
You could set labels = FALSE inside axis(...) and then print the labels in a separate command using text(...). This option would allow you to rotate the text in case you need it.
lablist<-as.vector(c(1:10))
axis(1, at=seq(1, 10, by=1), labels = FALSE)
text(seq(1, 10, by=1), par("usr")[3] - 0.2, labels = lablist, srt = 45, pos = 1, xpd = TRUE)
Detailed explanation here

Resources