Ggplot2
- Creates customized, high-quality graphics in R for data exploration and communication.
- Builds plots by composing distinct components (aesthetics, geoms) and layering visual elements like points, lines, and bars.
- Supports easy addition of statistical transformations (e.g., adding a linear trend line with geom_smooth(method=“lm”)).
Definition
Section titled “Definition”ggplot2 is a data visualization package for the R programming language. It is based on the grammar of graphics, a framework that separates the construction of a plot into distinct components.
Explanation
Section titled “Explanation”ggplot2 enables users to create customized, high-quality graphics by mapping data to aesthetic attributes and adding graphical layers. Typical elements include geoms such as points, lines, and bars; these elements can be layered to form complex graphics. The package also provides functions to apply statistical transformations to the data and visualize those results (for example, fitting and displaying a regression line).
Examples
Section titled “Examples”Scatterplot of temperature vs. ozone
Section titled “Scatterplot of temperature vs. ozone”ggplot(data=city_data, aes(x=temperature, y=ozone)) + geom_point()
This creates a scatterplot using the city_data dataset where the x-axis is temperature and the y-axis is ozone; geom_point() adds the individual observations as points.
Scatterplot with linear trend line
Section titled “Scatterplot with linear trend line”ggplot(data=city_data, aes(x=temperature, y=ozone)) + geom_point() + geom_smooth(method="lm")
This adds a linear regression line to the scatterplot; the argument method="lm" specifies that a linear model should be fit to the data.
Use cases
Section titled “Use cases”- Data exploration and communication through visual summaries.
- Visualizing relationships in data, such as scatterplots with trend lines.
Related terms
Section titled “Related terms”- Grammar of graphics
- R (programming language)
- geom_point
- geom_smooth
- method=“lm”