[R] ggraph and changing colors in geom_node_arc_bar

Monica Palaseanu-Lovejoy mon|c@p@|@|ovejoy @end|ng |rom gm@||@com
Thu Apr 23 19:34:07 CEST 2020


Hi,

I am not sure if anybody was able to tackle this subject, but finally after
hours of trolling the internet i came to this quite ugly solution - which
is partial but at least it changes the colors.
So, without further ado here you go:

library(tidygraph)
library(tidyverse)
library(ggraph)
library(igraph)
# i hope i remembered all the pertinent libraries i needed.

gr <- graph_from_data_frame(flare$edges, vertices = flare$vertices)

palette1 <- colorRampPalette(c("darkviolet","orange", "forestgreen",
"blue","black"))

hex <- palette1(252)

gr<- as_tbl_graph(gr)

gr <- gr %>%
    activate(nodes) %>%
    mutate(hex = ifelse(name, as.character(myHSB$hex)[shortName], NA)) %>%
    activate(edges) %>%
    mutate(hex = .N()$hex[to] == 'hex')

gr1 <- gr
gr1 <- gr1 %>%
    activate(nodes) %>%
mutate(hex=palette1(252)) %>%
activate(edges) %>%
mutate(hex=palette1(251))

laygr<- create_layout(gr1, layout = 'partition', circular=TRUE)

colsgr <- laygr$hex

ggraph(gr1, layout = 'partition', circular = TRUE) +
    geom_node_arc_bar(aes(fill=hex))+
scale_fill_manual(values=colsgr)+
scale_color_manual(values=colsgr)+
theme_graph(background="grey20", border=FALSE)+
theme(legend.position="none")

This code does actually changes the node colors, although it is still not
very clear in which order .... i tried different orders / sort solutions,
none very exciting. But i did change the colors. If you have other ideas
how to do it, i will very much welcome it.

Thanks,
Monica



On Wed, Apr 22, 2020 at 8:31 AM Monica Palaseanu-Lovejoy <
monicapalalovejoy using gmail.com> wrote:

> Hi,
>
> I am playing with the ggraph that is amazing. But i don't quite understand
> some of the options it has. For example:
>
> gr <- graph_from_data_frame(flare$edges, vertices = flare$vertices)
>
> l <- ggraph(gr, layout = 'partition', circular = TRUE)
> l + geom_node_arc_bar(aes(fill = depth)) +
>     coord_fixed()
>
> This will give a nice graph in shades of blue.
> But if i want to change the fill aesthetic with a grey scale for example:
>
> l + geom_node_arc_bar(aes(fill = grey(seq(0,1,length=252)))) +
>     coord_fixed() +
> theme(legend.position = "none")
>
> This will give a graphic with set colors that definitely are not on a grey
> scale. So i am missing a piece in my code.
> I tried to add scale_edge_fill_manual(values= grey(seq(0,1,length=252)))
> but to no avail, and besides this has to do with edges and not nodes. So
> this is not the solution.
>
> What i am doing wrong, or what i am missing from my command?
>
> Also i am interested how the graph and ggraph plots, in the sense in what
> order is the data plotted? I am interested in that because i may want to
> set up either colors or widths of edges separately from my graph data for
> visualization.
>
> Thanks,
> Monica
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list