Creating Custom Shapes with ggplot

727 views
Skip to first unread message

Brandon Bertelsen

unread,
Feb 6, 2011, 9:02:53 PM2/6/11
to ggp...@googlegroups.com

Hi ggploters,

Full Disclosure: Cross posted on SO (http://stackoverflow.com/questions/4917570/r-creating-custom-shapes-with-ggplot)

I'm a bit lost on this one, I've tried messing around with geom_polygon but successive attempts seem worse than the previous.

The image that I'm trying to recreate is this, the colours are unimportant, but the positions are:

enter
          image description here

In addition to creating this, I also need to be able to label each element with text.

At this point, I'm not expecting a solution (although that would be ideal) but pointers or similar examples would be immensely helpful.

One option that I played with was hacking scale_shape and using 1,1 as coords. But was stuck with being able to add labels.

The reason I'm doing this with ggplot, is because I'm generating scorecards on a company by company basis. This is only one plot in a 4 x 10 grid of other plots (using pushViewport)

Note: The top tier of the pyramid could also be a rectangle of similar size.

TIA,

-- 
Brandon Bertelsen
Office: 647-722-0481 
Cell: 647-287-1009

Brandon Bertelsen

unread,
Feb 6, 2011, 11:53:30 PM2/6/11
to ggp...@googlegroups.com
Chase, from SO, was kind enough to provide this solution (http://stackoverflow.com/questions/4917570/r-creating-custom-shapes-with-ggplot):

It seems like you could use a combination of geom_path() and geom_segment() since you either know or can reasonably guesstimate the coordinate locations for each major point on your graph/chart/thingamajigger up there. Maybe something like this would work? The data.frame that was constructed contains the outline of the shape above (I opted for the rectangle at the top...I'm sure you could find an easy way to generate the points to approximate a circle if you really wanted. Then use geom_segment() to divvy up that large shape as you need.

df <- data.frame(
    x = c(-8,-4,4,8,-8, -8, -8, 8, 8, -8)
    , y = c(0,18,18,0,0, 18, 22, 22, 18, 18)
    , group = c(rep(1,5), rep(2,5)))

qplot(x,y, data = df, geom = "path", group = group)+
    geom_segment(aes(x = 0, y = 0, xend = 0, yend = 12 )) +
    geom_segment(aes(x = -6.75, y = 6, xend = 6.75, yend = 6)) +
    geom_segment(aes(x = -5.25, y = 12, xend = 5.25, yend = 12)) +
    geom_segment(aes(x = -2, y = 12, xend = -2, yend = 18)) + 
    geom_segment(aes(x = 2, y = 12, xend = 2, yend = 18)) + 
    geom_text(aes(x = -5, y = 2.5), label = "hi world")

Brandon Bertelsen
Office: 647-722-0481 
Cell: 647-287-1009

baptiste auguie

unread,
Feb 7, 2011, 4:11:12 AM2/7/11
to Brandon Bertelsen, ggp...@googlegroups.com
Hi,

Why don't you use grid graphics directly?

library(gridExtra)

grid.newpage()
grid.ellipse(0.5, 0.8, ar=2.5, size=0.3, angle=pi,
             default.units="npc", size.units="npc", gp=gpar(fill=rgb(0, 113, 193, max=256)))

x <- c(0.5, 0.5, 0.45, 0.4, 0.5)
y <- c(0.65, 0.75, 0.75, 0.65, 0.65)

grid.polygon(x, y, gp=gpar(fill=rgb(163, 163, 223, max=256)))
grid.polygon(1-x, y, gp=gpar(fill=rgb(163, 163, 223, max=256)))

x2 <- c(0.4, 0.35, 0.5, 0.5, 0.4)
y2 <- c(0.65, 0.55, 0.55, 0.65, 0.65)

grid.polygon(x2, y2, gp=gpar(fill=rgb(209, 210, 240, max=256)))
grid.polygon(1-x2, y2, gp=gpar(fill=rgb(209, 210, 240, max=256)))

x3 <- c(0.35, 0.3, 0.5, 0.5, 0.35)
y3 <- c(0.55, 0.45, 0.45, 0.55, 0.55)

grid.polygon(x3, y3, gp=gpar(fill=rgb(217, 217, 217, max=256)))
grid.polygon(1-x3, y3, gp=gpar(fill=rgb(217, 217, 217, max=256)))

x4 <- c(0.5, 0.47)
y4 <- c(0.8, 0.7)
labels <- c("ellipse", "top")

grid.text(labels, x4,y4)


HTH,

baptiste

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

m4tZk.jpg

Andrie de Vries

unread,
Feb 8, 2011, 8:50:46 AM2/8/11
to ggplot2
I have answered this question on stackoverflow:

http://stackoverflow.com/questions/4917570/r-creating-custom-shapes-with-ggplot/4919601#4919601

Andrie

On Feb 7, 2:02 am, Brandon Bertelsen <bran...@bertelsen.ca> wrote:
> Hi ggploters,
>
> Full Disclosure: Cross posted on SO
> (http://stackoverflow.com/questions/4917570/r-creating-custom-shapes-w...)
>
> I'm a bit lost on this one, I've tried messing around with geom_polygon
> but successive attempts seem worse than the previous.
>
> The image that I'm trying to recreate is this, the colours are
> unimportant, but the positions are:
>
Reply all
Reply to author
Forward
0 new messages