How to query axis limits in R? - r

Is it possible to get the axis limits for the current plot in R? How?
Preferably in the standard base library (I don't like to use ggplot etc., but these solutions are also welcome).

par("usr")
That ought to do it; see ?par.

Related

How to determine x, y coordinates when adding text in metafor

I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.

How do you extract what units ggplot2 is using for its coordinates?

This is silly, I can't be the only person who has problems aligning annotations with ggplot2 graphics.
I can't use gridBase because I'm trying to automate a complex and repetitious plotting task, so I need to get at the coordinates programmatically, and pass them as parameters to annotate before the grobs are plotted.
ggplot has access to this information. There's got to be a way for me to do so as well!
I'm surprise something this important for cleanly annotating plots isn't deliberately exposed by ggplot2 to begin with.
The coordinates - or x and y scales - are set when generating the final plot. Hadley used the term "training".
This means that you have to have it plotted, either as a grob or to a device, before ggplot knows the scales.
Instead, you can explicitly tell ggplot the extend of the axis, either with coord_cartesian or xlim. For this, you can use the range and pretty functions to determine the extent together with some nice rounding.
You can use layer_scales(plot, i = 1L, j = 1L).
Check here for reference

how to know what category corresponds to each point in a plot in R?

I just wanted how know what category matches with everypoint when I do this:
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group)
I know how to make a legend and put text with an arbitrary vector c('A','B',C'), but is there a more "automatic" way for doing this? This is an easy example but I need to do it with residuals or survival functions plot
Thank you in advance.
The traditional graphics system provides the legend function for adding a
legend or key to a plot. But It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. There is no automatic checking that
data symbols in the legend match those in the plot. It is simpler to do it using lattice or ggplot2. for example:
library(lattice)
xyplot(y~x,groups=group,auto.key=T)
if you want absolutly to use base graphics, you can do this :
x<-rnorm(mean=0,sd=1,500)
y<-sample(1:500,500,replace=T)
group<-as.factor(sample(c('A','B','C'),500,replace=T,prob=c(0.2,0.3,0.5)))
plot(x,y,col=group,pch=as.numeric(group))
legend(2, 500, c('A','B','C'),
cex=1.5, pch=1:3,col=1:3)

R X&Y axes interval setup in histogram

I have been using the following code. But, instead of starting at -5000, the x-axis is starting at 0. Also, I would like to specify an interval of 100 for both x and y-axes ranges. I tried to use xaxt='n' and axis(side=1), but that didn't help. The following code is the one I am currently working on
setwd("/path/")
data<-read.table("input.txt",sep="\t",header=F)
x<-data$V4
h<-hist(x, col="green",xlim=c(-5000, 162000),ylim=c(0,0.0003),main="TPMDistribution",xlab="TPMValues",probability=TRUE)
s=sd(x)
m=mean(x)
curve(dnorm(x,mean=m,sd=s),add=TRUE,lwd=3,col="red")
lines(density(x),col="blue")
abline(v=mean(x),col="blue")
mtext(paste("mean",round(mean(x),1),";sd",round(sd(x),1),";N",length(x),sep=""),side=1,cex=.75)
dev.off()
All helps appreciated. Thanks in advance.
You'll want to specify the breaks, probably in an array, see here:
http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/hist.html
And adjust the axis using something like this:
http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/axis.html
You may need to use axes=FALSE in your hist(), so you can replace them with axis() in a subsequent line.

equivalent to MatLab "bar" function in R?

Is there an function in R that does the same job as Matlab's "bar" function?
R does have a "barplot" function in the library graphics, however, it is not the same.
The Matlab bar(X,Y) (verbatim excerpt from MATLAB documentation) "draws a bar for each element in Y at locations specified in X, where X is a vector defining the x-axis intervals for the vertical bars." (emphasis mine)
However, the R barplot function does not allow one to specify locations.
Perhaps there is a method in ggplot2 that supports this? I am only able to find standard bar charts in ggplot2.
No, barplot is not the same as bar, but you should read the whole help. You can do many things to position the bars. The first is simply their order in Y. You could insert spaces if you wish (additional 0s). If you have X and Y then sort Y on X (Y[order(X)]) and plot it. If you need to change positions use the "space" and "width" arguments. It's not as straightforward as specifying X values I suppose but it's definitely more useful in most situations. Generally what you want to adjust is widths of bars and spaces between bars. Their position on the X-axis should be arbitrary. If the position on the X-axis is really meaningful then you should be using line plots, not bar graphs.
In R:
barplot(rbind(1:10, 2:11), beside=T, names.arg=1:10)
In MATLAB:
>> bar(1:10, [(1:10)' (2:11)'])
Read up on par . Then observe, for example:
x<-c(1,2,4,5,6)
y<-c(3,4,3,4,2)
plot(x,y,type='h',lwd=6)
Edit: yes, I know this doesn't (yet) plot multiple data sets, but I would hope you can see simple ways to make that happen, with spacings, colors, etc. specified to your exact liking :-)
Sounds vaguely like the R stepfun. On the other hand one would need to know what "draws a bar" means before saying it is not the same as barplot(..., horiz=TRUE) One would, of course, need to examine some more detailed evidence such as data and plots before arriving at a conclusion, however. #John Colby should be congratulated for adding some specificity to the discussion. The axis function is probably what Quant Guy needs education regarding.

Resources