Load Plotly Library and Dataset
library(plotly)
## Loading required package: ggplot2
##
## 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
data(mtcars)
Scatter Plot
x = mtcars$disp
y = mtcars$mpg
df_1 = data.frame(x, y)
plot_1 = plot_ly(df_1, x = ~x, y = ~y)
plot_1
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
Line Plot
x = c(1:length(mtcars$hp))
y = as.numeric(mtcars$hp)
df_2 = data.frame(x, y)
plot_2 = plot_ly(df_2, x = ~x, y = ~y, mode = 'lines')
plot_2
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
Bar Plot
x = mtcars$hp
y = mtcars$cyl
df_3 = data.frame(x, y)
plot_3 = plot_ly(df_3, x = ~x, y = ~y, type = 'bar')
plot_3
Histogram
x = mtcars$cyl
plot_4 = plot_ly(x = ~x, type = 'histogram')
plot_4