Make your scatter plots, line plots, bar plots, etc interactive
Plotly
Highcharts
crosstalk
Make D3.js plots in R (no javascript required!)
rCharts (D3.js)
d3scatter
networkD3
4/7/2017
Some super cool examples include:
Make your scatter plots, line plots, bar plots, etc interactive
Plotly
Highcharts
crosstalk
Make D3.js plots in R (no javascript required!)
rCharts (D3.js)
d3scatter
networkD3
library(ggplot2) g <- ggplot(txhousing, aes(x = date, y = sales, group = city)) + geom_line(alpha = 0.4) g
library(plotly)
g <- ggplot(txhousing, aes(x = date, y = sales, group = city)) +
geom_line(alpha = 0.4)
ggplotly(g, tooltip = c("city"))
g <- txhousing %>%
# group by city
group_by(city) %>%
# initiate a plotly object with date on x and median on y
plot_ly(x = ~date, y = ~median) %>%
# add a line plot for all texan cities
add_lines(name = "Texan Cities", hoverinfo = "none",
type = "scatter", mode = "lines",
line = list(color = 'rgba(192,192,192,0.4)')) %>%
# plot separate lines for Dallas and Houston
add_lines(name = "Houston",
data = filter(txhousing,
city %in% c("Dallas", "Houston")),
hoverinfo = "city",
line = list(color = c("red", "blue")),
color = ~city)
g
g %>% rangeslider
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
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")
library(networkD3) data(MisLinks, MisNodes) head(MisLinks, 3)
## source target value ## 1 1 0 1 ## 2 2 0 8 ## 3 3 0 10
head(MisNodes, 3)
## name group size ## 1 Myriel 1 15 ## 2 Napoleon 1 20 ## 3 Mlle.Baptistine 1 23
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.9, Nodesize = 3,
linkDistance = 100, fontSize = 20)
# devtools::install_github("mattflor/chorddiag")
library(chorddiag)
# plot the hair preferences data
hair.preference
## prefer ## have black blonde brown red ## black 11975 5871 8916 2868 ## blonde 1951 10048 2060 6171 ## brown 8010 16145 8090 8045 ## red 1013 990 940 6907
# make a chord diagram
groupColors <- c("#000000", "#FFDD89", "#957244", "#F26223")
chorddiag(hair.preference,
groupColors = groupColors, groupnamePadding = 40)
plotly references:
https://cpsievert.github.io/plotly_book/
crosstalk:
https://rstudio.github.io/crosstalk/using.html
chord diagram example:
http://stackoverflow.com/questions/14599150/chord-diagram-in-r