Basic Data Visualization
Introduction to Basic Data Visualization
Welcome! In our previous lesson, we focused on data manipulation and transformation using the dplyr library. This allowed us to prepare and refine our datasets. Now, we are moving on to an exciting part of data science: data visualization.
Every data science task involves a data exploration phase, and visualizations are a critical part of this phase. They allow you to visually and more quickly explore the data, detect patterns, and gain insights that might be missed in raw data forms.
What You'll Learn
In this lesson, you'll learn the foundational concepts of creating visual representations of data in R using the ggplot2 library. Specifically, we'll focus on:
- Scatter Plots: These help you see the relationship between two continuous variables.
- Bar Charts: These are great for comparing categorical data.
Introduction to `ggplot2`
ggplot2 is a powerful and widely-used library in R for creating elegant and complex visualizations. It follows the principles of "The Grammar of Graphics", which is a coherent system for describing and building graphs.
Example Code with Explanations
To get you started, here's a detailed look at the kind of visualizations you will be creating. We'll be using the famous iris dataset for our examples.
Loading the Data:
First, let's load the iris dataset, which comes pre-loaded in R:
The iris dataset contains measurements of iris flowers from three different species: setosa, versicolor, and virginica. It includes 150 observations with five variables: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.
Creating a Scatter Plot:
Next, we'll create a scatter plot to visualize the relationship between Sepal Length and Sepal Width, colored by species, using ggplot2:
ggplot(iris, aes(...)): Initializes the plotting system with theirisdataset and maps Sepal.Length to the x-axis, Sepal.Width to the y-axis, and Species to the color scale.geom_point(): Adds points to the plot.labs(...): Adds labels for the title, x-axis, and y-axis.scale_color_manual: Manually sets colors for species.
Creating a Bar Chart:
Now, let's create a bar chart to visualize the count of each species using ggplot2:
ggplot(iris, aes(...)): Initializes the plotting system with theirisdataset and maps Species to the x-axis.geom_bar(fill = "steelblue"): Adds bars to the plot with a steel blue fill.labs(...): Adds labels for the title, x-axis, and y-axis.
Displaying the Plots:
Finally, you can display the plots using the print function:
By running the above code, you'll generate a scatter plot showing the relationship between Sepal Length and Sepal Width for different species, and a bar chart showing the count of observations for each species. You should see similar outputs, when running the code:

