knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning=FALSE, message=FALSE ) options(width = 300)
Assuming that the following dataframe is what you initially have at your disposal.
library(somar) df <- somar::experimental_data.df str(df)
In order to be displayed in a graph, the dataframe informations must first be correctly selected and filtered.
metaframe <- separate_data(df, "\\_[A-z]+\\_", inverse = TRUE) dataframe <- separate_data(df, "\\_[A-z]+\\_") params <- prefix_vector(dataframe, "_") params_dataframes_list <- list() for (param in params) { params_dataframes_list[[param]] <- separate_data(dataframe, paste("^", param, sep="")) }
This gives us in this case 4 dataframe.
metaframe
params_dataframes_list
After that it can be interesting to gather the R, CI and P data according to the metadata.
dataframe_units <- list() for (n in 1:nrow(metaframe)) { dataframe_units[[n]] <- list() for (metadata in names(metaframe)) { dataframe_units[[n]][[metadata]] <- as.numeric(metaframe[n, metadata]) } final_dataframe_unit <- data.frame() for (i in 1:length(params)) { temp_dataframe <- params_dataframes_list[[i]][n,] row.names(temp_dataframe) <- params[i] temp_dataframe <- substract_prefix_from_all_column(temp_dataframe, paste(params[i], "_", sep="")) final_dataframe_unit <- rbind(final_dataframe_unit, temp_dataframe) } dataframe_units[[n]][["data"]] <- final_dataframe_unit } dataframe_units[[1]]
In this part it is first described how to construct a graph from one of the dataframes of the previously constructed list.
It is first necessary to build the structure of the graph and to assign the parameters recorded in one of the dataframes built previously.
library(igraph) selected.df <- dataframe_units[[1]][["data"]] g <- graph_structure_construction(selected.df) plot(g)
For the graph to respect a shape evoking somatotopy, a half circle must be passed to it as a layout. Note that for some reason I don't know, it is also necessary to modify the aspect ratio so that the coordinates of the semicircle are correctly reflected on the graph.
layout <- layout_in_semicircle(g) plot(g, layout = layout, asp = 0.4)
For the graph to evoke many notions directly related to somatotopy, the nodes must then be reordered.
g <- nodes_permutation_somatotopy(g) plot(g, layout = layout, asp = 0.4)
Finally, make sure that the attributes of the graph are visible on the vertices and edges. At this stage, you have the choice between changing the aesthetics of the graph according to your criteria or using one of the functions to display a default aesthetics.
Beware the graph ethic depends strongly on the attributes that have been added to your dataframe. So it goes without saying that this part will certainly have to be customized according to your needs.
color_scale <- colorRampPalette(c("red4", "red1", "yellow", "lawngreen", "forestgreen")) precision_scale <- 1000 color_pallet <- color_scale(precision_scale); edgeColor <- function(r) { color_pallet[abs(signif(r, 3)) * precision_scale] } E(g)$color <- edgeColor(E(g)$R)
edge_max_width <- 50 edge_min_width <- 1 edgeWidth <- function(r, minWidth, maxWidth) { range_of_values <- maxWidth - minWidth unit <- range_of_values / precision_scale minWidth + unit * abs(signif(r, 2)) * precision_scale } E(g)$width <- edgeWidth(E(g)$R, edge_min_width, edge_max_width)
E(g)$lty <- ifelse(E(g)$P > 0.05, "dotted", "solid")
E(g)$curved <- seq(0, 0.7, length = ecount(g))
V(g)$selected <- grepl(as_ids(V(g)), pattern="^S") V(g)$label <- ifelse(V(g)$selected, substract_prefix(V(g)$name, "Sel"), substract_prefix(V(g)$name, "NnSel")) V(g)$color <- ifelse(V(g)$selected, "gray60", "gray90")
V(g)$size <- 40 V(g)$frame.color <- ifelse(degree(g) > 1, "forestgreen", "gray") V(g)$frame.width <- 2 V(g)$label.cex <- 2 V(g)$label.family="" V(g)$label.cex=1.4 V(g)$label.color="black" plot(g, layout = layout, asp = 0.4)
You can select one of the default esthetic functions (like default_vertex_aes(g), ...) or apply them all with the following function.
g <- default_graph_aesthetic(g) plot(g, layout = layout, asp = 0.4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.