R X&Y axes interval setup in histogram - r

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.

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 to plot just one axis label in R?

A beginner question: I currently have some plots that look like this.
I'm keeping the plots free of other annotation because I have to fit a lot of them onto one page and specific values aren't important, just the general trend that the red line is to the right of the black one. However, I'd like to be able to indicate that the dashed line in my plot actually represents zero, like so:
Is there any way to tell R to only display that value on the x-axis labels? I don't think using text() would be a good idea because it relies on absolute coordinates, but I'm not really sure how to work with axis options to make it do what I want.
Try:
axis(side = 1, at = 0)
See ?axis for details.

Axis automatically changed?

I have a plot, where the lines are only in the negative range and when I plot it, the axis is automatically changed, i.e. negative larger values are going up and not down, in a normal plot. Currently I have the following plot:
But I want to have the y axis the other way round, so that negative larger values are going down and not up, I hope it is understandable what I mean.
How can I achieve this in R?
My code with my specific data is just using the normal plot() function.
As Ben Bolker said, the following has to be said:
I set the ylim range wrong, I set it like
ylim=c(-0.05,-1)
but
ylim=c(-1,-0.05)
should do what I want!

R plot axes don't meet and data extends beyond them

I have a VERY basic plot in R, and I'd like to solve two issues. Here is the code which produces the plot:
plot(o,n,bty="n",pch=21,cex=1.5,bg="gray",xlab="y",ylab="x",lwd=2)
And, here's the plot
There are two unwanted behaviors of this plot that I'm trying to fix. And I don't know how to do either one (nor do I understand why R doesn't do these things already...)
The X and Y axes do not meet. There is a gap near the origin in this plot. I want to remove that. The axes should touch, just like any other graph.
The data extends past the axis is both the X and Y direction. This clearly is unwanted. How can I fix this without having to manually make my own axis. Seems like there should be something more intuitive here.
bty="l".
You may also want to use something like:
xlim=c(0.02, 0.24), ylim=c(0.02, 0.24)
if you don't like the default limits of your two axes.
In general, check out ?par for guidance on both of these and many other options.
Try leaving out bty="n" or replacing it by bty="L" if you really do not want a box with edges above or on the right

How to query axis limits in 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.

Resources