# Cars Analysis This article shows several common operations while programming in R, making use of the mtcars dataset that is included as part of the R 'graphics' package. ![cars.jpg][nextjournal#file#a754a3e1-61fa-416a-83b3-95486815ab7a] The mtcars data was extracted from the 1974 Motor Trend US magazine. It compares the fuel consumption and performance of thirty-two different automobiles based on ten different aspects of their design. The code used in this article was based on the [ggplot manual](https://www.statmethods.net/advgraphs/ggplot2.html). ## Coding with the mtcars dataset Add `("ggplot2")` to the list of libraries available to this R runtime. ```r id=b8c2c6b6-5db6-4115-8a49-4e0fa71b2f07 library(ggplot2) ``` [nj-data](https://nextjournal.com/data/QmQcnasDwBWd7PDfMp5gZgWZQYV4kwGwwUxgtBHhhDKjrN?content-type=application%2Fjson&filename=nj-data) Create factors with value labels. ```r id=a92ab67e-1f1a-477c-93d9-2ad178a0d1a2 mtcars\$gear <- factor(mtcars\$gear,levels=c(3,4,5), labels=c("3gears","4gears","5gears")) mtcars\$am <- factor(mtcars\$am,levels=c(0,1), labels=c("Automatic","Manual")) mtcars\$cyl <- factor(mtcars\$cyl,levels=c(4,6,8), labels=c("4cyl","6cyl","8cyl")) ``` [nj-data](https://nextjournal.com/data/QmcSvKTh1718wW3LeZrqbxCFCFod5Nrem4yjy5rEWjgUvi?content-type=application%2Fjson&filename=nj-data) The data can be directly printed to a table by returning as the last value in a cell. ```r id=4e72715a-3bb0-49a5-aa8d-a1a608b2e099 mtcars ``` [nj-data](https://nextjournal.com/data/QmT4uPLFwDN3SH8FYnh4PfGqzP3vFpr85Mz7YKWdUSjN93?content-type=text%2Fcsv&filename=nj-data) ## Gas Mileage Visualized in Plotly ### Density Distribution Here we visualize the distribution of the number of cars with a particular average gas mileage (in mpg, miles per gallon), grouped by the number of gears in the car's transmission. Although we used a 'ggplot' function, it is wrapped with the corresponding 'plotly' function `("ggplotly()")` in order to provide interactivity to the resulting figure. Interactive features provided by plotly include 'tool tips' showing values for datapoints on the plot and the ability to display or hide elements of the graph by clicking on on the corresponding element in the plot legend. ```r id=6a3df63e-3539-4665-b2e2-1eb9678fc30e ggplot(mtcars, aes(mpg, fill = gear)) + geom_density(alpha = I(0.5)) + labs(title = "Distribution of Gas Milage", x = "Miles Per Gallon", y = "Density") ``` ![__out-0.png](https://nextjournal.com/data/QmQDWWGG87xmPevpSWyLa4gKHDNuo1scE9CdQSvKkFDext?content-type=image%2Fpng&filename=__out-0.png) ### Regression Plots In this example, we plot separate regressions of mpg on weight for each number of cylinders. The performance distribution of each engine type is highlighted for visual clarity. Plot creation maintains standard conventions of plotting using in ggplot. ```r id=1402c0fc-f0e0-4c00-ba3c-91a069d85164 ggplot(mtcars, aes(wt, mpg, color = cyl)) + geom_point() + geom_smooth(method = "lm") + labs(title = "Regression of MPG on Weight", x = "Weight", y = "Miles per Gallon", color = "Cylinders") ``` ![__out-0.png](https://nextjournal.com/data/QmNXGCsfkExGhNCJ7rvJnFaTcZEaLhrM365gYMNACe5AJn?content-type=image%2Fpng&filename=__out-0.png) ### Box Plots In this example we show box plots of mpg, grouped by number of gears. The observations (points) are overlayed on the boxes and jittered for visibility. ```r id=aaa044de-b519-4ded-83ec-a45dc080c579 ggplot(data = mtcars, aes(gear, mpg, fill = gear)) + geom_boxplot() + labs(title = "Mileage by Gear Number", x = "", y = "Miles Per Gallon") ``` ![__out-0.png](https://nextjournal.com/data/QmdUNqEaF23LDHX2acSxB59ZMa752obDQZG4vN2x1Jge6P?content-type=image%2Fpng&filename=__out-0.png) [nextjournal#file#a754a3e1-61fa-416a-83b3-95486815ab7a]: https://nextjournal.com/data/QmX5Wq7himPttgjTRdg3rhCjMdzhLdLmUTUftQjS2eTeRe?content-type=image%2Fjpeg&filename=cars.jpg
This notebook was exported from https://nextjournal.com ```edn nextjournal-metadata {:nodes {"1402c0fc-f0e0-4c00-ba3c-91a069d85164" {:compute-ref #uuid "aacca89c-600a-4f5f-8054-e4a430fee06d", :exec-duration 2616, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"], :stdout-collapsed? true}, "4e72715a-3bb0-49a5-aa8d-a1a608b2e099" {:compute-ref #uuid "ec5f3d9a-b848-4023-a5b2-e703ab6ff8dd", :exec-duration 373, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"]}, "6a3df63e-3539-4665-b2e2-1eb9678fc30e" {:compute-ref #uuid "401c0ec7-98fb-4757-83f1-ef79b7883e5e", :exec-duration 2376, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"], :stdout-collapsed? true}, "8e519ad3-0a99-47cc-a149-e0d61f91845e" {:environment [:environment {:article/nextjournal.id #uuid "5accb601-b16a-4637-ae55-5fd73544a52f", :node/id "10bc64db-a75e-4f08-8981-c5c6f4e614b9", :change/nextjournal.id #uuid "5b28b353-1ca2-42cb-b2c3-6a214b0d6de4"}], :kind "runtime", :language "r", :type :nextjournal}, "a754a3e1-61fa-416a-83b3-95486815ab7a" {:kind "file"}, "a92ab67e-1f1a-477c-93d9-2ad178a0d1a2" {:compute-ref #uuid "3f1df36f-5880-4183-9b00-815953a44fb5", :exec-duration 459, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"]}, "aaa044de-b519-4ded-83ec-a45dc080c579" {:compute-ref #uuid "0e9b8156-a417-424c-8d2b-26d0fd9d83ce", :exec-duration 1384, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"]}, "b8c2c6b6-5db6-4115-8a49-4e0fa71b2f07" {:compute-ref #uuid "a327a107-d7da-4e26-820b-6e5b898152ad", :exec-duration 1322, :kind "code", :output-log-lines {:stdout 0}, :refs (), :runtime [:runtime "8e519ad3-0a99-47cc-a149-e0d61f91845e"]}}} ```