install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
# Load the required library
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
# Load the mtcars dataset
data(mtcars)
# Creating a scatter plot of horsepower vs. miles per gallon (mpg) using Plotly
plot_ly(data = mtcars, x = ~hp, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(color = 'blue'), name = 'Data') %>%
layout(title = "Relationship between Horsepower and MPG",
xaxis = list(title = "Horsepower"),
yaxis = list(title = "Miles per Gallon (MPG)")) %>%
add_trace(y = ~fitted(lm(mpg ~ hp, data = mtcars)), type = 'scatter', mode = 'lines',
line = list(color = 'red'), name = 'Linear Regression Line')
## A marker object has been specified, but markers is not in the mode
## Adding markers to the mode...