Skip to content

Commit c659e17

Browse files
committed
draw_network example and title family
1 parent 106e5f2 commit c659e17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+275
-263
lines changed

DESCRIPTION

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ Imports:
2222
tidygraph,
2323
tidyr,
2424
vegan,
25-
viridisLite,
2625
ade4,
27-
viridis,
2826
gridExtra
2927
Suggests:
3028
testthat (>= 2.1.0),

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,3 @@ importFrom(tidygraph,centrality_betweenness)
7777
importFrom(tidygraph,centrality_degree)
7878
importFrom(tidygraph,edge_is_incident)
7979
importFrom(vegan,spantree)
80-
importFrom(viridisLite,viridis)

R/F_Graphs.R

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,33 @@
1919
#' @param node_groups optional vector separating the nodes into groups
2020
#' @param edge_groups optional matrix specifying groups of edges
2121
#' @param legend optional boolean for generating a legend when groups are provided
22-
#' @param pos optional legend position ("bottom", "top", "right" or "left")
22+
#' @param leg.pos optional legend position ("bottom", "top", "right" or "left")
23+
#' @param title.family font family of the title
24+
#' @param title.size font size of the title
2325
#' @return \itemize{
2426
#' \item{G} {the network as a ggplot2 object, with highlighted high betweenness nodes}
2527
#' \item{graph_data}{ data needed for plotting the network}
28+
#' \item{legend}{ ggplot2 information about the legend}
2629
#' }
2730
#' @export
2831
#' @importFrom tidygraph .N as_tbl_graph activate centrality_betweenness centrality_degree edge_is_incident
2932
#' @importFrom ggraph ggraph set_graph_style geom_edge_arc scale_edge_alpha_manual scale_edge_width_continuous geom_node_text geom_node_point scale_edge_colour_manual create_layout
3033
#' @importFrom tibble tibble rownames_to_column
3134
#' @importFrom ggplot2 aes theme labs scale_color_manual scale_size_manual ggplot_gtable ggplot_build element_text
3235
#' @importFrom dplyr mutate filter
33-
#' @importFrom viridisLite viridis
3436
#' @importFrom tibble as_tibble
3537
#' @importFrom gridExtra grid.arrange
36-
#' @examples adj_matrix= SimCluster(20,2,0.4, 10)
37-
#' draw_network(adj_matrix,"Cluster graph", layout="stress", shade=TRUE)
38+
#' @examples
39+
#' set.seed(1)
40+
#' adj_matrix= SimCluster(p=20,k=2,dens=0.5, r=10)
41+
#' groups=sample(3,20, replace=TRUE)
42+
#' draw_network(adj_matrix,"Cluster graph", layout="stress", shade=TRUE, btw_rank=3)$G
43+
#' draw_network(adj_matrix,"Cluster with groups of nodes", layout="stress", shade=TRUE, btw_rank=3,
44+
#' node_groups=groups, legend=TRUE,title.family="mono",title.size=12)$G
3845
draw_network<-function(adj_matrix,title="", label_size=4, curv=0,width=1, shade=FALSE, remove_isolated=FALSE,btw_rank=2,
3946
layout=NULL,stored_layout=NULL,nodes_label=NULL,nodes_size=c(2,5),pal_edges=NULL, pal_nodes=NULL,
40-
node_groups=NULL, edge_groups=NULL,legend=FALSE, pos="bottom"){
47+
node_groups=NULL, edge_groups=NULL,legend=FALSE, leg.pos="bottom",
48+
title.family="sans",title.size=12){
4149
adj_matrix=as.matrix(adj_matrix)
4250
p=nrow(adj_matrix) ; binary=FALSE
4351

@@ -53,7 +61,11 @@ draw_network<-function(adj_matrix,title="", label_size=4, curv=0,width=1, shade=
5361
min.width=ifelse(binary,0,0.1)
5462
#edges colour
5563
if(is.null(pal_edges)){
56-
pal_edges=viridisLite::viridis(5, option = "C")[c(3,2,4,1)][1:length(unique(edge_groups))]
64+
ncol_e=length(unique(edge_groups))
65+
if(ncol_e<5){
66+
pal_edges=c("#31374f","#1F968BFF","#B8DE29FF","de7065ff")[1:ncol_e]
67+
}else{stop("Please fill the pal_edges parameter")}
68+
5769
}
5870
#betweenness computations
5971

@@ -90,8 +102,8 @@ if(is.null(pal_edges)){
90102
}
91103

92104
#draw graph
93-
set_graph_style(family="sans")
94-
layout = ifelse(is.null(layout), "circle", layout)
105+
set_graph_style(title.family)
106+
layout = ifelse(is.null(layout), "nicely", layout)
95107

96108
if(is.null(stored_layout)){
97109
finallayout=create_layout(res,layout=layout)
@@ -120,20 +132,21 @@ if(is.null(pal_edges)){
120132
geom_node_text(aes(label = label), color = "black", size = label_size) +#,nudge_x = 0.3
121133
labs(title = title) + theme(plot.title = ggplot2::element_text(
122134
hjust=0.5,
123-
family = "Helvetica",
124-
size = 12 ))+
135+
family = title.family,
136+
size = title.size ),
137+
legend.position=leg.pos)+
125138
scale_edge_width_continuous(range=c(min.width,width))
126139
leg=NULL
127140
if(!is.null(node_groups) & legend ){#add legend if groups provided
128141
tmp=ggplot(data.frame(node_groups=as.factor(node_groups), row1=adj_matrix[1,], row2=adj_matrix[2,]),
129142
aes(row1, row2, color=node_groups))+
130-
geom_point()+scale_color_manual("",values=pal_nodes)+theme(legend.position=pos)
143+
geom_point()+scale_color_manual("",values=pal_nodes)+theme(legend.position=leg.pos)
131144
tmp <- ggplot_gtable(ggplot_build(tmp))
132145
leg <- tmp$grobs[[which(sapply(tmp$grobs, function(x) x$name) == "guide-box")]]
133146

134147
}
135148

136-
return(list(G=g,legend=leg,graph_data=res))
149+
return(list(G=g,graph_data=res,legend=leg))
137150
}
138151

139152
utils::globalVariables(c("edges", "weight", "nodes", "btw.weights", "btw",

R/F_inference.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ Psi_alpha <- function(CorY, n, cond.tol=1e-10){
141141
#' @importFrom tibble tibble rowid_to_column
142142
#' @importFrom ggplot2 ggplot geom_point geom_line theme_minimal labs aes
143143
#' @importFrom stats optim
144-
#' @examples n=30
144+
#' @examples
145+
#' set.seed(1)
146+
#' n=50
145147
#' p=10
146-
#' S=5
147148
#' Y=data_from_scratch("tree",p=p,n=n)$data
148-
#' beta = matrix(1/10,10,10)
149+
#' mean.val=exp((-(p-2)*log(p))/(p-1))
150+
#' beta = matrix(mean.val, p, p); diag(beta)=0
149151
#' psi=Psi_alpha(cor(Y), n)$psi
150152
#' FitEM = FitBeta(beta.init=beta, psi=psi, maxIter = 6, sum.weights=sum(beta))
151153
FitBeta <- function(beta.init, psi, maxIter=50, eps = 1e-6, unlinked=NULL,sum.weights){
@@ -394,7 +396,8 @@ StATS<-function(Pmat, nlambda=100, stab.thresh=0.9,plot=FALSE){
394396
#'}
395397
#'custom_resample=ResampleEMtree(simu$data,S=S,cores = 1,user_covariance_estimation=estimSigma)
396398
#'
397-
#'# We then run the stability selection to find the optimal selection frequencies, for a stability of 85%:
399+
#'# We then run the stability selection to find the optimal selection frequencies,
400+
#'# for a stability of 85%:
398401
#'stab_default=StATS(default_resample$Pmat, nlambda=50, stab.thresh=0.85,plot=TRUE)
399402
#'stab_custom=StATS(custom_resample$Pmat, nlambda=50, stab.thresh=0.85,plot=TRUE)
400403
#'

README.Rmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ knitr::opts_chunk$set(
1717
[![Codecov test coverage](https://codecov.io/gh/Rmomal/EMtree/branch/master/graph/badge.svg)](https://codecov.io/gh/Rmomal/EMtree?branch=master)
1818
[![DOI](https://zenodo.org/badge/166967948.svg)](https://zenodo.org/badge/latestdoi/166967948)
1919

20-
> `EMtree` infers direct species association networks, implementing the procedure described in [Momal *et al.*](https://besjournals.onlinelibrary.wiley.com/doi/abs/10.1111/2041-210X.13380). This package uses averages over spanning trees within an Expectation-Maximization algorithm to infer conditional dependence networks, and involves plotting funcitonalities (using `ggraph` and `tydigraph`). By default, it uses the Poisson log-Normal Model ([PLNmodels](https://github.com/jchiquet/PLNmodels>)) to accomodate abundance data. However, EMtree is an inference procedure which only requires an estimate of a Gaussian covariance matrix, and can be used with any model which either use Gaussian latent variables, Gaussian copulas, or Gaussian data transformations.
20+
> `EMtree` infers direct species association networks, implementing the procedure described in [Momal *et al.*](https://besjournals.onlinelibrary.wiley.com/doi/abs/10.1111/2041-210X.13380). This package uses averages over spanning trees within an Expectation-Maximization algorithm to infer conditional dependence networks, and involves plotting funcitonalities (using `ggraph` and `tydigraph`).
21+
22+
By default, it uses the Poisson log-Normal Model ([PLNmodels](https://github.com/jchiquet/PLNmodels>)) to accommodate abundance data. However, EMtree is an inference method which only requires an estimate of a Gaussian covariance matrix, and can be used with any model which either use Gaussian latent variables, Gaussian copulas, or Gaussian data transformations.
2123

2224
## Installation
2325

@@ -28,7 +30,7 @@ knitr::opts_chunk$set(
2830
```{r,eval=FALSE}
2931
required_CRAN <- c("Matrix", "purrr","parallel", "mvtnorm", "vegan","huge",
3032
"ggplot2", "magrittr", "dplyr","tidyr", "tibble",
31-
"PLNmodels","ggraph", "tidygraph", "ade4", "viridisLite")
33+
"PLNmodels","ggraph", "tidygraph", "ade4")
3234
not_installed_CRAN <- setdiff(required_CRAN, rownames(installed.packages()))
3335
if (length(not_installed_CRAN) > 0) install.packages(not_installed_CRAN)
3436
```

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ coverage](https://codecov.io/gh/Rmomal/EMtree/branch/master/graph/badge.svg)](ht
1515
> This package uses averages over spanning trees within an
1616
> Expectation-Maximization algorithm to infer conditional dependence
1717
> networks, and involves plotting funcitonalities (using `ggraph` and
18-
> `tydigraph`). By default, it uses the Poisson log-Normal Model
19-
> ([PLNmodels](https://github.com/jchiquet/PLNmodels%3E)) to accomodate
20-
> abundance data. However, EMtree is an inference procedure which only
21-
> requires an estimate of a Gaussian covariance matrix, and can be used
22-
> with any model which either use Gaussian latent variables, Gaussian
23-
> copulas, or Gaussian data transformations.
18+
> `tydigraph`).
19+
20+
By default, it uses the Poisson log-Normal Model
21+
([PLNmodels](https://github.com/jchiquet/PLNmodels%3E)) to accommodate
22+
abundance data. However, EMtree is an inference method which only
23+
requires an estimate of a Gaussian covariance matrix, and can be used
24+
with any model which either use Gaussian latent variables, Gaussian
25+
copulas, or Gaussian data transformations.
2426

2527
## Installation
2628

@@ -31,7 +33,7 @@ coverage](https://codecov.io/gh/Rmomal/EMtree/branch/master/graph/badge.svg)](ht
3133
``` r
3234
required_CRAN <- c("Matrix", "purrr","parallel", "mvtnorm", "vegan","huge",
3335
"ggplot2", "magrittr", "dplyr","tidyr", "tibble",
34-
"PLNmodels","ggraph", "tidygraph", "ade4", "viridisLite")
36+
"PLNmodels","ggraph", "tidygraph", "ade4")
3537
not_installed_CRAN <- setdiff(required_CRAN, rownames(installed.packages()))
3638
if (length(not_installed_CRAN) > 0) install.packages(not_installed_CRAN)
3739
```

0 commit comments

Comments
 (0)