Creating Basic Plots with Base R

Creating Basic Plots with Base R

Data visualization is a critical aspect of data analysis, and in R, one of the most straightforward ways to create visual representations of data is through its base plotting system. This section will introduce you to the fundamental functions for creating basic plots in R, allowing you to effectively visualize your data.

Getting Started with Base R Plots

Base R provides a set of functions for creating various types of plots. The most common plotting function is plot(), which can be used for different types of data visualizations, including scatter plots, line plots, and more.

Scatter Plots

A scatter plot displays values for typically two variables for a set of data. Here's how you can create a basic scatter plot:

`r

Sample data

x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 5, 7, 11)

Basic scatter plot

plot(x, y, main="Basic Scatter Plot", xlab="X Axis", ylab="Y Axis", col="blue", pch=19) `

In this example: - x and y are the vectors containing the data points. - The main argument sets the title of the plot. - xlab and ylab set the labels for the x and y axes, respectively. - col specifies the color of the points, and pch sets the type of point marker.

Line Plots

Line plots are useful for displaying data points over time or continuous data. Here’s how to create a line plot:

`r

Sample data

x <- 1:10 y <- c(1, 3, 2, 5, 7, 8, 12, 14, 13, 15)

Basic line plot

plot(x, y, type="o", main="Basic Line Plot", xlab="X Axis", ylab="Y Axis", col="red") `

In this code: - The type="o" argument indicates that points should be both plotted and connected by lines.

Histogram

Histograms show the distribution of a continuous variable and can be created using the hist() function:

`r

Sample data

set.seed(123) data <- rnorm(1000)

Basic histogram

hist(data, main="Basic Histogram", xlab="Value", col="lightgreen", border="black") `

In this example: - rnorm(1000) generates 1000 random numbers from a normal distribution. - The border argument specifies the color of the border around the bars.

Customizing Plots

R provides extensive options for customizing plots: - Colors: Use named colors or hexadecimal color codes. - Point Types: Different pch values can be used to change the type of point marker. - Axes: The axis() function allows you to add custom tick marks and labels.

Example of Customization

`r

Customized scatter plot

plot(x, y, main="Customized Scatter Plot", xlab="X Axis", ylab="Y Axis", col="purple", pch=17) axis(1, at=1:5, labels=c("One", "Two", "Three", "Four", "Five")) `

In this example, we customize the x-axis labels using the axis() function.

Conclusion

Base R plotting functions are powerful tools that allow data scientists to create a wide range of visualizations quickly and effectively. By mastering these basics, you can begin to explore more complex visualizations and enhance the interpretability of your data.

For further exploration, try combining multiple plotting functions and experiment with different datasets to hone your visualization skills!

Back to Course View Full Topic