Let’s prepare the datasets we will use in this notebook:

library(tidyverse)
data(mtcars)
data(mpg)

mtcars2 <- mtcars
mtcars2$am <- factor(
  mtcars$am, labels = c('automatic', 'manual')
)

Let’s plot a simple graph using ggplot2 package:

ggplot(mtcars2, aes(hp, mpg, color = am)) +
  geom_point() + geom_smooth() +
  theme(legend.position = 'bottom')
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'


Chunk options allow us to control the dimentions of the output plot:

ggplot(mtcars2, aes(hp, mpg, color = am)) +
  geom_point() + geom_smooth() +
  theme(legend.position = 'bottom')
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'


If we want the plot to dynamically change its size, we can specify the width in percentages:

ggplot(mtcars2, aes(hp, mpg, color = am)) +
  geom_point() + geom_smooth() +
  theme(legend.position = 'bottom')
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

1 Hicharter

Highchirter package is an R wrapper for a Highcharts javascript library.

 library(highcharter)

1.1 Histogram

hchart(mtcars$mpg, name = "MPG", color = "#17b8b6") 

1.2 Barplot

mtcars2 %>%
  count(am) %>%
 hchart('column', hcaes(x = am, y = n))

More examples

2 dygraphs

Dygraphs gallery and documentation

This library is most useful to display time-series data:

 library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)

3 plotly

Plotly R Library

This library is easy to use if you are familiar with ggplot2 library:

 library(plotly)

g <- ggplot(mpg, aes(class))  
p <-  g + geom_bar(aes(fill = drv))
ggplotly(p)