问题
Drawing a graph with R and igraph, I'm using color to label vertex type. See code below. Is it possible to fill vertices with a pattern rather than a color so that the node types are distinguishable when viewed in color AND black and white? I need 4 unique colors/patterns. The only palette from colorbrewer that fits is this one: http://colorbrewer2.org/#type=sequential&scheme=OrRd&n=4 However, for my sparse graph, its still difficult to differentiate the colors. Also, I'm color blind, which doesn't help. I wan to know if I can fill the vertices with stripes,hasing, polka-dots, anything other than color.
library(igraph)
library(RColorBrewer)
cols = brewer.pal(4, "Dark2") #looks good in color, not in bw
g <- sample_pa(20)
l <- layout_with_fr(g,niter=300)
plot(g,layout=l,edge.arrow.size=0,vertex.size=8,
vertex.frame.color="white",vertex.label=NA,vertex.color=cols)
回答1:
Your graph has 20 vertices.
> length(V(g))
[1] 20
The shape argument (see ?vertex.shape.pie can apply a varying pie-"pattern" (or shape) in the form of a numerical specification of the relative segment sizes and varying colors:
values <- lapply(1:20, function(x) sample(1:5)) #segment sizes
colrs <- lapply(1:20, function(x) sample( c(heat.colors(4), "#000000FF"), 5)) # order of colors
plot(g, vertex.shape="pie", vertex.pie=values,
vertex.pie.color=colrs,
vertex.size=seq(10,30,length=10), vertex.label=NA)
You could make these "white" with "#ffffffff" or make them varying shades of grey. It is possible to pattern base graphics (which is the paradigm used by plot.igraph with arguments like:
rect(.8,.8,1,1, col=grey(.2), density=20, angle=45)
However, I don't see the use of the 'density' parameter in the plot.igraph code. If you want to do your own inspection, you will need to look inside the igraph-namespace:
ls( envir= as.environment("package:igraph") )
来源:https://stackoverflow.com/questions/43402827/r-igraph-is-it-possible-to-fill-vertex-with-pattern