ggplot2: Curly braces on an axis?

前端 未结 5 1244
执笔经年
执笔经年 2020-11-28 09:30

In answering a recent visualization question I really needed braces to show a span on an axis, and I can\'t figure out how to do it in ggplot2. Here\'s the plot:

5条回答
  •  抹茶落季
    2020-11-28 10:06

    Here is kludgy solution in ggplot that constructs a line drawing that vaguely resembles a curly bracket.

    Construct a function that takes as input the position and dimensions of a curly bracket. What this function does is to specify the co-ordinates of an outline drawing of a bracket and uses some math scaling to get it to the size and position desired. You can use this principle and modify the co-ordinates to give you any desired shape. In principle you can use the same concept and add curves, ellipses, etc.

    bracket <- function(x, width, y, height){
      data.frame(
          x=(c(0,1,4,5,6,9,10)/10-0.5)*(width) + x,
          y=c(0,1,1,2,1,1,0)/2*(height) + y
      )
    }
    

    Pass that to ggplot and specifically geom_line

    qplot(x=x,y=y) +
        scale_x_continuous("",breaks=c(.5,2.5), labels=c("Low types","High types")) +
        geom_line(data=bracket(0.5,1,0,-0.2)) +
        geom_line(data=bracket(2.5,2,0,-0.2))
    

    enter image description here

提交回复
热议问题