0% found this document useful (0 votes)
7 views

Creating Data Visualizations using ggplot

This document provides a step-by-step guide for creating data visualizations using the ggplot package in R, specifically with the mtcars dataset. It includes instructions for creating scatterplots, boxplots, and histograms, along with code snippets for each visualization. The document concludes with an encouragement for users to enjoy the lab experience.

Uploaded by

Simhadri Sevitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Creating Data Visualizations using ggplot

This document provides a step-by-step guide for creating data visualizations using the ggplot package in R, specifically with the mtcars dataset. It includes instructions for creating scatterplots, boxplots, and histograms, along with code snippets for each visualization. The document concludes with an encouragement for users to enjoy the lab experience.

Uploaded by

Simhadri Sevitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5/5/23, 3:43 PM https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0105EN-SkillsNetwork/labs/Module2/plotting_with_R.md.

html

Creating Data Visualizations using ggplot


Objective for Exercise

We will create different data visualizations using the ggplot package using the inbuilt dataset in R called mtcars

1. Click on the + symbol on the top left and choose R Script from the menu to open a new R edit window in RStudio:

2. Read and view the first 5 rows of the Data using the following:

library(datasets)
# Load Data
data(mtcars)
# View first 5 rows
head(mtcars, 5)

3. Type this ?mtcars to get information about the variables. This will print the information at the bottom right panel, on the Help tab

4. Copy and paste the following code to load the ggplot package and create a scatterplot of disp and mpg.

#load ggplot package


library(ggplot2)
# create a scatterplot of displacement (disp) and miles per gallon (mpg)
ggplot(aes(x=disp,y=mpg,),data=mtcars)+geom_point()

5. Use the following code to add a title.

# Add a title
ggplot(aes(x=disp,y=mpg,),data=mtcars)+geom_point()+ggtitle("displacement vs miles per gallon")

6. Use the following code to change the name of the x-axis and y-axis

https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0105EN-SkillsNetwork/labs/Module2/plotting_with_R.md.html 1/2
5/5/23, 3:43 PM https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0105EN-SkillsNetwork/labs/Module2/plotting_with_R.md.html

# change axis name


ggplot(aes(x=disp,y=mpg,),data=mtcars)+geom_point()+ggtitle("displacement vs miles per gallon") + labs(x = "Displacement", y =
"Miles per Gallon")

7. Use the following to create a boxplot of the the distribution of mpg for the individual Engine types vs Engine (0 = V-shaped, 1 =
straight)

To do this you have to make vs a string or factor.

#make vs a factor
mtcars$vs <- as.factor(mtcars$vs)
# create boxplot of the distribution for v-shaped and straight Engine
ggplot(aes(x=vs, y=mpg), data = mtcars) + geom_boxplot()

8. Add color to the boxplots to help differentiate:

ggplot(aes(x=vs, y=mpg, fill = vs), data = mtcars) +


geom_boxplot(alpha=0.3) +
theme(legend.position="none")

9. Finally, let us create the histogram of weight wt.

ggplot(aes(x=wt),data=mtcars) + geom_histogram(binwidth=0.5)

This concludes this lab, we hope that you had fun!

Author(s)
Aije Egwaikhide

Change log
Date Version Changed by Change Description

2020-12-14 1.0 Aije Created initial version of the lab

https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0105EN-SkillsNetwork/labs/Module2/plotting_with_R.md.html 2/2

You might also like