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

How to make the function "geom_node_rect" work in a loop #29

Open
wangshiming opened this issue May 18, 2024 · 3 comments
Open

How to make the function "geom_node_rect" work in a loop #29

wangshiming opened this issue May 18, 2024 · 3 comments

Comments

@wangshiming
Copy link

wangshiming commented May 18, 2024

g <- pathway("ko00520")
V(g)$color_1 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set1"))(length(V(g)))
V(g)$color_2 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set2"))(length(V(g)))
V(g)$color_3 <- colorRampPalette(RColorBrewer::brewer.pal(5,"PuOr"))(length(V(g)))
V(g)$color_4 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Paired"))(length(V(g)))

V(g)$space <- V(g)$width/4

tem_plot = ggraph(g, x=x, y=y)
for (i in c(1:4)){
tem_plot = tem_plot + geom_node_rect(
aes(xmin= xmin + (i-1) * space,
xmax= xmin + i * space,
fill=I(get(paste0('color_', i))),
filter=type=="ortholog"))
}

tem_plot

@wangshiming
Copy link
Author

I also tried the technique by passing '.data' variable mention by:
https://stackoverflow.com/questions/15987367/how-to-add-layers-in-ggplot-using-a-for-loop

@noriakis
Copy link
Owner

Hi, thank you for your inquiry and I apologize for the very late response. You can use !! inside aes for evaluating the number like:

g <- pathway("ko00520")
V(g)$color_1 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set1"))(length(V(g)))
V(g)$color_2 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Set2"))(length(V(g)))
V(g)$color_3 <- colorRampPalette(RColorBrewer::brewer.pal(5,"PuOr"))(length(V(g)))
V(g)$color_4 <- colorRampPalette(RColorBrewer::brewer.pal(5,"Paired"))(length(V(g)))

V(g)$space <- V(g)$width/4

tem_plot = ggraph(g, x=x, y=y)
for (i in seq_len(4)){
  tem_plot = tem_plot + 
    geom_node_rect(
      aes(xmin= .data$xmin + .data$space*!!(i-1),
          xmax= .data$xmin + .data$space*!!(i),
          fill=I(get(paste0('color_', !!i))),
          filter=type=="ortholog"))
}
tem_plot

Hope this helps and I'll try to implement functions like geom_node_rect_multi for visualization for this purpose.

@noriakis
Copy link
Owner

geom_node_rect_multi implemented from version 1.3.3.

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

No branches or pull requests

2 participants