Psychology

Chapter 2 R Ggplot2 Examples

T

Taylor Kertzmann

September 29, 2025

Chapter 2 R Ggplot2 Examples
Chapter 2 R Ggplot2 Examples Chapter 2 R ggplot2 Examples Unveiling the Power of Data Visualization In the everevolving landscape of data analysis the ability to visualize data effectively is paramount Rs ggplot2 package has emerged as a cornerstone for creating aesthetically pleasing and insightful graphics This chapter delves into the core functionalities of ggplot2 showcasing practical examples that illustrate its versatility and power Setting the Stage Data and Packages We will use the mtcars dataset readily available in R which contains information on fuel efficiency horsepower and other characteristics of 32 different car models r Load the ggplot2 package libraryggplot2 View the mtcars dataset headmtcars Exploring Data Relationships Scatter Plots Scatter plots are essential for visualizing the relationship between two continuous variables Lets examine the relationship between horsepower and miles per gallon mpg r ggplotmtcars aesx hp y mpg geompoint labsx Horsepower y Miles per Gallon title Horsepower vs Miles per Gallon This code generates a basic scatter plot We can enhance it by adding a trendline r ggplotmtcars aesx hp y mpg 2 geompoint geomsmoothmethod lm se FALSE labsx Horsepower y Miles per Gallon title Horsepower vs Miles per Gallon Here geomsmooth adds a linear regression line method lm The se FALSE argument removes the confidence interval bands Unveiling Distributions Histograms and Density Plots To understand the distribution of a single variable we can use histograms and density plots Lets visualize the distribution of miles per gallon r ggplotmtcars aesx mpg geomhistogrambinwidth 2 labsx Miles per Gallon y Frequency title Distribution of Miles per Gallon This code creates a histogram with binwidth of 2 For a smoother representation we can use a density plot r ggplotmtcars aesx mpg geomdensity labsx Miles per Gallon y Density title Density Distribution of Miles per Gallon Visualizing Categorical Data Bar Charts Bar charts are ideal for visualizing categorical data Lets explore the distribution of car transmission types r ggplotmtcars aesx am geombar labsx Transmission Type 0 Automatic 1 Manual y Frequency 3 title Distribution of Transmission Types We can customize the appearance of the bar chart by adding color and changing the fill type r ggplotmtcars aesx am fill factoram geombar scalefillmanualvalues cskyblue coral labels cAutomatic Manual labsx Transmission Type y Frequency title Distribution of Transmission Types Bringing Depth Boxplots Boxplots provide a visual summary of a continuous variable across different categories Lets visualize the distribution of miles per gallon for different car cylinder types r ggplotmtcars aesx factorcyl y mpg geomboxplot labsx Number of Cylinders y Miles per Gallon title Miles per Gallon by Number of Cylinders Adding Layers Facets and Groups To explore interactions between multiple variables we can utilize facets or groups Lets visualize the relationship between horsepower and miles per gallon for different transmission types using facets r ggplotmtcars aesx hp y mpg geompoint facetwrap am labeller labelleram c0 Automatic 1 Manual labsx Horsepower y Miles per Gallon title Horsepower vs Miles per Gallon by Transmission Type 4 Alternatively we can use groups to represent different transmission types with different colors r ggplotmtcars aesx hp y mpg color factoram geompoint scalecolormanualvalues cskyblue coral labels cAutomatic Manual labsx Horsepower y Miles per Gallon title Horsepower vs Miles per Gallon by Transmission Type Customizing Appearance ggplot2 offers extensive customization options Here are some key elements Theme Controls the overall aesthetic of the plot Labels Modify axis labels and plot title Color Customize color palettes Text Change font size and style Legend Adjust legend position and appearance Example r ggplotmtcars aesx hp y mpg geompointcolor darkgreen size 3 geomsmoothmethod lm se FALSE color red labsx Horsepower y Miles per Gallon title Horsepower vs Miles per Gallon themebw themeaxistext elementtextsize 12 axistitle elementtextsize 14 plottitle elementtextsize 16 face bold This code changes the point color and size adds a red trendline applies a white background theme and adjusts text sizes and styles Conclusion 5 ggplot2 empowers users to create insightful and visually appealing data visualizations This chapter has provided a glimpse into its capabilities through practical examples By mastering the principles outlined here you can unlock the potential of ggplot2 to explore analyze and communicate data effectively

Related Stories