[R] It This data viz possible in R?

baptiste auguie baptiste.auguie at googlemail.com
Thu Apr 8 09:15:16 CEST 2010


An improved version below (now the connections are drawn in the correct order),

library(grid)

arcTextGrob <- function(x=unit(0.5, "npc"), y=unit(0.5, "npc"),
                        labels=library()$results[,1],
                        links=sample(seq_along(labels), 20, rep=T),
                        default.units="npc",
                        gp=gpar(), ...)
  {

    ##     circle
    full.height <- sum(stringHeight(labels))
    radius <- 1.2 /(2*pi) * full.height
    g1 <- circleGrob(0.5, 0.5, r=radius, default.units="npc", gp=gpar(col=NA))

    ##     text labels
    n <- length(labels)
    ang <- seq(0, n-1) * 2 * pi/n

    radius.npc <- convertUnit(radius, "npc", val=T)
    coords <- data.frame(x=0.5+radius.npc*cos(ang), y=0.5+radius.npc*sin(ang))
    g2 <- textGrob(labels, x=coords$x , y=coords$y , rot=ang*180/pi,
default.units="npc", hjust=0)

    ##     connecting pairs

    xm <- matrix(coords$x[links], ncol=2, byrow=T)
    ym <- matrix(coords$y[links], ncol=2, byrow=T)

    ## find out which pairs are not in trigo order
    ## and swap them
    swap <- as.logical(sign((xm[, 1]-0.5)*(ym[, 2]-0.5) - (xm[,
2]-0.5)*(ym[, 1]-0.5)) + 1)
    xm[swap, ] <- rev(xm[swap])
    ym[swap, ] <- rev(ym[swap])

    g3 <- do.call(gList, mapply(curveGrob, x1=xm[, 1], y1=ym[, 1],
x2=xm[, 2], y2=ym[, 2],
                                ncp=8, curvature=0.4, square=F, SIMPLIFY=FALSE))

    gTree(children=gList(g1, g2, g3),
          outer.radius=convertUnit(radius, "npc") +
          convertUnit(max(stringWidth(labels)), "npc"))
  }


grid.arcText <- function(...)
  grid.draw(arcTextGrob(...))

set.seed(1234)
grid.newpage()
grid.arcText()


On 7 April 2010 23:13, baptiste auguie <baptiste.auguie at googlemail.com> wrote:
> The following grob might be a starting point. I couldn't find a clean
> way of orienting the linking arcs though...
>
> Best,
>
> baptiste
>
> library(grid)
>
> paragraph <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Praesent adipiscing lobortis placerat. Nunc vel arcu mauris. Aliquam
> erat volutpat. Integer et pharetra orci. Sed rutrum facilisis dolor et
> condimentum. Class aptent taciti sociosqu ad litora torquent per
> conubia nostra, per inceptos himenaeos. Nunc leo nibh, pellentesque et
> convallis quis, mattis ut mi. Nunc dignissim auctor elit pulvinar
> malesuada. Cras dapibus hendrerit ligula quis suscipit. Proin porta
> tempor feugiat. Ut quis nisi lacus, et egestas tortor. Fusce porttitor
> tincidunt fringilla. Vivamus rhoncus ultrices elit, at fermentum nisl
> scelerisque et. Duis placerat est at justo vestibulum sodales.
> Curabitur quis eros tellus. "
>
> words <- strsplit(paragraph, " ")[[1]]
> labels <- apply(matrix(words, ncol=3, byrow=T), 1, paste, collapse=" ")
>
> arcTextGrob <- function(x=unit(0.5, "npc"), y=unit(0.5, "npc"),
>                        labels=letters[1:10],
>                        links=sample(seq_along(labels), 10),
>                        min.radius=unit(2, "cm"),
>                        default.units="npc",
>                        gp=gpar(), ...)
>  {
>
>    ##     circle of perimeter = 1.5 * the text height
>    full.height <- sum(stringHeight(labels))
>    radius <- 1.5 /(2*pi) * full.height
>
>    g1 <- circleGrob(0.5, 0.5, r=radius, default.units="npc")
>
>    ##     text labels
>    n <- length(labels)
>    ang <- seq(0, n-1) * 2 * pi/n
>
>    radius.mm <- convertUnit(radius, "npc", val=T)
>    coords <- data.frame(x=0.5+radius.mm*cos(ang), y=0.5+radius.mm*sin(ang))
>    g2 <- textGrob(labels, x=coords$x , y=coords$y , rot=ang*180/pi,
> default.units="npc", hjust=0)
>
>    ## links,
>    ## NOTE: they are not well ordered...
>
>    xm <- matrix(coords$x[links], ncol=2, byrow=T)
>    ym <- matrix(coords$y[links], ncol=2, byrow=T)
>
>    g3 <- do.call(gList, mapply(curveGrob, x1=xm[, 1], y1=ym[, 1],
> x2=xm[, 2], y2=ym[, 2],
>                                ncp=8, curvature=0.3, square=F, SIMPLIFY=FALSE))
>
>    gTree(children=gList(g1, g2, g3))
>  }
>
>
> grid.arcText <- function(...)
>  grid.draw(arcTextGrob(...))
>
> dev.new()
> grid.arcText(labels=labels)
>
>
>
>
> On 7 April 2010 16:44, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>> There is draw.arc in the plotrix package.
>>
>> On Wed, Apr 7, 2010 at 10:20 AM, baptiste auguie
>> <baptiste.auguie at googlemail.com> wrote:
>>> Hi,
>>>
>>> Barry suggested a way to place the text labels; I would like to point
>>> out the grid.curve() function that might help in connecting the labels
>>> with nice-looking curves. I don't know of a base graphics equivalent
>>> (xspline() might come close) so it might be best to opt for Grid.
>>>
>>> HTH,
>>>
>>> baptiste
>>>
>>>
>>> On 7 April 2010 15:46, Barry Rowlingson <b.rowlingson at lancaster.ac.uk> wrote:
>>>> On Wed, Apr 7, 2010 at 2:28 PM, Brock Tibert <btibert3 at yahoo.com> wrote:
>>>>> Hi All,
>>>>>
>>>>> I am new to R, but it has been a lot of fun learning as I go and have been blow away by what it can do.  Came across this example and wanted to see if ggplot2 or some other visualization package could make this sort of graphic.
>>>>>
>>>>> http://www.visualcomplexity.com/vc/project.cfm?id=717&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+visualcomplexity+(visualcomplexity.com)&utm_content=Google+Reader
>>>>>
>>>>> Thanks in advance!
>>>>
>>>>  Not quite out-of-the box, but you can draw text with the text()
>>>> function setting the angle with the 'srt' parameter, and you can draw
>>>> lines using 'lines'. You can compute angles using 'pi'. You'll need a
>>>> bit of trig to work out the angle that the lines start and end at.
>>>> That's about all you need to know.
>>>>
>>>>  Some of the subtleties of the typesetting of that specific piece may
>>>> be tricky, but it's easy to write a function that takes a vector of
>>>> strings and an adjacency matrix and plots something like it.
>>>>
>>>>  Give R-help another hour and I reckon something will turn up. Not
>>>> from me, I'm watching the IPL cricket.
>>>>
>>>> Barry
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>



-- 
____________________

Baptiste Auguié

Departamento de Química Física,
Universidade de Vigo,
Campus Universitario, 36310, Vigo, Spain

tel: +34 9868 18617
http://webs.uvigo.es/coloides



More information about the R-help mailing list