> "friends are just like skittles" - dancingapple
“友谊就像彩虹糖” - dancingapple

其实太阳系最牛的R搞这些易如反掌,只是数据源有点不靠谱,略表失望。所以只是意思一下,点到即止。这次需要用到的是三个R库,“ jsonlite ”,解析Json格式用的;“igraph”,画一些基本网络图的;“rgl”,画一些3D图的。
这次笔记的目的是优美优雅地可视化那些甜蜜的友谊。
> It's so easy to manipulate data in the almighty R. Nevertheless, I was a little disappointed by the data source, which I did some fact-checking and found not so update-to-date or accurate. The purpose of this note is to show some proof-of-concept, not intended to serve as thorough analysis anyway.
We'll mainly use three R libraries, "jsonlite" for parsing Json formatted data, "igraph" for denoting and plotting some basic network/graph, and "rgl" for advanced 3D visualization.
The whole purpose is to elegantly present the sweet friendship in a colorful way.
# 1. 清内存,读库 | clear memory and load libraries
```
rm(list = ls())
require(jsonlite)
require(igraph)
require(rgl)
```
# 2. 设定一个简单函数 | create a simple function
读取你的粉丝和你粉的丝,并转化为一个网络图
> load your followers or the people you follow, and turn the relationship into a graph
```
ID.network <- function(user.ID){
#核心webapi | the core webapi access operation
fan.res <- fromJSON(paste("https://api.steemdata.com/busy.org/",
user.ID,"/followers", sep = ""))
follow.res <- fromJSON(paste("https://api.steemdata.com/busy.org/",
user.ID,"/following", sep = ""))
N.fan <- nrow(fan.res)
N.follow <- nrow(follow.res)
unique.name <- union(follow.res$name, c(fan.res$name,user.ID))
N.name <- length(unique.name)
mtx <- matrix(0,N.name,N.name)
colnames(mtx) <- rownames(mtx) <- unique.name
mtx[user.ID, follow.res$name] <- 1
mtx[fan.res$name, user.ID] <- 1
tmp.g <- graph_from_adjacency_matrix( mtx )
return(tmp.g)
}
```
# 3. 请出 @justyy 练手 | let's try an example
```
# 替换justyy为你想偷窥的ID
# feel free to replace "justyy" as any steemit ID you are curious
g1 <- ID.network(user.ID = "justyy")
plot(g1)
```
<br>哗~这就是他的朋友圈。。。但是有点挤,色泽单调
> wow~ that's his friendship... a little bit crowded, and dry in terms of color

<br> 好吧,来点颜色,比如256种彩虹色。并想象大家只是无名的小花。
> ok. let's paint some colors, e.g. a series of rainbow colors. Also, let's imagine friends are all anonymous flowers.
`plot(g1, vertex.color = rainbow(256), vertex.label=NA)`

太平,太飞机场了,通过rgl库的3D可视化,搞点凸凹有致~
> it's still flat. let's apply 3D visualization function in the rgl library:
'rglplot(g1, vertex.color = rainbow(256), vertex.label=NA)'
好了,可远观,可放大缩小旋转亵玩焉。。。
> done! then you can zoom-in & -out, rotate and play with it from all different angles



# 4. 搞个循环多挖点人? | make a loop to include more users
```
# 你可以直接定义想挖的人,把它们组织成一个字符串数组
# feel free to define a group of users you are curious about, and make them as a vector of strings
user.ID.vec <- c("justyy","susanli3769", "tumutanzi", "dapeng", "tvb", "bigyellow","jubi")
g.union <- ID.network(user.ID = user.ID.vec[1])
for(k in 2 : length(user.ID.vec)){
g2 <- ID.network(user.ID = user.ID.vec[k])
g.union <- union(g.union, g2)
}
```
<br>
如果他们是红花,大伙儿是绿叶
> let's pretend they are red flowers and the rest are green leaves.
<br>
```
plot.igraph(g.union, layout=layout_with_fr, vertex.label=NA, edge.arrow.size = 0.1,
vertex.color = ifelse(is.element(V(g.union)$name,user.ID.vec),
yes = "red", no = "green"),
vertex.size = ifelse(is.element(V(g.union)$name,user.ID.vec),
yes = 10, no = 3))
```
<br>

或者大家都是无名的彩色小花儿
又或者,大家都是亲密无间的甜蜜的彩虹糖
> or, all of us are just colorful flowers
or, friends are just like sweet sweet skittles
```
plot.igraph(g.union, layout=layout_with_fr,vertex.size=6,edge.arrow.size = 0.1,
vertex.label=NA, vertex.color = rainbow(256), edge.color = "green")
rglplot(g.union, layout=layout_with_fr,
vertex.color = rainbow(256), vertex.label=NA)
```
<br>

<br>

# 5. 打完,收工 | Done!
。。。截止到现在目前为止,我们都在干什么呢?其实我们在创作艺术,用太阳系最牛的R
>... wait a min, what are we doing so far? we are actually creating some art-works, using almighty R
又及,如果想再深挖的话,igraph有很多很牛的网络分析函数,比如Betweenness centrality(中文都不知道咋翻)。有心人可以细玩。
> BTW, there are tons of cool graph analytical functions in the "igraph" library, to compute key statistics of the network/graph. Enjoy if interested.