Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to customize y-axis step size in R

Tags:

r

statistics

I would like to plot a scatterplot with y-axis is customized to step size of 0.2, within range of 0 - 2.6, and x-axis can be auto-defined. I tried the below, but it doesnt work. May I know how should I set the param correctly?


# Read data
pt.n <- read.table("p0_n300m20r1c1_regression.txt", header=T)
# auto-scale
# plot(pt.n$maee~pt.n$idx, main="P2PSim Seq#1,300n,20%,1r,Corrective", ylab="MAEE", xlab="Seq #")

# customize
ylabel <- c(0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6)
y_range <- range(0, ylabel)
plot(pt.n$maee~pt.n$idx, main="P2PSim Seq#3,300n,20%,1r,Corrective", ylab="MAEE", xlab="Seq #", ylim=y_range, axes=FALSE, ann=FALSE)
axis(1, at=0:6, lab=c(0,50,100,150,200,250,300))
axis(2, las=1, at=0.2*0:y_range[1])
box()
like image 225
twfx Avatar asked Sep 28 '12 06:09

twfx


People also ask

How do I change the Y axis scale 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 I increase the size of Y axis in R?

To increase the length of Y-axis for ggplot2 graph in R, we can use scale_y_continuous function with limits argument.

How do you change intervals on a graph in R?

In this method of changing the axis intervals, the user needs to call the xlim() and ylim() functions, passing the arguments of the range of the axis intervals required by the user in form of the vector, this will be changing the axis intervals of the plot as per the specified parameters by the user in the R ...


1 Answers

If something is not working check each bit of the thing that isn't doing what you want to make sure you are supplying the correct data and haven't made a booboo. If we run the bits of your code that are associated with the axis

ylabel <- c(0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6)
y_range <- range(0, ylabel)
0.2*0:y_range[1]

You would immediately see the problem:

R> 0.2*0:y_range[1]
[1] 0

where you are basically telling R to draw a tick at 0. Even if you chose the correct element of y_range (the maximum is in the second element) you still wouldn't get the right answer:

R> 0.2*0:y_range[2]
[1] 0.0 0.2 0.4
R> 0:y_range[2]
[1] 0 1 2

and that is because of the way the : operator works. A call of x:y is essentially a call to seq(from = x, to = y, by = 1) and because 2.6+1 is greater than 2.6 (the to argument) R creates the sequence 0, 1, 2.

If you want to draw ticks and label at 0 - 2.6 incrementing by 0.2 then use:

ylabel <- seq(0, 2.6, by = 0.2)
axis(2, at = ylabel)

where ylabel now contains:

R> ylabel
[1] 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6

To illustrate:

dat <- data.frame(y = runif(20, min = 0, max = 3),
                  x = rnorm(20))

plot(y ~ x, data = dat, axes = FALSE)
ylabel <- seq(0, 2.6, by = 0.2)
axis(1)
axis(2, at = ylabel, las = 1)
box()

which produces

enter image description here

like image 140
Gavin Simpson Avatar answered Oct 01 '22 11:10

Gavin Simpson