Practice 5 Histograms with R
5.1 Directions
In this practice exercise, you will load data into R, and create a histogram.
5.2 R code used in the VoiceThread
# Loads the tidyverse package
library(tidyverse)
# Historgram in base R
diamonds
data <-
hist(diamonds$price)
# Change the number of bins with the "breaks" argument:
hist(diamonds$price, breaks = 100)
# Histogram in ggplot2
%>% ggplot(aes(x = price)) +
data geom_histogram(bins=100, color = "black", fill="gray90")
5.3 Now you try
Use R to complete the following activities (this is just for practice you do not need to turn anything in).
- Plot a histogram of mpg from the mtcars dataset.
- Plot a histogram of mpg from the mtcars dataset using 10 bins.
- BONUS: Use
xlim = c(0,40)
to make the 10 bin histogram fit the axis better.
R Code Window
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImRhdGEoXCJtdGNhcnNcIikiLCJzYW1wbGUiOiIjIFRoZSBkYXRhIGhhcyBhbHJlYWR5IGJlZW4gbG9hZGVkXG5cbiMgUXVlc3Rpb24gMVxuXG5cbiMgUXVlc3Rpb24gMiIsInNvbHV0aW9uIjoiIyBMb2FkIGRhdGFcbmRhdGEoXCJtdGNhcnNcIilcblxuIyBRdWVzdGlvbiAxXG5oaXN0KG10Y2FycyRtcGcpXG5cbiMgUXVlc3Rpb24gMlxuaGlzdChtdGNhcnMkbXBnLCBicmVha3MgPSAxMClcblxuIyBCT05VU1xuaGlzdChtdGNhcnMkbXBnLCBicmVha3MgPSAxMCwgeGxpbT1jKDAsNDApKSJ9