Plot%20basics in ggplot2

Convenient wrapper for creating a number of different types of plots using a consistent calling scheme and then convert them with ggplotly


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Create the plot using data from data.frame

p <-  qplot(mpg, wt, data = mtcars)

plotly::ggplotly(p)

Add coluor parameter

p <-  qplot(mpg, wt, data = mtcars, colour = cyl)

plotly::ggplotly(p)

Add size parameter

p <-  qplot(mpg, wt, data = mtcars, size = cyl)

plotly::ggplotly(p)

Create multiple miltiple plots with facets

p <-  qplot(mpg, wt, data = mtcars, facets = vs ~ am)

plotly::ggplotly(p)

Generate data and colour it using normal and uniform distirbutions

p <-  qplot(1:10, rnorm(10), colour = runif(10))

plotly::ggplotly(p)

Generate data using sequance

p <-  qplot(1:10, letters[1:10])

plotly::ggplotly(p)

Plot residual against fitted data

mod <- lm(mpg ~ wt, data = mtcars)
p <-  qplot(resid(mod), fitted(mod))

plotly::ggplotly(p)

Function that generates squares

f <- function() {
   a <- 1:10
   b <- a ^ 2
   qplot(a, b)
}
p <-  f()

plotly::ggplotly(p)

To set aesthetics, wrap in I()

p <-  qplot(mpg, wt, data = mtcars, colour = I("red"))

plotly::ggplotly(p)

Scatterplot auto-suggest using x,y inputs

qplot will attempt to guess what geom you want depending on the input both x and y supplied = scatterplot

p <-  qplot(mpg, wt, data = mtcars)

plotly::ggplotly(p)

Histogram auto-suggest, using only x input

stat_bin() using bins = 30. Pick better value with binwidth.

p <-  qplot(mpg, data = mtcars)

plotly::ggplotly(p)

Scatterplot auto-suggest only y input

x will be automatically set to seq_along(y)

p <-  qplot(y = mpg, data = mtcars)

plotly::ggplotly(p)

Use different geoms

path

p <-  qplot(mpg, wt, data = mtcars, geom = "path")

plotly::ggplotly(p)

boxplot and jitter

p <-  qplot(factor(cyl), wt, data = mtcars, geom = c("boxplot", "jitter"))

plotly::ggplotly(p)

dotplot

Bin width defaults to 1/30 of the range of the data. Pick better value with binwidth.

p <-  qplot(mpg, data = mtcars, geom = "dotplot")

plotly::ggplotly(p)