Last active
August 29, 2015 14:22
-
-
Save mbojan/e137577f72abfe31c82f to your computer and use it in GitHub Desktop.
Rysowanie zespolow bezposrednio z 'obrazki' na Google drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# zainstalować 'googlesheets' jeśli nie ma | |
devtools::install_github("jennybc/googlesheets") | |
library(googlesheets) | |
library(igraph) | |
library(scales) | |
# autoryzacja | |
gs_auth() | |
# gs_ls() | |
alist_to_elist <- function(ego, adjlist) | |
{ | |
stopifnot( length(ego) == length(adjlist) ) | |
lens <- sapply(adjlist, length) | |
rval <- cbind(ego=rep(ego, lens), alter=unlist(adjlist)) | |
isna <- apply(rval, 1, function(x) any(is.na(x))) | |
rval[ !isna , ] | |
} | |
sheet <- "WF01" | |
vis <- function(sheet) | |
{ | |
require(googlesheets) | |
require(igraph) | |
require(scales) | |
# Wczytanie | |
obrazki <- gs_key("1IApsDIawqBGH1KuWpo22zJWAbrLJp5ft0oZDUuZ8UOs") | |
d <- gs_reshape_cellfeed(gs_read_cellfeed(obrazki, ws=sheet, range=cell_limits(rows=c(2,NA), cols=c(1,7)))) | |
names(d) <- c("ego", "kolor", "ksztalt", "frame", "grupa", "wspolpracownicy", "boss") | |
# fix adjlists | |
vnames <- c("grupa", "wspolpracownicy", "boss") | |
d[vnames] <- lapply(d[vnames], function(x) gsub(" *, *", ",", x)) | |
# wspolpraca | |
adjlist_wspolpraca <- lapply(strsplit(d$wspolpracownicy, ","), as.numeric) | |
g <- simplify(graph.edgelist(alist_to_elist(d$ego, adjlist_wspolpraca), directed=TRUE)) | |
# master-slave | |
boss_adjlist <- lapply(strsplit(d$boss, ","), as.numeric) | |
g <- add.edges(g, t(alist_to_elist(d$ego, boss_adjlist)), attr=list(boss=TRUE)) | |
E(g)$boss <- !is.na(E(g)$boss) | |
# kolor | |
pal <- brewer_pal(type="qual", palette=2)(length(unique(d$kolor))) | |
V(g)$color <- pal[d$kolor] | |
# shape | |
shps <- c("circle", "square") | |
V(g)$shape <- shps[d$ksztalt] | |
# grupy | |
l <- strsplit(d$grupa, ",") | |
u <- na.omit(unique(unlist(l))) | |
grupy <- lapply(u, function(gid) which(sapply(l, function(x) gid %in% x))) | |
# rys! | |
gb <- delete.edges(g, E(g)[!boss]) | |
gw <- as.undirected(delete.edges(g, E(g)[boss])) | |
lay <- layout.fruchterman.reingold(g) | |
plot(gb, layout=lay, vertex.shape="none", edge.curved=0.3, | |
edge.color="black", mark.groups=grupy) | |
plot(gw, layout=lay, add=TRUE, edge.color="red", edge.width=3) | |
} | |
# Zastosowanie | |
vis("WF01") | |
# TODO narysowanie wszystkich w pętli |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zamykam komentarze.
Wszystko przeniesione tu https://github.com/mbojan/recon, tam wrzucajcie issues.