Skip to content

The simplest form of the plot function that returns a scatterplot of the values vs the index, add lines to the function and add a title to make it easier to read and understand the plot in R. Here we add a line to the plot and to add a title. We specify the title function, create more beautiful visualizations using the ggplot libraries. ggplot i…

Notifications You must be signed in to change notification settings

Aje-dotcom/testrepo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 

About

The simplest form of the plot function that returns a scatterplot of the values vs the index, add lines to the function and add a title to make it easier to read and understand the plot in R. Here we add a line to the plot and to add a title. We specify the title function, create more beautiful visualizations using the ggplot libraries. ggplot i…

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published