Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set R plots x axis to show at y=0

Tags:

plot

r

Usually when I plot some R line plot and set ylim=c(0,some_value), there is small space between the x axis and y=0.

I would like the y axis to show exactly at y=0 so points (x,0) will be plotted on the y axis (and not above).

like image 518
David B Avatar asked Aug 06 '10 08:08

David B


People also ask

How do I set the x-axis value in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do you set X and Y limits in R?

You can use the xlim() and ylim() functions to set the x-axis limits and y-axis limits of plots in R.


2 Answers

You probably want the graphical parameters xaxs and yaxs with style "i":

plot(1:10, rnorm(10), ylim=c(0,10), yaxs="i") 

See ?par:

xaxs: The style of axis interval calculation to be used for the x-axis. Possible values are "r", "i", "e", "s", "d". The styles are generally controlled by the range of data or xlim, if given. Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range. Style "i" (internal) just finds an axis with pretty labels that fits within the original data range. Style "s" (standard) finds an axis with pretty labels within which the original data range fits. Style "e" (extended) is like style "s", except that it is also ensures that there is room for plotting symbols within the bounding box. Style "d" (direct) specifies that the current axis should be used on subsequent plots. (Only "r" and "i" styles are currently implemented)

yaxs: The style of axis interval calculation to be used for the y-axis. See xaxs above.

like image 84
rcs Avatar answered Sep 22 '22 22:09

rcs


Another option is to disable the axis plotting in plot using axes=F and then put the axes in separately using the axis command, with the pos option controlling the axis spacing.

plot(1:10, runif(10), ylim=c(0,1), xlim=c(0,10),axes=F)      axis(1, pos=0)      axis(2, pos=0)    
like image 27
WetlabStudent Avatar answered Sep 22 '22 22:09

WetlabStudent