Skip to content

Commit

Permalink
Fix to accomidate ggplot 2.2.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
minimaxir committed Nov 24, 2016
1 parent 76ad030 commit f252873
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.Rhistory
.RData
.Ruserdata
plotly/
plotly/
flights-graph-ggraph*
42 changes: 35 additions & 7 deletions flights-graph.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "How to Create an Interactive WebGL Network Graph Using R"
title: "How to Create an Interactive WebGL Network Graph Using R and Plotly"
author: "Max Woolf (@minimaxir)"
date: "November 15th, 2016"
output:
Expand All @@ -21,16 +21,21 @@ This notebook is licensed under the MIT License. If you use the code or data vis
Setup the R packages.

```{r}
# must install ggnetwork using from source to avoid ggplot2 2.2.0 issue
# install.packages("ggnetwork", type="source")
library(dplyr)
library(nycflights13)
library(igraph)
library(intergraph)
library(sna)
library(ggplot2)
library(ggnetwork)
library(viridis)
library(plotly)
library(htmlwidgets)
sessionInfo()
```

The `nycflights13` package contains a `flights` dataset.
Expand Down Expand Up @@ -110,7 +115,7 @@ E(net)$endlon <- end_loc$lon
Use `ggnetwork` to transform the network to a `ggplot` friendly format.

```{r}
df_net <- ggnetwork(net, layout = "fruchtermanreingold", weights="weight", niter=5000, arrow.gap=0)
df_net <- ggnetwork(net, layout = "fruchtermanreingold", weights="weight", niter=50000, arrow.gap=0)
df_net %>% head()
```

Expand All @@ -119,7 +124,7 @@ plot <- ggplot(df_net, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(aes(color = color), size=0.4, alpha=0.25) +
geom_nodes(aes(color = color_v, size = degree, text=text)) +
ggtitle("Network Graph of U.S. Flights Outbound from NYC in 2013") +
scale_color_manual(labels=c("LGA", "EWR", "JFK", "Others"),
scale_color_manual(labels=c("EWR", "JFK", "LGA", "Others"),
values=c(colors, "#1a1a1a"), name="Airports") +
guides(size=FALSE) +
theme_blank()
Expand All @@ -134,7 +139,8 @@ plot %>% ggplotly(tooltip="text") %>% toWebGL()
Save interactive plot locally to disk using `htmlwidgets` for uploading to my website. (this only saves the data/layout; you will need to provide the relevant plot.ly javascript on your own website)

```{r}
plot %>% ggplotly(tooltip="text", width="100%", height=600) %>%
plot %>% ggplotly(tooltip="text") %>%
toWebGL() %>%
saveWidget("ggplot-graph-1.html", selfcontained=F, libdir="plotly")
```

Expand All @@ -145,7 +151,7 @@ plot <- ggplot(df_net, aes(x = lon, y = lat, xend = endlon, yend = endlat)) +
geom_edges(aes(color = color), size=0.4, alpha=0.25) +
geom_nodes(aes(color = color_v, size = degree, text=text)) +
ggtitle("Locations of U.S. Flights Outbound from NYC in 2013") +
scale_color_manual(labels=c("LGA", "EWR", "JFK", "Others"),
scale_color_manual(labels=c("EWR", "JFK", "LGA", "Others"),
values=c(colors, "#1a1a1a"), name="Airports") +
guides(size=FALSE) +
theme_blank()
Expand All @@ -158,10 +164,32 @@ plot %>% ggplotly(tooltip="text") %>% toWebGL()
```

```{r}
plot %>% ggplotly(tooltip="text", width="100%", height=600) %>%
plot %>% ggplotly(tooltip="text") %>%
toWebGL() %>%
saveWidget("ggplot-graph-2.html", selfcontained=F, libdir="plotly")
```

# Simplified Version

Minimum amount of code needed to demonstate proof-of-concept for article.

```{r}
df_edges <- flights %>% group_by(origin, dest) %>% summarize(weight = n())
net <- graph.data.frame(df_edges, directed = T)
V(net)$degree <- centralization.degree(net)$res
df_net <- ggnetwork(net, layout = "fruchtermanreingold", weights="weight", niter=5000)
plot <- ggplot(df_net, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(size=0.4, alpha=0.25) +
geom_nodes(aes(size = degree, text=vertex.names)) +
ggtitle("Network Graph of U.S. Flights Outbound from NYC in 2013") +
theme_blank()
plot
plot %>% ggplotly(tooltip="text") %>% toWebGL()
```


# LICENSE

The MIT License (MIT)
Expand Down
290 changes: 234 additions & 56 deletions flights-graph.nb.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions ggplot-graph-1.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions ggplot-graph-2.html

Large diffs are not rendered by default.

0 comments on commit f252873

Please sign in to comment.