thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.06k stars 112 forks source link

Segmented node colours #218

Open kdome opened 4 years ago

kdome commented 4 years ago

Is there some way to segment nodes into multiple sections of different colours? I'm imagining something where a node belongs to, say, two categories and can be split into a blue half and a green half. If not, could this be considered for a future release?

thomasp85 commented 3 years ago

This is not really possible at the moment

schochastics commented 6 months ago

here is a "workaround" that I came up with

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(scatterpie)
#> Loading required package: ggplot2
library(ggraph)
set.seed(1439)
# create a random graph with three random attributes
g <- sample_pa(20, 1)
V(g)$A <- abs(rnorm(20, sd = 1))
V(g)$B <- abs(rnorm(20, sd = 2))
V(g)$C <- abs(rnorm(20, sd = 3))

# precompute the layout
xy <- graphlayouts::layout_with_stress(g)
V(g)$x <- xy[, 1]
V(g)$y <- xy[, 2]

ggraph(g, "manual",x=xy[,1],y=xy[,2]) +
    geom_edge_link0() +
    geom_scatterpie(
        aes(x=x,y=y),
        cols = c("A", "B", "C"),
        data = as_data_frame(g, "vertices"),
        colour = NA,
        pie_scale = 2
    ) +
    coord_fixed() +
    theme_graph() +
    theme(legend.position = "bottom")

Created on 2024-01-16 with reprex v2.1.0