[R] ggraph and changing colors in geom_node_arc_bar

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Apr 23 19:45:32 CEST 2020

I don't know anything about ggraph, but I am pretty sure that ggplot does not work internally with data of character type... it converts such columns to factor. If you want to control the order of levels then you need to convert to factors before you give the data to ggplot. If you use the factor (not as.factor) function to do this, then you have full control of the sequence of levels, and you can assume this same sequence in your scale_color_ or scale_fill_ specifications.

On April 23, 2020 10:34:07 AM PDT, Monica Palaseanu-Lovejoy <monicapalalovejoy using gmail.com> wrote:
>I am not sure if anybody was able to tackle this subject, but finally
>hours of trolling the internet i came to this quite ugly solution -
>is partial but at least it changes the colors.
>So, without further ado here you go:
># 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",
>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) %>%
>laygr<- create_layout(gr1, layout = 'partition', circular=TRUE)
>colsgr <- laygr$hex
>ggraph(gr1, layout = 'partition', circular = TRUE) +
>    geom_node_arc_bar(aes(fill=hex))+
>theme_graph(background="grey20", border=FALSE)+
>This code does actually changes the node colors, although it is still
>very clear in which order .... i tried different orders / sort
>none very exciting. But i did change the colors. If you have other
>how to do it, i will very much welcome it.
>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
>> 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
>> 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
>> scale. So i am missing a piece in my code.
>> I tried to add scale_edge_fill_manual(values=
>> but to no avail, and besides this has to do with edges and not nodes.
>> 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
>> order is the data plotted? I am interested in that because i may want
>> set up either colors or widths of edges separately from my graph data
>> visualization.
>> Thanks,
>> Monica
>	[[alternative HTML version deleted]]
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

Sent from my phone. Please excuse my brevity.

More information about the R-help mailing list