Plotly scatterplots with diamond data.

library(plotly)
## Loading required package: ggplot2
## 
## 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
# Let's use the diamonds data set :
d <- diamonds[sample(nrow(diamonds), 1000), ]
 
# First we do a ggplot with several graphs
p <- ggplot(data = d, aes(x = carat, y = price)) +
  geom_point(aes(text = paste("Clarity:", clarity)), size = 4) +
  geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)
## Warning: Ignoring unknown aesthetics: text
# Then we make them interactive with ggplotly
p=ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p

Boxplots using Plotly

# A basic boxplot
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p

Overlaid Histograms

# Overlaid histogram of 2 vectors:
graph=plot_ly(x = rnorm(500), opacity = 0.6, type = "histogram") %>%
    add_trace(x = rnorm(500)+1) %>%
    layout(barmode="overlay")
graph

3-dimensional plot of a volcano

# Let's use the volcano dataset:
volcano[1:6, 1:6]
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  100  100  101  101  101  101
## [2,]  101  101  102  102  102  102
## [3,]  102  102  103  103  103  103
## [4,]  103  103  104  104  104  104
## [5,]  104  104  105  105  105  105
## [6,]  105  105  105  106  106  106
# 3D plot :
p=plot_ly(z = volcano, type = "surface")
p

Housing

library(plotly)
g <- ggplot(txhousing, aes(x = date, y = sales, group = city)) +
  geom_line(alpha = 0.4) 
ggplotly(g, tooltip = c("city"))

Linking with Crosstalk

Sometimes you have two plots of the same data and you want to be able to link the data from one plot to the data in the other plot. This, unsurprisingly, is called “linking”, and can be achieved using the crosstalk package.

library(crosstalk)
# define a shared data object
d <- SharedData$new(mtcars)
# make a scatterplot of disp vs mpg
scatterplot <- plot_ly(d, x = ~mpg, y = ~disp) %>%
  add_markers(color = I("navy"))
# define two subplots: boxplot and scatterplot
subplot(
  # boxplot of disp
  plot_ly(d, y = ~disp) %>% 
    add_boxplot(name = "overall", 
                color = I("navy")),
  # scatterplot of disp vs mpg
  scatterplot, 
  shareY = TRUE, titleX = T) %>% 
  layout(dragmode = "select")

Make subplots

# make subplots
p <- subplot(
  # histogram (counts) of gear
  plot_ly(d, x = ~factor(gear)) %>% 
    add_histogram(color = I("grey50")),
  # scatterplot of disp vs mpg
  scatterplot, 
  titleX = T
) 
layout(p, barmode = "overlay")

Globe Data with latitude and longitude can be mapped onto the globe. The code below reads in a list of airports that contains coordinates for each given location. The size of the airport (“small”, “medium”, “large”) is also included as metadata. Each point is plotted on the globe, colored by size:

library(jsonlite)
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(threejs)
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following object is masked from 'package:plotly':
## 
##     groups
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
# globejs 
airports <- fromJSON("https://raw.githubusercontent.com/jbrooksuk/JSON-Airports/master/airports.json")

# create a dataframe key of size to colors
sizecolors <-
  data_frame(size = c("small","medium","large"),
             color = c("lightblue", "gold","firebrick"))

airports <-
  airports %>%
  # some of the data is missing lat and lon
  filter(!is.na(lat) & !is.na(lon)) %>%
  # join with colors
  left_join(sizecolors)
## Joining, by = "size"
globejs(lat = airports$lat, 
        lon = airports$lon,
        color = airports$color)

Using R with Java

#install.packages("iplots",dep=TRUE)
library(iplots)
library(iplots)
## Loading required package: rJava
cyl.f <- factor(mtcars$cyl)
gear.f <- factor(mtcars$factor) 
attach(mtcars) 
## The following object is masked from package:ggplot2:
## 
##     mpg
ihist(mpg) # histogram 
## ID:1 Name: "Histogram (mpg)"
ibar(carb) # barchart
## ID:2 Name: "Barchart (carb)"
iplot(mpg, wt) # scatter plot
## ID:3 Name: "Scatterplot (wt vs mpg)"
ibox(mtcars[c("qsec","disp","hp")]) # boxplots 
## ID:4 Name: "Boxplot (qsec)"
ipcp(mtcars[c("mpg","wt","hp")]) # parallel coordinates
## ID:5 Name: "Parallel coord. plot (default)"
#imosaic(cyl.f,gear.f) # mosaic plot

Using GGobi

#source("http://www.ggobi.org/downloads/install.r")

Demos of GGobi

library(rggobi)
## 
## Attaching package: 'rggobi'
## The following object is masked from 'package:igraph':
## 
##     edges
g <- ggobi(iris)
clustering <- hclust(dist(iris[,1:4]),method="average")
glyph_colour(g[1]) <- cutree(clustering, 3)
g <- ggobi(mtcars)
d <- display(g[1],
"Parallel Coordinates Display")
variables(d)
## $X
##  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
##    1    2    3    4    5    6    7    8    9   10   11 
## 
## $Y
## named numeric(0)
## 
## $Z
## named numeric(0)
variables(d) <- list(X=8:6)
variables(d) <- list(X=8:1)
variables(d)
## $X
##   wt qsec   vs drat   hp disp  cyl  mpg 
##    6    7    8    5    4    3    2    1 
## 
## $Y
## named numeric(0)
## 
## $Z
## named numeric(0)

Animation

df <- data.frame(
x=1:2000,
y=sin(1:2000 * pi/20) + runif(2000, max=0.5)
)
g <- ggobi_longitudinal(df[1:100, ])
df_g <- g[1]
for(i in 1:1901) {
df_g[, 2] <- df[i:(i + 99), 2]
}

Longitudinal Data

ggobi_longitudinal(stormtracks, seasday, id)

GGally

library(devtools)
#install_github("ggobi/ggally")
library(GGally)

See the demo page http://ggobi.github.io/ggally/#ggally