How to customize the size of the nodes in ggnet2 - network-analysis

I want to know how to customize the size of the node by ggnet2. For example in the data below, i want to use the popularity as the size of the event node and the actor_enjoyed_event as the edge size. I kinda figured out the edge size but not the node size
bip_edge_df <- data.frame(
actor = c("a", "a", "b", "b", "c", "d", "d", "e"),
event = c("e1", "e2", "e1", "e3", "e3", "e2", "e3", "e1"),
actor_enjoyed_event = rep(c(1, 0), 4),
stringsAsFactors = FALSE
)
bip_edge_df = bip_edge_df %>% group_by(event) %>% mutate(popularity = sum(actor_enjoyed_event))

Related

Adding extra track to outside of circos plot (circlize, chordDiagram)

I'm trying to recreate this figure below, where the "to" variable (i.e. target genes) is further grouped into outer (labelled) categories (i.e. receptors).
I have generated some example data, unfortunately I'm not sure what format is needed for the additional outer categories, but it's possibly not far off the link format.
library(circlize)
links <- data.frame(from = c("A", "B", "C", "B", "C"),
to = c("D", "E", "F", "D", "E"),
value = c(1, 1, 1, 1, 1))
categories <- data.frame(from = c("D", "E", "F", "D", "E"),
to = c("X", "X", "Y", "Y", "Y"),
value = c(1, 1, 1, 1, 1))
chordDiagram(links)
Any assistance greatly appreciated!

Efficient way to use geom_boxplot with specified quantiles and long data

I have a dataset with calculated quantiles for each department and country. It looks like this:
df <- structure(list(quantile = c("p5", "p25", "p50", "p75", "p95",
"p5", "p25", "p50", "p75", "p95", "p5", "p25", "p50", "p75",
"p95", "p5", "p25", "p50", "p75", "p95"), value = c(6, 12, 20,
33, 61, 6, 14, 23, 38, 63, 7, 12, 17, 26, 50, 7, 12, 18, 26,
51), country = c("A", "A", "A", "A", "A", "B", "B", "B", "B",
"B", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B"), dep = c("D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "I", "I", "I", "I",
"I", "I", "I", "I", "I", "I"), kpi = c("F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F")), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
Now, I would like to build a boxplot for each department comparing countries and using p5/p95 instead of min/max similar to this plot but without outliers (hence, Train_number would be countries):
The corresponding code to this plot is (from question ggplot2, geom_boxplot with custom quantiles and outliers):
ggplot(MyData, aes(factor(Stations), Arrival_Lateness,
fill = factor(Train_number))) +
stat_summary(fun.data = f, geom="boxplot",
position=position_dodge(1))+
stat_summary(aes(color=factor(Train_number)),fun.y = q, geom="point",
position=position_dodge(1))
I tried to derive a solution from the code above and the provided answers. Unfortunately I lack the knowledge how to provide the neccessary values from the variables quantile and value to ggplot(). Is there an argument in the stat_summary() function I missed and could use? Or just another simple solution?
Whatever data you have provided from that you can generate the following plot
library(ggplot2)
f <- function(x) {
r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
ggplot(df, aes(factor(dep), value)) +
stat_summary(fun.data = f, geom="boxplot",
position=position_dodge(1))+
facet_grid(.~country, scales="free")
I don't know whether it is correct or not.

How to assign levels to nodes in multi-level sankey diagram?

I am trying to build interactive multi-level sankey diagram using R. I can't find the solution how to assign the levels to nodes. For example, a1 node should be on the second level in chart but not in the fifth. It seems that the package assigns to the last node in the chain the rightmost position, which is not preferable in my case.
I tried different packages like echarts4r, networkD3, ggvis but it seems that these packages doesn't provide the functionality to manage levels in graph.
If you know how to solve this issue, please, share.
library('networkD3')
library('echarts4r')
library('googleVis')
sankey <- data.frame(
source = c("a", "a", "b", "c", "d", "c"),
target = c("b", "a1", "c", "d", "e", "e"),
value = ceiling(rnorm(6, 10, 1)),
stringsAsFactors = FALSE
)
# googleVis solution
plot(gvisSankey(sankey, from = 'source', to = 'target', weight = 'value'))
# echarts4r solution
sankey %>%
e_charts() %>%
e_sankey(source, target, value, focusNodeAdjacency = 'allEdges')
# networkD3 solution
nodes <- data.frame(name = c("a", "a1", "b", "c", "d", "e"))
links <- data.frame(
source = c(0, 0, 2, 3, 4, 3),
target = c(2, 1, 3, 4, 5, 5),
value = ceiling(rnorm(6, 10, 1))
)
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "source",
Target = "target", Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30, sinksRight = TRUE)
Using networkd3, change sinksRight = TRUE to sinksRight = FALSE
library('networkD3')
sankey <- data.frame(
source = c("a", "a", "b", "c", "d", "c"),
target = c("b", "a1", "c", "d", "e", "e"),
value = ceiling(rnorm(6, 10, 1)),
stringsAsFactors = FALSE
)
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "source",
Target = "target", Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30, sinksRight = FALSE)

How to display specific labels on igraph?

I have a very large igraph but when I plot, I want that just specific labels appear. Those labels are saved in different dfs (activator and receptor).
I tried with:
nodes <- data.frame(symbol = LETTERS[1:7])
edges <- data.frame(from=c("B", "C", "D", "D", "D", "E", "A", "G"),
to = c("A", "B", "A", "E", "B", "A", "F", "F"))
activator <- c('A')
receptor <- c('E', 'F', 'G')
g <- graph_from_data_frame(edges, directed=TRUE, vertices=nodes)
print(g, e=TRUE, v=TRUE)
plot(g, vertex.label1 = ifelse(V(g)$name %in% c(activator, receptor), V(g)$name, NA))
But it is not working. Any comments?

Render multiple transition plots on one page (Gmisc)

I wonder if there is a way to arrange multiple of the nice transition plots of the Gmisc package on one page (e.g. two next to each other or two-by-two)? I tried various common approaches (e.g. par(mfrow = c(2,2)) and grid.arrange()) but was not successful thus far. I would appreciate any help. Thanks!
library(Gmisc)
data.1 <- data.frame(source = c("A", "A", "A", "B", "B", "C", "C"),
target = c("A", "B", "C", "B", "C", "C", "C"))
data.2 <- data.frame(source = c("D", "D", "E", "E", "E", "E", "F"),
target = c("D", "E", "D", "E", "F", "F", "F"))
transitions.1 <- getRefClass("Transition")$new(table(data.1$source, data.1$target), label = c("Before", "After"))
transitions.2 <- getRefClass("Transition")$new(table(data.2$source, data.2$target), label = c("Before", "After"))
# wish to render transition 1 and transition 2 next to each other
transitions.1$render()
transitions.2$render()
This was actually a bug prior to the 1.9 version (uploading to CRAN when writing this, available now from GitHub). What you need to do is use the grid::viewport system:
library(grid)
grid.newpage()
pushViewport(viewport(name = "basevp", layout = grid.layout(nrow=1, ncol=2)))
pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
transitions.1$render(new_page = FALSE)
popViewport()
pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
transitions.2$render(new_page = FALSE)

Resources