[R] geom_edge & color
Kimmo Elo
k|mmo@e|o @end|ng |rom utu@||
Thu Mar 21 10:51:22 CET 2024
Dear Sibylle,
your example is not working! E.g. no data for "aes_collapsed".
Best,
Kimmo
ke, 2024-03-20 kello 19:28 +0100, SIBYLLE STÖCKLI via R-help kirjoitti:
> Dear community
>
> I am using ggraph to plot a network analysis. See part 2 in the working
> example.
> Besides different colors for different groups of nodes:
> --> geom_node_point(aes(size = V(network)$hub_score*200, color=
> as.factor(V(network)$community)))
> I additionally want to consider different colors for different edge
> groups
> The grouping is defined in the edge_list$relationship: negative
> relationship
> = red and positive relationship = darkblue. The code is working in the
> way
> that the groups are separated by two colors. However, the code uses not
> the
> assigned colors. Does anyone have any idea how to adapt the code?
> --> geom_edge_arc(curvature=0.3, aes(width=(E(network)$weight/10),
> color=c("darkblue", "red")[as.factor(edge_list$relationship)],
> alpha=0.5)) +
>
> Kind regards
> Sibylle
>
>
>
>
> Working example
>
> library(circlize)
> library(ggplot2)
> library(igraph)
> library(tidyverse)
> library(RColorBrewer)
> library(stringi)
> library(scico)
> library(plotly)
> library(ggraph)
>
> edges_table_Test.csv
>
> Names target weight relationship
> B.B A.A 4 pos
> C.C A.A 5 pos
> D.D A.A 5 neg
> E.E A.A 5 neg
> F.F A.A 1 pos
> C.C B.B 5 pos
> E.E B.B 1 pos
> F.F B.B 2 pos
> A.A C.C 5 pos
> B.B C.C 1 pos
> D.D C.C 5 pos
> E.E C.C 5 pos
> F.F C.C 3 pos
> A.A D.D 5 neg
> B.B D.D 1 neg
> C.C D.D 5 neg
> E.E D.D 5 neg
> F.F D.D 4 neg
> A.A E.E 5 neg
> B.B E.E 1 neg
> C.C E.E 4 neg
> D.D E.E 5 neg
> F.F E.E 5 pos
> A.A F.F 5 pos
> B.B F.F 1 pos
> C.C F.F 2 pos
> D.D F.F 3 pos
> E.E F.F 4 pos
> F.F F.F 5 pos
>
> edge_list<-read.csv("edges_table_Test.csv")
>
> network <- graph_from_data_frame(aes_collapsed, directed= FALSE,
> vertices = details)
>
> temp<-cluster_optimal(network)
> temp<-cbind(membership=temp$membership, Names=temp$name) aes_collapsed <-
> aes_collapsed %>%
> merge(temp, by="Names")
>
>
> network <- network %>%
> set_edge_attr(name = "type", value = factor(aes_collapsed$Names,
> ordered =
> is.ordered(V(network)$name))) %>%
> set_edge_attr(name = "membership", value = aes_collapsed$membership)
> %>%
> set_edge_attr(name = "color",
> value = c(viridis::viridis(5))
> [match(E(.)$type, c(factor(V(.)$name)))]) %>%
> set_vertex_attr(name = "trans_v_net", value = c(transitivity(., type =
> "local"))) %>%
> set_vertex_attr(name = "hub_score", value = c(hub_score(.)$vector)) %>%
> set_vertex_attr(name = "color",
> value = c(viridis::viridis((5)))
> [match(V(.)$name, c(factor(V(.)$name)))]) %>%
> set_vertex_attr(name= "community", value=cluster_optimal(.)$membership)
> clrs<-scico(3, palette = "batlow")
>
> ### part 1: network plot
> par(bg="black")
> network %>% plot(
> vertex.color=clrs[V(.)$community],
> vertex.size=V(.)$hub_score*5,
> vertex.frame.color=V(.)$color,
> vertex.label.color="white",
> vertex.label.cex=0.5,
> vertex.label.family="Helvetica",
> vertex.label.font=1,
> edge.curved=0.5,
> edge.width= network,
> layout=layout_with_mds(.))
>
> ### part 2: ggraph
> tiff("figures/AES_network_bymembership.tiff", width=1000, height=700,
> res=120) network %>%
> ggraph(., layout = "auto")+
> geom_edge_arc(curvature=0.3, aes(width=(E(network)$weight/10),
> color=c("darkblue", "red")[as.factor(edge_list$relationship)],
> alpha=0.5)) +
>
> geom_node_point(aes(size = V(network)$hub_score*200, color=
> as.factor(V(network)$community))) +
> geom_node_text(aes(label = V(network)$name), size=5, color="white",
> repel=T)+
> scale_color_scico_d(palette = "batlow")+
> scale_edge_width(range = c(0.2,4))+
> scale_size(range = c(0.5,20)) +
> #scale_edge_color_manual(values = c(scico(21, palette="batlow")))+
> theme(plot.background = element_rect(fill = "black"),
> legend.position = "right",
> panel.background = element_rect(fill = "black"))
> dev.off()
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list