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()
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.
To increase the length of Y-axis for ggplot2 graph in R, we can use scale_y_continuous function with limits argument.
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 ...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With