Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Multiple axes can be utilized to display columns in a plot using R by using the par() function to set the mfrow parameter to a vector that specifies the number of rows and columns for the plot layout. Then, individual plots can be created using the plot() function and customized by using the axis() function to add additional axes. Here is an example:

# create example data
data <- data.frame(id = 1:5, column1 = c(2, 4, 6, 8, 10), column2 = c(1, 3, 5, 7, 9))

# create a plot with two columns side by side
par(mfrow=c(1, 2))

# create the first plot
plot(data$id, data$column1, type = "l", xlab = "ID", ylab = "Value", main = "Column 1")

# add a secondary y-axis on the right for the first plot
axis(side = 4, at = pretty(data$column1), labels = pretty(data$column1))

# create the second plot
plot(data$id, data$column2, type = "l", xlab = "ID", ylab = "Value", main = "Column 2")

# add a secondary y-axis on the right for the second plot
axis(side = 4, at = pretty(data$column2), labels = pretty(data$column2))

This will create a plot with two columns side by side, each displaying a different column of data with its own y-axis on the left and a secondary y-axis on the right.