# Plotly is a graphical package that we can use in R
# we use the function lm (linear model). As we saw, mod is a list about various
# information for the model.
mod = lm(mpg ~ wt, data=mtcars)
x = mtcars$wt; y = mtcars$mpg
xax <- list(
title = "Weight", titlefont = list(family="Modern Computer Roman"))
yax <- list(
title = "Miles Per Gallon", titlefont = list(family="Modern Computer Roman"),
range = c(0,40))
# The first 2 inputs are the wt column and the mpg column.
# type = "scatter" means that our plot will be scatter plot.
# mode = "markers means that we only plot the points.
# The name of the first trace (we have two traces in this example) is data.
# We can also control the width and the height.
# Then we use the pipe operator %>%.
# After that, we add lines by using the command add lines.
# We name the second trace "fitted".
# Furthermore, we use the command layout and we add names to x and y axis.
# Finally, we specify the margins. l stands for left, r stands for right,
# b for bottom and t for top.
fig <- plot_ly(x=x, y=y, type="scatter", mode="markers", name="data",
width=690, height=300) %>%
add_lines(x = x, y = fitted(mod), name="fitted") %>%
layout(xaxis = xax, yaxis = yax) %>%
layout(margin=list(l=150,r=50,b=20,t=40))