I have build a network and created both igraph and network object. I can plot the object with ggplot and export to plot_ly using ggplotly.
The problem is that i donĀ“t have real control over the graph so i was wondering if there is a better way to import my network (or Ig graph) into plotly to have better control over the layout.
For example when connecting two nodes i have a weight score and would like to add a slider to filter according to the weight score (between 0 and 1).
I guess plotly could be a nice alternative that would allow dynamic filtering over the network graph.
So here is a reproducible exemple.
I create a bipartite network as follows:
library(ggnet)
library(plotly)
library(network)
n <- data.frame(event1 = c(-0.2,0.8,0.4,0),
event2 = c(0.34,-0.17,0.3,0),
event3 = c(0.2,0.1,-0.73,0),
row.names = letters[1:4])
net <- network(n,
matrix.type = "bipartite",
ignore.eval = FALSE,
names.eval = "weights")
col = c("actor" = "grey", "event" = "gold")
set.edge.attribute(net, "color", ifelse(net %e% "weights" > 0, "green", "red"))
g <- ggnet2(net, color = "mode", palette = col, label = TRUE, edge.color = "color",edge.label="weights")
Thanks,
I convert the plot to ggplot format:
gp <- ggplotly(g)
htmlwidgets::saveWidget(as_widget(gp), "ggplot.html")
I get the following graph.
How can i add a slider to the graph so i can filter out (dynamically) the edges above or nelow a certain value ???
For example to see all edges >0 ??? I know how do it from the graph itself but it is not dynamic. I want to use plotly to view and use a slider bar to remove edges.
Thanks,
Related
I'm learning to use tidygraph and ggraph to plot social network data. I like it so far, but I can't figure out how to use color gradients to indicate edge direction while also mapping the color (that the gradient is based on) to an edge attribute.
Example data
require(tidygraph)
require(ggraph)
edges_df <- tibble(from = c("A","A","B","C","C","D"),
to = c("C","B","D","B","D","A"),
edgewidth = c(1,3,3,2,4,1),
edgegradient = factor(c("red","blue","red","blue","blue","red")))
# I realize edge gradient could be any attribute, not necessarily the names of the desired colors.
nodes_df <- tibble(node_id = c("A","B","C","D"))
my_graph <- tbl_graph(directed = TRUE, nodes = nodes_df, edges = edges_df)
ggraph(my_graph %>%
activate(edges), layout = "linear", circular = TRUE) +
geom_edge_arc(aes(width = edgewidth, color = stat(..index..))) +
geom_node_label(aes(label = node_id), size = 10)
Here is the ggraph plot (since I'm new to SO, I can't embed the image).
What I want: Edges A->C, D->A, and B->D to be a red gradient, and edges A->B, C->B, and C->D to be a blue gradient. I have a feeling I'm confusing what geom_edge_arc's color aesthetic is doing versus what the scale_edge_color_* aesthetic does.
I like that ggraph uses the ggplot engine and similar syntax, so if at all possible, an answer using ggraph would be great. If not, that's okay.
Maybe this what you are looking for. There is probably a solution to achieve via a well-specified color gradient. However, after habing a look at the issue a simple solution to get two different "color gradients" is to map ..index.. on the alpha aesthetic, while mapping edgegradient on color. To get the right colors you could make use scale_edge_color_identity:
library(tidygraph)
library(ggraph)
library(tibble)
edges_df <- tibble(from = c("A","A","B","C","C","D"),
to = c("C","B","D","B","D","A"),
edgewidth = c(1,3,3,2,4,1),
edgegradient = factor(c("red","blue","red","blue","blue","red")))
# I realize edge gradient could be any attribute, not necessarily the names of the desired colors.
nodes_df <- tibble(node_id = c("A","B","C","D"))
my_graph <- tbl_graph(directed = TRUE, nodes = nodes_df, edges = edges_df)
ggraph(my_graph %>%
activate(edges), layout = "linear", circular = TRUE) +
geom_edge_arc(aes(width = edgewidth, alpha = stat(..index..), color = edgegradient)) +
geom_node_label(aes(label = node_id), size = 10) +
scale_edge_color_identity(guide = "legend")
I have what is probably the easiest question in the R Studio world. I'm and R novice, bumbling my way through a published program. The program generates dendrogram heatmaps, and I created a custom color palette for each of the dendrograms I need to export. My heatmaps look good, however, I don't know how to either (1) display the custom color-coded scale bar on my heatmap, or alternatively (2) just see and save the custom palette, as you can do with display.brewer.pal, which would allow me at least to have the palette so I can annotate later.
Here's what I have done
pal <- colorRampPalette(c("#4d4d4d", "white", "#32c200")) #GreytoGreen
curr.pal = pal(15)
which gives me (when I say View(curr.pal))
"#4D4D4D" "#666666" "#7F7F7F" "#999999" "#B2B2B2" "#CCCCCC" "#E5E5E5" "#FFFFFF" "#E1F6DA" "#C4EDB6" "#A7E491" "#89DC6D" "#6CD348" "#4FCA24" "#32C200"
The relevant part of the heatmap code is
myHeatmap <- function(x) {
map.input = t(x)
distance <- dist(map.input[, 18:24], method = "euclidean")
cluster <- hclust(distance, method = "complete")
heatmap(map.input, Rowv = as.dendrogram(cluster), Colv = NA, xlab = "Lag", col = curr.pal, scale = "none")
Any suggestions how to get the legend displayed for my 15 color palette on the heat map, or at least get a png of it? Thank you, I apologize for the easy question.
I tried to implement your code (at least, in a similar way) in "gplots" package. The heatmap.2 function has some extensions, as compared to heatmaps.
The code is attached (I used "mtcars" dataframe to play around):
library(gplots)
map.input <- scale(mtcars)
pal <- colorpanel(15, "#4d4d4d", "white", "#32c200")
distance <- dist(map.input, method = "euclidean")
cluster <- hclust(distance, method = "complete")
heatmap.2(map.input, Rowv = as.dendrogram(cluster), Colv = TRUE, xlab = "Lag", col = pal, scale = "none",
trace = "none")
I can aslo easily export the png data in RStudio in the plot tab.
I am plotting my network graph with over 117 nodes. I have made it to where you can visualize a legend for the nodes, but I have to make the text really small. Is there a way I can adjust the legend to have two columns of my nodes? I have attached my code and a picture of the graph in this question.
Network image I have
Network image I want
Thanks!!
g = LoGo(dataset, normal= FALSE)
qgraph(g, layout = "spring", diag = FALSE, labels = TRUE,
nodeNames = rownames(g),
legend.cex = 0.16,vsize = 2, GLratio = 9, vsize=4, legend=TRUE, legend.cex=.6)
Context
I am using ggraph to arrange nodes (leaves of a tree) in a circular dendrogram and then add connections between some of the nodes (using hierarchical bundling using geom_conn_bundle):
library(ggraph)
library(igraph)
# Example data
edges <- data.frame(from="root", to=paste("leaf", seq(1,100), sep=""))
vertices <- data.frame(name = unique(c(as.character(edges$from), as.character(edges$to))) )
tree <- graph_from_data_frame( edges, vertices=vertices )
# Drawing nodes
pr <- ggraph(tree, layout = "dendrogram", circular = TRUE) +
geom_edge_diagonal(alpha = 0.2)
# Example connection
pr <- pr + geom_conn_bundle(
data = get_con(from = 23, to = 42),
alpha=0.8,
width=3,
colour="skyblue",
tension = 0.9
)
print(pr)
This nicely displays a nearly transparent dendrogram and some (in this example one) connections in skyblue.
Problem / Desired output
What I'd like though, is the direction of the connection being indicated by a color gradient (i.e. starting with green, slowly changing into red) instead of showing the connection in just one color (skyblue). How can I achive such a color gradient using R and ggraph's geom_conn_bundle?
The following excerpt from Holten (2006) can serve of an example of how I'd like the connections to look:
Several of the ggraph geoms for drawing edges, including geom_conn_bundle and geom_edge_diagonal, have a calculated index stat. It's a number from 0 to 1 of how far along the edge a point is. Note that the simplified versions of these geoms (geom_*0) don't calculate it. Some mentions of it are in this blog post by the ggraph author.
In this case, map the index stat(index) to color inside your bundle's aes, then set a gradient scale with (scale_edge_color_gradient, not scale_color_gradient as I initially tried).
In the example picture, I can't tell whether the width is also scaled, but the same would work, e.g. edge_width = stat(index).
library(ggraph)
library(igraph)
ggraph(tree, layout = "dendrogram", circular = TRUE) +
geom_edge_diagonal(alpha = 0.2) +
geom_conn_bundle(aes(color = stat(index)),
data = get_con(from = 23, to = 42),
alpha=0.8,
width=3,
# colour="skyblue",
tension = 0.9
) +
scale_edge_color_gradient(low = "green", high = "red")
Created on 2019-03-09 by the reprex package (v0.2.1)
I'm having trouble adding a title and legend to the inside of my graph. Is it not possible with tkplot OR plot?
dff <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = dff, vertices = nod,directed = F)
# plot function with edge.label added
tkplot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')
It doesn't seem to want to work the way I want it to. I just want the legend to show the vertices adjusted to be bigger for a higher degree.
I was also wondering if there is a way to plot only certain vertices from the graph? For example, you choose one vertex and plot only the vertices that form a path back to it?
Based on the help for tkplot, here's an example of how to add a title to an interactive plot:
library(tcltk)
id = tkplot(pg, edge.label = nod$wt, main = "this is my graph")
canvas = tk_canvas(id)
width = as.numeric(tkcget(canvas, "-width"))
height = as.numeric(tkcget(canvas, "-height"))
tkcreate(canvas, "text", width/2, 25, text="My title",
justify="center",
font=tkfont.create(family="helvetica", size=20, weight="bold"))
For the standard base R plot, your code works as is:
plot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')