Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(jsonlite)
json <- fromJSON("~/Downloads/all-friends.json")
# negative friend counts?!?
idx <- with(json$nodes, friends_count > 0 & followers_count > 0)
f <- json$nodes[idx, ]
f <- data.frame(
log_friends_count = log(f$friends_count),
log_followers_count = log(f$followers_count)
)
# ensure reproducibility
set.seed(1237)
km <- kmeans(f, centers = 4, nstart = 100)
f$cluster <- km$cluster
f$name <- json$nodes$name[idx]
f$screen_name <- json$nodes$screen_name[idx]
f$author <- json$nodes$author[idx]
library(plotly)
p <- plot_ly(
f,
x = log_followers_count,
y = log_friends_count,
color = factor(cluster),
text = paste0(name, " (@", screen_name, ")"),
mode = "markers",
type = "scattergl",
opacity = 0.2,
inherit = FALSE
)
p2 <- add_trace(
p,
x = log_followers_count,
y = log_friends_count,
text = paste0(name, " (@", screen_name, ")"),
marker = list(symbol = "cross", color = toRGB("red")),
mode = "markers",
type = "scattergl",
name = "authors",
data = subset(f, author)
)
plotly_POST(p2)
# https://plot.ly/~agvd/1755
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment