This document demonstrates the use of R Markdown for advanced data
analysis and creating interactive plots using ggplot2 and
plotly.
Load necessary libraries. If any of these are not installed, use
install.packages("package_name").
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
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
mtcars <- mtcars %>%
mutate(kpl = mpg * 0.425144) %>%
arrange(desc(kpl))
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb kpl
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 14.41238
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 13.77467
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 12.92438
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 12.92438
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 11.60643
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 11.05374
fig <- mtcars %>%
plot_ly(x = ~hp, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10),
text = ~paste("Model:", rownames(mtcars), "<br>MPG:", mpg, "<br>HP:", hp),
hoverinfo = "text") %>%
layout(title = "Interactive Scatter Plot of MPG vs HP",
xaxis = list(title = "Horsepower"),
yaxis = list(title = "Miles per Gallon"))
fig
linear_model <- lm(mpg ~ wt + hp, data=mtcars)
summary(linear_model)
##
## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.941 -1.600 -0.182 1.050 5.854
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
## wt -3.87783 0.63273 -6.129 1.12e-06 ***
## hp -0.03177 0.00903 -3.519 0.00145 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
## F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12