Nothing
#' @importFrom rlang .data
get_ggplot_ind_map <- function(scores, axes_label, main) {
if (length(axes_label) > 1 ) {
if (length(axes_label) >= 3) {
message("Three axes or more provided. Using only the first two")
}
p <- ggplot(scores, aes(x = .data$a1, y = .data$a2, label = .data$names, colour = .data$labels)) +
geom_hline(yintercept = 0, colour = "gray65") +
geom_vline(xintercept = 0, colour = "gray65") +
geom_text(alpha = 0.8, size = 4) +
ggtitle(main) +
theme_bw() +
labs(x = axes_label[1], y = axes_label[2])
} else {
p <- ggplot(scores, aes(x = .data$a1, group = .data$labels, fill = .data$labels, colour = .data$labels)) +
geom_density(alpha = .4) +
geom_rug() +
ggtitle(main) +
theme_bw() +
labs(x = axes_label[1])
}
p
}
get_ggplot_corr <- function(correlations, axes_label, main) {
if (length(axes_label) > 1 ) {
if (length(axes_label) >= 3) {
message("Three axes or more provided. Using only the first two")
}
## data frame with arrows coordinates
arrows <- data.frame(x1 = rep(0, nrow(correlations)), y1 = rep(0, nrow(correlations)),
x2 = correlations$axe1, y2 = correlations$axe2,
names = correlations$names, labels = correlations$labels)
## geom_path will do open circles
p <- ggplot() + xlim(-1.1, 1.1) + ylim(-1.1, 1.1) +
geom_hline(yintercept = 0, colour = "gray65") +
geom_vline(xintercept = 0, colour = "gray65") +
geom_segment(data = arrows, aes(x = .data$x1, y = .data$y1, xend = .data$x2, yend = .data$y2, colour = .data$labels)) +
geom_text(data = correlations, aes(x = .data$axe1, y = .data$axe2, label = .data$names, colour = .data$labels), size=3) +
theme_bw() + theme(legend.position = "none") + ggtitle(main) + labs(x = axes_label[1], y = axes_label[2])
} else {
sign <- rep(c(-1,1), each = ceiling(nrow(correlations)/2))[1:nrow(correlations)]
value <- stats::runif(nrow(correlations), 0.25, 1)
arrows <- data.frame(x1 = correlations$axe1, y1 = rep(0, nrow(correlations)),
x2 = correlations$axe1, y2 = sign * value,
names = correlations$names, labels = correlations$labels)
p <- ggplot(arrows) + xlim(-1.1, 1.1) + ylim(-1.1, 1.1) +
geom_hline(yintercept = 0, colour = "gray65") +
geom_segment(aes(x = .data$x1, y = .data$y1, xend = .data$x2, yend = .data$y2, colour = .data$labels)) +
geom_text(aes(x = .data$x2, y = .data$y2, label = rownames(correlations), colour = .data$labels), vjust = -.5, angle = 90, size=5) +
geom_point(aes(x = .data$x2, y = 0)) +
theme_bw() + theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(), legend.position = "none") + ggtitle(main) +
labs(x = paste("correlation with", axes_label[1]))
}
p
}
get_ggplot_corr_circle <- function(correlations, axes_label, main) {
p <- get_ggplot_corr(correlations, axes_label, main)
if (length(axes_label) > 1 ) {
## add correlation circle
corcir <- circle(c(0, 0), npoints = 100)
p <- p + geom_path(data = corcir, aes(x = .data$x,y = .data$y), colour = "gray65")
}
p
}
get_ggplot_corr_square <- function(correlations, axes_label, main) {
p <- get_ggplot_corr(correlations, axes_label, main)
if (length(axes_label) > 1 ) {
## add square of size 2 centered at the origin
square <- data.frame(x = c(-1, -1, 1, 1, -1), y = c(-1, 1, 1, -1, -1))
p <- p + geom_path(data = square, aes(x = .data$x,y = .data$y), colour = "gray65")
}
p
}
circle <- function(center = c(0, 0), radius = 1, npoints = 100) {
r = radius
tt = seq(0, 2 * pi, length = npoints)
xx = center[1] + r * cos(tt)
yy = center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
g_legend <- function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
# courtesy of S. Donnet
plot_matrix = function(Mat, rowFG = "sample", colFG = "variable", clustering = NULL, log_scale = TRUE){
n1 <- dim(Mat)[1]
n2 <- dim(Mat)[2]
u <- range(c(Mat))
binary = FALSE
val <- sort(unique(c(Mat)))
if (setequal(val ,c(0,1))) {binary = TRUE}
if (!is.null(clustering)) {
oRow <- oCol <- order(clustering)
uRow <- cumsum(table(clustering)) + 0.5
uRow <- uRow[-length(uRow)]
sepRow <- as.data.frame(uRow)
Mat <- Mat[oRow, , drop = FALSE]
names(sepRow) <- 'sep'
sepRow = n1 - sepRow
}
index_row = rep(1:dim(Mat)[1],each = dim(Mat)[2])
index_col = rep(1:dim(Mat)[2],dim(Mat)[1])
melted_Mat = data.frame(n1 - index_row , index_col)
link = rep(-10,dim(Mat)[2]*dim(Mat)[1])
for (k in 1:(dim(Mat)[2] * dim(Mat)[1])) {link[k] = Mat[index_row[k],index_col[k]]}
melted_Mat$count = link
if (binary){
melted_Mat$count <- as.factor(melted_Mat$count)
}
colnames(melted_Mat) <- c('index_row', 'index_col', 'count')
g <- ggplot(data = melted_Mat, aes(y = index_row, x = index_col, fill = count))
g <- g + geom_tile()
if (!binary & log_scale) {g <- g + scale_fill_viridis_b(limits = u, na.value = "transparent", trans = "log10")}
if (!binary & !log_scale) {g <- g + scale_fill_viridis_b(limits = u, na.value = "transparent")}
if (binary) {g <- g + scale_fill_manual(breaks = c("0", "1"),values = c("white", "black"),na.value = "transparent")}
g <- g + scale_x_discrete(drop = FALSE, expand = expansion(0, 0)) + scale_y_discrete(drop = FALSE, expand = expansion(0, 0))
g <- g + theme(axis.text.x = element_text(angle = 270, hjust = 0))
g <- g + labs(x = colFG, y = rowFG) + theme(aspect.ratio = n1/n2)
if (!is.null(clustering)){
g <- g + geom_hline(data = sepRow, mapping = aes(yintercept = .data$sep),col = 'grey')
}
g
}
#' @importFrom grDevices rgb adjustcolor
.plot_network = function(net ,
type ,
output ,
edge.color = c("#F8766D", "#00BFC4"),
remove.isolated = FALSE,
node.labels = NULL,
layout = layout_in_circle,
edge.alpha = 0.2,
plot = TRUE) {
if (output == "igraph") {
G <- graph_from_adjacency_matrix(net, mode = "undirected", weighted = TRUE, diag = FALSE)
if (!is.null(node.labels)) {
igraph::V(G)$label <- node.labels
} else {
igraph::V(G)$label <- colnames(net)
}
## Nice nodes
V.deg <- degree(G)/sum(degree(G))
igraph::V(G)$label.cex <- V.deg / max(V.deg) + .5
igraph::V(G)$size <- V.deg * 100
igraph::V(G)$label.color <- rgb(0, 0, .2, .8)
## Nice edges: both the width and the opacity carry the strength of the edge,
## so that a dense network does not collapse into an unreadable solid blob.
## Weights are scaled by the largest one, so the scale is relative to the
## network at hand rather than to the (model-dependent) range of the values.
E.col <- ifelse(igraph::E(G)$weight > 0, edge.color[1], edge.color[2])
if (type == "support") {
igraph::E(G)$width <- abs(igraph::E(G)$weight)
igraph::E(G)$color <- E.col
} else {
E.str <- abs(igraph::E(G)$weight)
E.str <- if (max(E.str) > 0) E.str / max(E.str) else rep(1, length(E.str))
igraph::E(G)$width <- 15 * abs(igraph::E(G)$weight)
igraph::E(G)$color <- mapply(adjustcolor, E.col,
alpha.f = edge.alpha + (1 - edge.alpha) * E.str,
USE.NAMES = FALSE)
}
if (remove.isolated) {
G <- delete.vertices(G, which(degree(G) == 0))
}
## vertex.frame.color = NA must be passed as a plot() argument, not a vertex
## attribute: as an attribute it triggers a spurious "contains NAs" warning
## from igraph, even though NA is its own documented way of requesting no frame.
if (plot) plot(G, layout = layout, vertex.frame.color = NA)
}
if (output == "corrplot") {
if (plot) {
if (ncol(net) > 100)
colnames(net) <- rownames(net) <- rep(" ", ncol(net))
G <- net
diag(net) <- 0
corrplot(as.matrix(net), method = "color", is.corr = FALSE, tl.pos = "td", cl.pos = "n", tl.cex = 0.5, type = "upper")
} else {
G <- net
}
}
invisible(G)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.