April 13, 2017

R Markdown

This is an R Markdown presentation created using Knit, which allows the capability to include content as well as output of embedded R code chunks within a document.

In this presentation, I will also demonstrate how a ggplot object can be turned into an interactive web graphic using the plotly package.

Plotly Code

#Load packages
library(plotly);library(dplyr);library("datasets")

#Read dataset
data(mtcars)

#Create plot
p <-ggplot(mtcars, aes(x = mpg, y = wt, col = factor(cyl))) + 
        geom_point() + 
        labs(title = "Weight vs Mpg", x = "Mpg",y= "Weight") +
        theme(legend.title = element_blank())

#Pass ggplot object to plotly function and format
ggplotly(p) %>%  add_annotations( text="Cylinder", 
                  xref="paper", yref="paper",
                  x=1.02, xanchor="left",
                  y=0.8, yanchor="bottom",   
                  legendtitle=TRUE, showarrow=FALSE ) %>%
                  layout( legend=list(y=0.8, yanchor="top" ) )

mtcars Interactive Plotly Scatter

Thank You!