25-November-2022

Simple plotly example - mtcars dataset

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Settings

We are going to plot ‘Mileage per gallon’ vs ‘Weight (1000 lbs)’, by ‘# of cylinders’

library(plotly)
## Warning: package 'plotly' was built under R version 4.1.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Settings (cont.)

f <- list(
  family = "Courier New, monospace",
  size = 11,   color = "#6e6e6e" )
x <- list(
  title = "Weight (1000 lbs)",   titlefont = f )

y <- list(
  title = "Mileage per gallon",   titlefont = f )
plot_ly(mtcars, x = mtcars$wt, y=mtcars$mpg, mode="markers", 
        type="scatter",color=as.factor(mtcars$cyl), size=mtcars$hp ) %>%   
        layout(xaxis=x, yaxis=y)

Plotting

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

Interpretation

Simply put:

  • At a glance, what it can be grasped from this analysis is that the less is the weight and number of cylinders in a motor, the more is the mileage per gallon, and conversely, the more the weight, less is the mileage per gallon.

References