R/visualizeTools.R

#' alpha
#'
#' my own implementation to add alpha to colors
alpha <- function(color,alpha){
    rgb(t(col2rgb(color)/255),alpha=alpha)
}


getCCFD <- function(){
    load("hundreds.bin")

    ca=hundreds$cascades
    res=cbind.data.frame(hundreds$summary,getScoreCascade(ca))
    png("CCDF_size.png")
    plot(result$size,(1-ecdf(result$size)(result$size))*100,log="xy",ylab="CCDF(%)",xlab="Cascade Size" )
    dev.off()
}


#'plot the CCFD following \emph{Vosoughi et al. (2018)} on a log-log axis and color it given utility
#'@param s : a two dimension vector with size of cascade and utility of the cascade for a simulation
#'@param ... : parameters passed to plot
#'@return nothing
plotCCFD_U <- function(s,pch=20,cex=2,col=NULL,legend=TRUE,...){
    s=s[order(s$size),]
    if(is.null(col)){
    br=seq(0,1,length.out=10)
    cls=cut(s$U,breaks=br)
    cols=alpha(heat.colors(length(br)),.4)
    names(cols)=levels(cls)
    col=cols[cls]
    }
    plot(c(0,s$size),c(1,(1-ecdf(s$size)(s$size)))*100,log="xy",ylab="CCDF(%)",xlab="Cascade Size",col=col,pch=pch,cex=cex,...)
    if(legend)legend("bottomleft",legend=round(br,digit=1),col=cols,pch=pch,title="U")
}

#'plot the CCFD following \emph{Vosoughi et al. (2018)} on a log-log axis and color it given utility
#'@param s : a two dimension vector with size of cascade and utility of the cascade for a simulation
#'@param ... : parameters passed to plot
#'@return nothing
pointsCCFD_U <- function(s,pch=20,cex=2,col=NULL,lenged=TRUE,...){
    s=s[order(s$size),]
    if(is.null(col)){
        br=seq(0,1,length.out=10)
        cls=cut(s$U,breaks=br)
        cols=alpha(heat.colors(length(br)),.4)
        names(cols)=levels(cls)
        col=cols[cls]
    }
    points(s$size,(1-ecdf(s$size)(s$size))*100,col=col,pch=pch,cex=cex,...)
}

#'plot the CCFD following \emph{Vosoughi et al. (2018)} on a log-log axis
#'@param s : sample of sizes
#'@param ... : parameters passed to plot
#'@return nothing
plotCCFD <- function(s,pch=20,cex=1,xlab="",ylab="CCDF(%)",...){
    ccfd=ccfd(s)
    plot(ccfd[,"x"],ccfd[,"y"],log="xy",,pch=pch,ylab=ylab,cex=cex,xlab=xlab,...)
}

#' add points of the CCFD following \emph{Vosoughi et al. (2018)}
#'@param s : sample of sizes
#'@param ... : parameters passed to plot
#'@return nothing
pointsCCFD <- function(s,pch=20,cex=1,...){
    ccfd=ccfd(s)
    points(ccfd[,"x"],ccfd[,"y"],pch=pch,cex=cex,...)
}



#' Calculate the CCFD following \emph{Vosoughi et al. (2018)}
#'@param size : sample of sizes
#'@return a order table with two column with first column : frequency and second column the probality of the frequency  
ccfd <- function(size){
    total=length(size) 
    size=size[order(size)]
    counts=unique(size)
    id=unique(match(counts,size))
    p=sapply(id,function(i)length(size[i:total])) 
    p=p/total * 100
    x=counts
    y=p
    return(cbind(x,y))
}


#' return a vector of color that follow the values of utilities 
#'@param utilites : a vector with value we want to match
#'@return a vector of length = `length(utilities)` of colors with names=sort(names(utilities))
getUCols <- function(utilities){
    unique_u = unique(utilities) #all possible utility as defined in the default function `cascades3D`
    cols=heat.colors(length(unique_u))
    names(cols)=sort(unique_u)
    return(cols[as.character(utilities)])
}
simoncarrignon/twitter-spread documentation built on May 23, 2019, 5:04 a.m.