Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 3.3 KB

README.md

File metadata and controls

93 lines (71 loc) · 3.3 KB

testrepo

Editing the File

Its a markdown repository

Objective for Exercise

We will create different data visualizations using the ggplotpackage using the inbuilt dataset in "R" called mtcars This is set up on (formerly Big Data University) by IBM Developer Skills Network lab

  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 image OR Hold CTRL + SHIFT + ALT + N
  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)
  1. Type this ?mtcars to get information about the variables. This will print the information at the bottom right panel, on the Help tab

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

Load ggplot package

library(ggplot2) 2022-08-14 08_15_21-Skills Network Labs

create a scatterplot of displacement (disp) and miles per gallon (mpg)

ggplot(aes(x=disp,y=mpg,), data=mtcars)+ geom_point() 2022-08-15 06_24_45-Skills Network Labs

  1. 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")

2022-08-15 06_24_45-Skills Network Labs

  1. Use the following code to change the name of the x-axis and y-axis 2022-08-15 06_26_52-

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")

2022-08-15 06_24_45-Skills Network Labs

  1. 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)

2022-08-15 06_26_52-

create boxplot of the distribution for v-shaped and straight Engine

ggplot(aes(x=vs, y=mpg), data = mtcars) + geom_boxplot()
  1. 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")
  1. Finally, let us create the histogram of weight wt.
 ggplot(aes(x=wt), data=mtcars) + geom_histogram(binwidth=0.5)

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

Author(s)

 Ekene Emmanuel Ajemba