In this R tutorial, we will learn some basic functions and learn to use the Plotly package in R to build histograms such as a basic histogram, normalized histogram and a linear histogram with the data from the used cars dataset.

Install and Load Packages

In order to build the Plotly histograms, we will need to load the below packages and libraries to complete this tutorial:

1

2

>install.packages("plotly")

>library(plotly)

Download and Load the Used Cars Dataset

In order to build the create the Plotly histograms, we will need to download and load the used cars dataset. This dataset is already packaged and available for an easy download from the dataset page or directly from here Used Cars Dataset – usedcars.csv

1

>usedcars<-read.csv("usedcars.csv",stringsAsFactors=FALSE)

View the Used Cars Data

In order to effectively build the histograms, one should know what data is in the dataset. We can use a few functions that would help with viewing the used car’s data.

str()

The str() command displays the internal structure of an R object. The str() function will only display one line for each structure of the data.

1

>str(usedcars)

1

2

3

4

5

6

7

'data.frame':150obs.of6variables:

$year:int2011201120112011201220102011201020112010...

$model:chr"SEL""SEL""SEL""SEL"...

$price:int21992209951999517809175001749517000169951699516995...

$mileage:int74131092673511161383672512527393210263265536116...

$color:chr"Yellow""Gray""Silver""Gray"...

$transmission:chr"AUTO""AUTO""AUTO""AUTO"...

summary()

The summary() function is a basic function that issued to produce the result summary of various model functions.

1

>summary(usedcars)

1

2

3

4

5

6

7

8

9

10

11

year model price mileage color

Min.:2000Length:150Min.:3800Min.:4867Length:150

1stQu.:2008Class:character1stQu.:109951stQu.:27200Class:character

Median:2009Mode:character Median:13592Median:36385Mode:character

Mean:2009Mean:12962Mean:44261

3rdQu.:20103rdQu.:149043rdQu.:55125

Max.:2012Max.:21992Max.:151479

transmission

Length:150

Class:character

Mode:character

range()

The range() function returns a vector containing the maximum and minimum of all the given arguments.