Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting the limits of x and y axis, when adding new curves to a plot in R

Tags:

plot

range

r

I have two datasets (df1 and df2) that are plotted.

df1 = data.frame(x=c(1:10), y=c(1:10))
df2 = data.frame(x=c(0:13), y=c(0:13)^1.2)

# plot
plot(df1)
# add lines of another dataset
lines(df2)

Some values of df2 are out of the plot-range and thus not visible. (In this example I could just plot df2 first). I usually try to find out the ranges of my data, as shown below.

# manual solution
minX = min(df1$x, df2$x)
minY = min(df1$y, df2$y)
maxX = max(df1$x, df2$x)
maxY = max(df1$y, df2$y)

plot (df1, xlim=c(minX, maxX), ylim=c(minY, maxY))
lines(df2)

When having many datasets, this becomes annoying. I was wondering, if there is an easier way of adjusting the ranges of the axis. In the first step R finds axis ranges itself. Is there also a way that R adjusts the axis-ranges, when new datasets are added?

like image 305
John Garreth Avatar asked Nov 08 '12 10:11

John Garreth


People also ask

How do you change 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.

How do I change the limit of the X axis 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.

Which function is used to set X axis and Y axis limits for the plots?

Change Axis Limits Specify the axis limits using the xlim and ylim functions. For 3-D plots, use the zlim function.

How do you set x and y axis in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.


2 Answers

You could use range to calculate the limits.

Imho, a better solution:

df1 <- data.frame(x=c(1:10), y=c(1:10))
df2 <- data.frame(x=c(0:13), y=c(0:13)^1.2)

ll <- list(df1,df2)

ll <- lapply(1:length(ll),function(i) {res <- ll[[i]]; res$grp <- i; res})

df <- do.call("rbind",ll)
df$grp <- factor(df$grp)

library(ggplot2)
p1 <- ggplot(df,aes(x=x,y=y,group=grp,col=grp)) + geom_line()
p1
like image 79
Roland Avatar answered Oct 03 '22 06:10

Roland


I like @Roland's solution, but here is an extension of @Glen_b's solution that works for an arbitrary number of data sets, if you have them all in a list.

(warning: untested!)

dflist <- list(df1,df2,df3,...)  ## dots are not literal!
plotline <- function(L,...) {    ## here the dots are literal
  ## use them to specify (e.g.) xlab, ylab, other arguments to plot()
  allX <- unlist(lapply(L,"[[","x"))
  allY <- unlist(lapply(L,"[[","y"))
  plot (df1, xlim=range(allX), ylim=range(allY),type="n",...)
  invisible(lapply(L,lines))
}

This assumes that you want all the data sets drawn as lines. If you want to start specify separate colours, point types, etc., you could extend this function -- but you would be starting to re-invent the lattice and ggplot2 packages at that point.

(If all your data sets are the same size, you should consider matplot)

like image 34
Ben Bolker Avatar answered Oct 03 '22 08:10

Ben Bolker