Student Name: Senet Manandhar

Data received: Built in R studi0 - cars dataset

Plotly:

library(openintro)
## Please visit openintro.org for free statistics materials
## 
## Attaching package: 'openintro'
## The following objects are masked from 'package:datasets':
## 
##     cars, trees
data(cars)
View(cars)
str(cars)
## 'data.frame':    54 obs. of  6 variables:
##  $ type      : Factor w/ 3 levels "large","midsize",..: 3 2 2 2 2 1 1 2 1 2 ...
##  $ price     : num  15.9 33.9 37.7 30 15.7 20.8 23.7 26.3 34.7 40.1 ...
##  $ mpgCity   : int  25 18 19 22 22 19 16 19 16 16 ...
##  $ driveTrain: Factor w/ 3 levels "4WD","front",..: 2 2 2 3 2 2 3 2 2 2 ...
##  $ passengers: int  5 5 6 4 6 6 6 5 6 5 ...
##  $ weight    : int  2705 3560 3405 3640 2880 3470 4105 3495 3620 3935 ...
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
ggplot(cars, aes(x=price, y=weight))+geom_point()

ggplot(cars, aes(x=price, y=weight, color=type))+geom_point()

ggplot(cars, aes(x=price, y=weight, z=type))+geom_point()

Download the package “Scatterplot3d”

library(scatterplot3d)
plot(cars$weight~cars$price)

#scatterplot3d(cars[1:3])

scatterplot3d(cars$price, cars$weight, cars$type)

s3d<-scatterplot3d(cars$price, cars$weight, cars$type, pch=16, highlight.3d = FALSE, type="h",main="3D Scatterplot", angle = 75)

comments:

pch plotting “character”, i.e. symbol to use.

type character indicating the type of plot: “p” for points, “l” for lines, “h” for vertical lines to x-y-plane, etc.

Showing a linear regression:

library(rgl)

plot3d(cars$price, cars$type, cars$weight,pch=16,highlight.3d = TRUE,main="3D Scatterplot", type= "s", col="green", size=1)
rglwidget()

Plotly

library(plotly)
## 
## 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
p <- plot_ly(cars, x = ~price, y = ~type, z = ~weight, 
        marker = list(color = ~mpgCity, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE)) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'Price'),
                     yaxis = list(title = 'Type'),
                     zaxis = list(title = 'Weight')),
         annotations = list(
           x = 1.15,
           y = 1.05,
           text = 'Miles/(US) gallon',
          
           showarrow = FALSE
         ))

p
cars$passengers <- as.factor(c("4", "5", "6"))

p <- plot_ly(cars, x = ~price, y = ~type, z = ~weight, size = ~mpgCity,color= ~passengers, colors="Paired", sizes=c(10,50),
        marker = list(opacity = 0.8, sizemode = 'diameter'), 
text=~paste("mpg:", mpgCity, "<br>Number of Passenger:", passengers, "<br>DriveTrain:", driveTrain))%>%
  add_markers() %>%
  layout(title= "Cars Dataset",scene = list(xaxis = list(title = 'Price'),
                     yaxis = list(title = 'Type'),
                     zaxis = list(title = 'Weight')),
         annotations = list(
           x = 1,
           y = .95,
           text = 'Number Of Passenger',
          
           showarrow = TRUE
         )
         
         
         )
p