Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question regarding circular layouts #256

Closed
richardbeare opened this issue Aug 27, 2020 · 4 comments
Closed

Question regarding circular layouts #256

richardbeare opened this issue Aug 27, 2020 · 4 comments
Labels

Comments

@richardbeare
Copy link

Hi,
This is a question about whether there is a better way of doing something in ggraph than the way I came up with a few years go by hacking ggforce.

The problem is to draw brain connectivity diagrams, with brain regions laid out in a circle according to an order I specify, and the angle each region subtends setable via a mapping - basically like the "amount" aesthetic in the ggforce pie chart. The ggforce solution was a hacked combination of the arc_bar and pie chart.

I was also able to place labels radiating from the centre with

geom_text(stat="text_pie", aes(x0=0, y0=0, r0=1.1, r=1.25, amount=area, label=regionfactors,
                                     sourcenode=regionfactors, hjust=0, vjust=0), size=3)

I can get kind of close with point nodes sized according to the thing I'd like to map to angle subtended, and the edges are fine:

(Not reproducible, sorry)

this.layout.p <- ggraph::create_layout(aa, layout = "linear", circular=TRUE, sort.by = circleorder)

ggraph(graph=aa, layout = (select(this.layout.p, x, y))) + 
    geom_node_point(aes(size=area, colour=lobe)) +
    geom_edge_arc(aes(circular=TRUE)

almost

I'd like to be able to have node label pointing outwards - (is there an appropriate angle being computed somewhere?), and also links going a bit closer to the centre, like the bezier option in ggforce.

It looks like the partition layout may be close, but requires a root node. Am I missing a more appropriate layout?

Thanks

@thomasp85
Copy link
Owner

Quite belated but can you share a plot or sketch that looks how you want it?

@richardbeare
Copy link
Author

I found some of my old ggforce hacks, that should give you an idea - I hope this was what I was asking about.

image

@thomasp85
Copy link
Owner

There is no way to ensure that all edges goes to the center like in your example. Personally I also believe the behaviour you showed in the first post is much better as it avoids a lot of overplotting and is easier to read (I know biology has a thing for those arcs in the last plot but it is misguided)

as for the angle of the node label that is easier to solve since ggraph provides a helper for this:

gr <- create_notable('Meredith') %>%
  mutate(class = sample(c('Class 1', 'Class 2', 'Class 3'), n(), replace = TRUE))

ggraph(gr, 'linear', circular = TRUE) +
  geom_edge_arc(aes(alpha = after_stat(index)), strength = 0.2) + 
  geom_node_text(aes(label = class, angle = node_angle(x, y), hjust = x < 0)) + 
  coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))

As for the "arc-like" nodes this is not currently possible but I might be tempted to add this functionality to the linear layout

@thomasp85
Copy link
Owner

Ok, With the latest changes I've added a weight argument to the linear layout to get uneven spacing of the nodes, plus the layout now returns enough information so that it can be used with geom_node_arc_bar()

ggraph(gr, 'linear', circular = TRUE, weight = sample(10, gorder(gr), T)) +
  geom_edge_arc() + 
  geom_node_arc_bar(aes(r = 1.1)) + 
  coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))

I think this is as close I can get you :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants