igraph-help
[Top][All Lists]

## Re: [igraph] [Ask] Pie chart

 From: Gábor Csárdi Subject: Re: [igraph] [Ask] Pie chart Date: Sun, 17 Oct 2010 22:53:42 +0200

```You can do something like this. Most is this coming from the pie()
code, and the "circle" vertex shape in igraph:

mypie <- function(x, y, values, radius, edges=200, col=NULL, angle=45,
density=NULL, border=NULL, lty=NULL, init.angle=90, ...) {
values <- c(0, cumsum(values)/sum(values))
dx <- diff(values)
nx <- length(dx)
twopi <- 2 * pi
if (is.null(col))
col <- if (is.null(density))
c("white", "lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk")
else par("fg")
col <- rep(col, length.out = nx)
border <- rep(border, length.out = nx)
lty <- rep(lty, length.out = nx)
angle <- rep(angle, length.out = nx)
density <- rep(density, length.out = nx)
t2xy <- function(t) {
t2p <- twopi * t + init.angle * pi/180
}
for (i in 1:nx) {
n <- max(2, floor(edges * dx[i]))
P <- t2xy(seq.int(values[i], values[i + 1], length.out = n))
polygon(x+c(P\$x, 0), y+c(P\$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i], ...)
}
}

.igraph.shape.pie <- function(coords, el=NULL, v=NULL, mode=c("clip", "plot"),
params, end=c("both", "from", "to")) {

mode=match.arg(mode)
end =match.arg(end)

#####################################################################
## clipping mode, this is the same as for circle

if (mode=="clip") {
if (length(coords)==0) { return (coords) }

vertex.size <- 1/200 * params("vertex", "size")

if (end=="from") {
phi <- atan2(coords[,4] - coords[,2], coords[,3] - coords[,1])
vsize.from <- if (length(vertex.size)==1) {
vertex.size
} else {
vertex.size[ el[,1] ]
}
res <- cbind(coords[,1] + vsize.from*cos(phi),
coords[,2] + vsize.from*sin(phi) )
} else if (end=="to") {
phi <- atan2(coords[,4] - coords[,2], coords[,3] - coords[,1])
r <- sqrt( (coords[,3] - coords[,1])^2 + (coords[,4] - coords[,2])^2 )
vsize.to <- if (length(vertex.size)==1) {
vertex.size
} else {
vertex.size[ el[,2] ]
}
res <- cbind(coords[,1] + (r-vsize.to)*cos(phi),
coords[,2] + (r-vsize.to)*sin(phi) )
} else if (end=="both") {
phi <- atan2(coords[,4] - coords[,2], coords[,3] - coords[,1])
r <- sqrt( (coords[,3] - coords[,1])^2 + (coords[,4] - coords[,2])^2 )
vsize.from <- if (length(vertex.size)==1) {
vertex.size
} else {
vertex.size[ el[,1] ]
}
vsize.to <- if (length(vertex.size)==1) {
vertex.size
} else {
vertex.size[ el[,2] ]
}
res <- cbind(coords[,1] + vsize.from*cos(phi),
coords[,2] + vsize.from*sin(phi),
coords[,1] + (r-vsize.to)*cos(phi),
coords[,2] + (r-vsize.to)*sin(phi) )
}

res

#####################################################################
## plotting mode

} else if (mode=="plot") {
getparam <- function(pname) {
p <- params("vertex", pname)
if (length(p) != 1 && !is.null(v)) {
p <- p[v]
}
p
}
vertex.color       <- getparam("color")
vertex.frame.color <- getparam("frame.color")
vertex.size        <- rep(1/200 * getparam("size"), length=nrow(coords))
vertex.pie         <- getparam("pie")
vertex.pie.color   <- getparam("pie.color")
vertex.pie.angle   <- getparam("pie.angle")
vertex.pie.density <- getparam("pie.density")
vertex.pie.lty     <- getparam("pie.lty")

for (i in seq_len(nrow(coords))) {
pie <- if(length(vertex.pie)==1) {
vertex.pie
} else {
vertex.pie[[i]]
}
col <- if (length(vertex.pie.color)==1) {
vertex.pie.color[[1]]
} else {
vertex.pie.color[[i]]
}
mypie(x=coords[i,1], y=coords[i,2], pie,
angle=na.omit(vertex.pie.angle[c(i,1)])[1],
density=na.omit(vertex.pie.density[c(i,1)])[1],
border=na.omit(vertex.frame.color[c(i,1)])[1],
lty=na.omit(vertex.pie.lty[c(i,1)])[1])
}
}
}

###########

library(igraph)
unlockBinding(".igraph.shapes", asNamespace("igraph"))
unlockBinding("i.default.values", asNamespace("igraph"))

shapes <- igraph:::.igraph.shapes
defaults <- igraph:::i.default.values

shapes[["pie"]] <- .igraph.shape.pie
defaults\$vertex[["pie"]] <- 1
defaults\$vertex[["pie.color"]] <- list(c("white", "lightblue", "mistyrose",
"lightcyan", "lavender", "cornsilk"))
defaults\$vertex[["pie.angle"]] <- 45
defaults\$vertex[["pie.density"]] <- -1
defaults\$vertex[["pie.lty"]] <- 1

assign(".igraph.shapes", shapes, asNamespace("igraph"))
assign("i.default.values", defaults, asNamespace("igraph"))

############

An then you can use it as

g <- graph.ring(10)
pie <- lapply(1:10, function(x) sample(1:10,3))
plot(g, vertex.shape="pie", vertex.pie=pie, vertex.frame.color="white",
vertex.label=NA, vertex.pie.color=list(heat.colors(5)),
vertex.size=seq(10,30,length=10))

Gabor

On Fri, Oct 15, 2010 at 10:08 PM, Choi <address@hidden> wrote:
> Right. I need to replace vertices with pies in the graph plotting.
> Since my network data (with known communities) will be divided into lots of
> partitions with different algorithms, I need an automatic way to plot these
> partitions.
> How can I define pie-chart as the vertex in plot(graph)?
>
> On Sat, Oct 16, 2010 at 3:44 AM, Gábor Csárdi <address@hidden> wrote:
>>
>> You can draw pie charts in R of course, see e.g.
>> http://www.statmethods.net/graphs/pie.html
>>
>> But what does this have to do with communities? The pie charts in the
>> PDF are based on external information as I understand. You can do such
>> a plot, either with R igraph and defining new vertex shapes, or just
>> using the pie and segments R functions directly, without using the
>> igraph plotting at all.
>>
>> Gabor
>>
>> On Fri, Oct 15, 2010 at 9:17 PM, Choi <address@hidden> wrote:
>> > Is there any (easy) way to plot those pie charts for community
>> > detection?
>> >
>> > Example of pie chart, see
>> > http://people.maths.ox.ac.uk/~porterm/papers/congposter.pdf
>> >
>> > _______________________________________________
>> > igraph-help mailing list
>> > http://lists.nongnu.org/mailman/listinfo/igraph-help
>> >
>> >
>>
>>
>>
>> --
>> Gabor Csardi <address@hidden>     UNIL DGM
>>
>> _______________________________________________
>> igraph-help mailing list
>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>
> _______________________________________________
> igraph-help mailing list
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>

--