Introduction The purpose of this code-through is to show how to create a histogram. The data set that ill be using for this Code-Through had vehicle data from a 70s Motor Trend Car magazine. For this example I chose to use fuel consumption (mile per gallon) data of the 32 vehicles that were observed.
?mtcars
Data This is a list of the 32 observations of Mile Per Gallon data for each respective vehicle.
mtcars$mpg
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
Histogram After using the function “hist”, this is the simple histogram that is derived.
hist(mtcars$mpg)
Histogram
First I decided to add some colors to the histogram to improve the appearance. It was also very important to update the main title, along with X and Y axis. Since the original histogram was only splitting that data into 5 columns, I decided to use the “breaks” function that helps break down the data into 12 columns. This function brought more transparency and clarity to the visual. Since there are more columns now, I had to update the X axis range of my visual, I used “xlim” for that. Overall the Histogram (hist) function is a very simple yet useful function that helps interpret data with use of a visual.
colors <- c("Blue", "red", "orange", "grey", "purple")
hist(mtcars$mpg,
col=colors,
main="Motor Trend - 70s Vehicles",
breaks=10,
xlim = range(10:35),
xlab="MPG",
ylab= "# of Vehicles")