Skip to content

ggraph and changing colors in geom_node_arc_bar

4 messages · Monica Palaseanu-Lovejoy, Jeff Newmiller, Rui Barradas

#
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
1 day later
#
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 at gmail.com> wrote:

            

  
  
#
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 at gmail.com> wrote:

  
    
#
Hello,

Try a scale_fill_gradient*, there are several.


l + geom_node_arc_bar(aes(fill = depth)) +
   scale_fill_gradient(low = "gray80", high = "gray20") +
   coord_fixed() +
   theme(legend.position = "none")


This will map increasing values of the 'fill' aesthetics to colors 
between those limits, 'gray80' (lighter) and 'gray20' (darker).


Hope this helps,

Rui Barradas

?s 18:34 de 23/04/20, Monica Palaseanu-Lovejoy escreveu: