create account

R是太阳系最牛的(2)-彩虹糖 | making skittles using R by eduter

View this thread on: hive.blogpeakd.comecency.com
· @eduter · (edited)
$5.00
R是太阳系最牛的(2)-彩虹糖 | making skittles using R
> "friends are just like skittles" - dancingapple

“友谊就像彩虹糖” - dancingapple 

![skittles_R.png](https://steemitimages.com/DQmZgYiF1FL9dgpuXdoDvJCM7DxgD1rdqM3v5Rp45G3467F/skittles_R.png)

其实太阳系最牛的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

![Rplot01.png](https://steemitimages.com/DQmZvDkUjz1z3nkxW8diQYURAz6qkBsLYDpRGEheSexsp3F/Rplot01.png)

<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)`

![rainbow_followers.jpg](https://steemitimages.com/DQmUduvRwCS85F2p5RXfXfCyUe37D8ywfrLwVmqefiaMdHL/rainbow_followers.jpg)


太平,太飞机场了,通过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 

![rainbow_followers_3Da.jpg](https://steemitimages.com/DQmXizq45kX8pyipcFpkXHugJfpxttXTpmipirS8HiTZ9Bq/rainbow_followers_3Da.jpg)

![rainbow_followers_3Db.jpg](https://steemitimages.com/DQmPCCYsD2nEvCqFWazCGT55fTLuG1cvtC4RhLaXnRnBbgp/rainbow_followers_3Db.jpg)

![rainbow_followers_3Dc.jpg](https://steemitimages.com/DQmeZC1uXSJbRwRPZFx9wFd7pLUVktGPVbsDRhWQoFyDnsb/rainbow_followers_3Dc.jpg)

# 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>
![red_flowers.jpg](https://steemitimages.com/DQmQrSviTtTdJTzu72vsMQUaJXn1rB1BCGNciHw5NLZZFiD/red_flowers.jpg)

或者大家都是无名的彩色小花儿
又或者,大家都是亲密无间的甜蜜的彩虹糖

> 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>

![red_flowers_b.jpg](https://steemitimages.com/DQmPiY8N2oNVcvcUCYKBzJdFHzByNRWqdquNau1yqw4bGqp/red_flowers_b.jpg)

<br>

![red_flowers_c.jpg](https://steemitimages.com/DQmZyUsB6NzT6u5THkBctE2RQeGDRySq6aPak7wQEUAehYn/red_flowers_c.jpg)

# 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.
👍  , , , , , , , , , , , , , ,
properties (23)
authoreduter
permlinkr-2-or-making-skittles-using-r
categorycn
json_metadata{"tags":["cn","cn-reader","cn-chat","programming","r"],"users":["justyy"],"image":["https://steemitimages.com/DQmZgYiF1FL9dgpuXdoDvJCM7DxgD1rdqM3v5Rp45G3467F/skittles_R.png","https://steemitimages.com/DQmZvDkUjz1z3nkxW8diQYURAz6qkBsLYDpRGEheSexsp3F/Rplot01.png","https://steemitimages.com/DQmUduvRwCS85F2p5RXfXfCyUe37D8ywfrLwVmqefiaMdHL/rainbow_followers.jpg","https://steemitimages.com/DQmXizq45kX8pyipcFpkXHugJfpxttXTpmipirS8HiTZ9Bq/rainbow_followers_3Da.jpg","https://steemitimages.com/DQmPCCYsD2nEvCqFWazCGT55fTLuG1cvtC4RhLaXnRnBbgp/rainbow_followers_3Db.jpg","https://steemitimages.com/DQmeZC1uXSJbRwRPZFx9wFd7pLUVktGPVbsDRhWQoFyDnsb/rainbow_followers_3Dc.jpg","https://steemitimages.com/DQmQrSviTtTdJTzu72vsMQUaJXn1rB1BCGNciHw5NLZZFiD/red_flowers.jpg","https://steemitimages.com/DQmPiY8N2oNVcvcUCYKBzJdFHzByNRWqdquNau1yqw4bGqp/red_flowers_b.jpg","https://steemitimages.com/DQmZyUsB6NzT6u5THkBctE2RQeGDRySq6aPak7wQEUAehYn/red_flowers_c.jpg"],"app":"steemit/0.1","format":"markdown"}
created2017-09-22 14:34:27
last_update2017-09-22 19:25:09
depth0
children4
last_payout2017-09-29 14:34:27
cashout_time1969-12-31 23:59:59
total_payout_value4.385 HBD
curator_payout_value0.615 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,253
author_reputation3,331,773,298,183
root_title"R是太阳系最牛的(2)-彩虹糖 | making skittles using R"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,622,278
net_rshares1,727,956,391,702
author_curate_reward""
vote details (15)
@dancingapple ·
神!请接受我的膜拜!!!
数学渣表示全程只看到一堆波波池!!还有彩虹糖!!
properties (22)
authordancingapple
permlinkre-eduter-r-2-or-making-skittles-using-r-20170922t153527913z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2017-09-22 15:35:33
last_update2017-09-22 15:35:33
depth1
children1
last_payout2017-09-29 15:35:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length37
author_reputation10,745,118,807,680
root_title"R是太阳系最牛的(2)-彩虹糖 | making skittles using R"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,627,685
net_rshares0
@eduter ·
又学会一个奇怪的词,波波池。但是的确很像
properties (22)
authoreduter
permlinkre-dancingapple-re-eduter-r-2-or-making-skittles-using-r-20170922t180817498z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2017-09-22 18:08:18
last_update2017-09-22 18:08:18
depth2
children0
last_payout2017-09-29 18:08:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20
author_reputation3,331,773,298,183
root_title"R是太阳系最牛的(2)-彩虹糖 | making skittles using R"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,640,374
net_rshares0
@oldman28 ·
太牛了!
properties (22)
authoroldman28
permlinkre-eduter-r-2-or-making-skittles-using-r-20170922t144232751z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2017-09-22 14:42:30
last_update2017-09-22 14:42:30
depth1
children0
last_payout2017-09-29 14:42:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4
author_reputation3,606,699,543,601
root_title"R是太阳系最牛的(2)-彩虹糖 | making skittles using R"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,622,929
net_rshares0
@tvb ·
太赞了!
properties (22)
authortvb
permlinkre-eduter-r-2-or-making-skittles-using-r-20170922t143732785z
categorycn
json_metadata{"tags":["cn"],"app":"steemit/0.1"}
created2017-09-22 14:37:39
last_update2017-09-22 14:37:39
depth1
children0
last_payout2017-09-29 14:37:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4
author_reputation35,178,037,825,802
root_title"R是太阳系最牛的(2)-彩虹糖 | making skittles using R"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,622,539
net_rshares0