R Programming language - R Plot Function
In R, you can create various types of plots using the plot()
function. This function is a versatile tool for creating a wide range of visualizations, from simple scatterplots to complex multi-panel plots.
Here is a basic example of how to create a scatterplot using the plot()
function:
# Create two vectors of data x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 3, 5, 6) # Create a scatterplot of x and y plot(x, y)
This will create a basic scatterplot of the data, with the values of x
plotted on the x-axis and the values of y
plotted on the y-axis.
You can customize the appearance of the plot by using the optional arguments of the plot()
function. For example, you can add labels to the x-axis and y-axis using the xlab
and ylab
arguments:
# Add labels to the x-axis and y-axis plot(x, y, xlab = "X-axis label", ylab = "Y-axis label")
You can also change the title of the plot using the main
argument:
# Add a title to the plot plot(x, y, main = "Title of the plot")
You can further customize the appearance of the plot by using the pch
, col
, and cex
arguments to change the shape, color, and size of the data points, respectively:
# Change the shape, color, and size of the data points plot(x, y, pch = 19, col = "red", cex = 1.5)
In addition to scatterplots, you can create other types of plots using the plot()
function, such as line plots, bar plots, and box plots. To create these types of plots, you need to format your data appropriately and use the relevant optional arguments of the plot()
function.