-4

I would like to add curly brackets next to the axis in a plot. The curly brackets should look something like this:

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
user3676232
  • 101
  • 2
  • 9
  • Did you try `mtext` ? – Ashwin May 26 '14 at 11:57
  • 1
    Base graphics (one could assume with the `plot` tag, buy it's not clear)? `ggplot2` graphics? What have you tried so far? Sample data/plots? – hrbrmstr May 26 '14 at 12:12
  • thanks for the inputs so far. its just a normal plot. Here you can see an example: http://www.r-forum.de/resources/image/40 – user3676232 May 26 '14 at 12:20
  • Welcome to StackOverflow! Please read the info about [how to ask a question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also share any code that you’ve tried so far. This will make it much easier for others to help you. – Jaap May 26 '14 at 12:24
  • 1
    [This](http://stackoverflow.com/questions/7001799/ggplot2-curly-braces-on-an-axis) post and [this](http://stackoverflow.com/questions/6178763/how-to-add-braces-to-a-graph) one show some of the options. Otherwise, `?arrows` are an alternative. – koekenbakker May 26 '14 at 12:46
  • this looks good. but i would like to place the bracket below the axis. these functions only works in the plot area. – user3676232 May 26 '14 at 13:10

1 Answers1

11

All credits to this answer, I only did some messy adjustments. Adding xpd=NA allows you to draw outside of the plot:

# function to draw curly braces
# x, y position where to put the braces
# range is the length of the brace
# position: 1 vertical, 2 horizontal
# direction: 1 left/down, 2 right/up
# depth controls width of the shape

CurlyBraces <- function(x0, x1, y0, y1, pos = 1, direction = 1, depth = 1) {

    a=c(1,2,3,48,50)    # set flexion point for spline
    b=c(0,.2,.28,.7,.8) # set depth for spline flexion point

    curve = spline(a, b, n = 50, method = "natural")$y * depth

    curve = c(curve,rev(curve))

    if (pos == 1){
        a_sequence = seq(x0,x1,length=100)
        b_sequence = seq(y0,y1,length=100)  
    }
    if (pos == 2){
        b_sequence = seq(x0,x1,length=100)
        a_sequence = seq(y0,y1,length=100)      
    }

    # direction
    if(direction==1)
        a_sequence = a_sequence+curve
    if(direction==2)
        a_sequence = a_sequence-curve

    # pos
    if(pos==1)
        lines(a_sequence,b_sequence, lwd=1.5,   xpd=NA) # vertical
    if(pos==2)
        lines(b_sequence,a_sequence, lwd=1.5, xpd=NA) # horizontal

}

windows(width=5, height=5)
par(oma=c(2,0,0,2))
plot(c(),c(), xlim=c(0,11), ylim=c(0,11), xlab='')
# horizontal brace
CurlyBraces(x0=2,  x1=8,  y0=-3, y1=-3, pos = 2, direction = 2, depth=1.5)
# vertical brace
CurlyBraces(x0=12, x1=12, y0=2, y1=6, pos = 1, direction = 1, depth=0.5)

enter image description here

Community
  • 1
  • 1
koekenbakker
  • 3,524
  • 2
  • 21
  • 30