user3639557
user3639557

Reputation: 5311

R cuts the plot

I am using the following couple of lines to produce the below plot from rows of two 4X10 Matrix d1, and d2 in one graph:

plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="firebrick",
ann=FALSE, pch=17, log = 'y',lty=4, xaxt = "n", yaxt="n",las=2) +
lines(as.matrix(d1[1,]),as.matrix(d2[1,]), type="o", col="deepskyblue",
ann=FALSE, pch=15, lty=4) +
lines(as.matrix(d1[3,]),as.matrix(d2[3,]), type="o", col="darkorange",
ann=FALSE, pch=18, lty=4)

x_axis_range <- c(0, 1, 2, 4, 6, 8, 10, 12, 14)
axis(1,at = x_axis_range, labels = x_axis_range)

y_axis_range <- c(0, 4e+2, 1e+3, 5e+3, 3e+4, 7e+4,2e+5, 1e+6, 2e+6) 
y_axis_labels <- c("0", "400", "1K", "5K", "30K", "70K", "200K", "1M", "2M")

axis(2,at = y_axis_range, labels = y_axis_labels, las=2)

And it produces: enter image description here

But there are two issues with this plot:

  1. As you can see part of the blue line is cut even though I forced the y-axis to start from 0. The lowest y-value for the blue line is 100.
  2. If you look at the code above, I plot three lines, the "darkorange" one is missing from the plot. The issue is similar to the above issue: the whole darkorange line starts from y = 1e+6 which I have failed R to show it in the plot. I have no idea what is going on :/.

Edit: These are the warning outputs of R when running this:

numeric(0)

Warning messages:

1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion

2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion

3: In xy.coords(x, y) : NAs introduced by coercion

4: In xy.coords(x, y) : NAs introduced by coercion

5: In xy.coords(x, y) : NAs introduced by coercion

6: In xy.coords(x, y) : NAs introduced by coercion

Upvotes: 1

Views: 2593

Answers (1)

Phann
Phann

Reputation: 1337

plot defines the range of the axis even if not plotted. As you can see, the "firebrick"-data is plotted within its range. lines adds just a line to an existing plot without changing the limits. So I suggest to try:

plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="firebrick",
ann=FALSE, pch=17, log = 'y',lty=4, xaxt = "n", yaxt="n",las=2,
ylim = c(50, 2e6))
#...

You could also check the minimum and maximum before plotting with:

MinY <- min(d2)
MaxY <- max(d2)
plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="firebrick",
ann=FALSE, pch=17, log = 'y',lty=4, xaxt = "n", yaxt="n",las=2,
ylim = c(MinY, MaxY))
#...

Upvotes: 1

Related Questions