Friday, 15 February 2013

r - ggplot2: Curly braces on an axis? -



r - ggplot2: Curly braces on an axis? -

in answering recent visualization question needed braces show span on axis, , can't figure out how in ggplot2. here's plot:

instead of tick mark, i'd y axis label "second letter of two-letter names" have brace extending 1 10 (the vertical span of reddish , bluish sec letters). i'm not sure how create happen. x axis benefit similar treatment.

code available in linked crossvalidated question (and unnecessarily complicated example, won't show it). instead, here's minimal example:

library(ggplot2) x <- c(runif(10),runif(10)+2) y <- c(runif(10),runif(10)+2) qplot(x=x,y=y) + scale_x_continuous("",breaks=c(.5,2.5),labels=c("low types","high types") )

in case, brace (0,1) low types , (2,3) high types ideal instead of tick marks.

i'd rather not utilize geom_rect because:

the tick marks remain i'd prefer braces it within plot instead of outside it

how accomplish this? perfect reply have:

a nice, smooth, lean curly brace drawn outside plotting area specified via high-level argument (ideally range-type object passed breaks alternative in scale_x_continuous)

the op requested bracket off plot. solution uses axis.ticks.length in combination axis.ticks = element_blank() allow brace outside plotting area. reply builds upon of @pankil , @user697473: utilize pbrackets r bundle -- , include pictures!

library(ggplot2) library(grid) library(pbrackets) x <- c(runif(10),runif(10)+2) y <- c(runif(10),runif(10)+2) the_plot <- qplot(x=x,y=y) + scale_x_continuous("",breaks=c(.5,2.5),labels=c("low types","high types") ) + theme(axis.ticks = element_blank(), axis.ticks.length = unit(.85, "cm")) #run grid.locator few times coordinates outer #most points of bracket, making sure #bottom_y coordinate @ bottom of grayness area. # exit grid.locator nail esc; after setting coordinates # in grid.bracket comment out grid.locator() line the_plot grid.locator(unit="native") bottom_y <- 284 grid.brackets(220, bottom_y, 80, bottom_y, lwd=2, col="red") grid.brackets(600, bottom_y, 440, bottom_y, lwd=2, col="red")

a quick note on @pankil's answer:

## bracket coordinates depend on size of plot ## instance, ## pankil's suggested bracket coordinates not work ## next sizing: the_plot grid.brackets(240, 440, 50, 440, lwd=2, col="red") grid.brackets(570, 440, 381, 440, lwd=2, col="red") ## 440 seems off graph...

and couple more showcase functionality of pbrackets:

#note, if reverse x1 , x2, bracket flips: the_plot grid.brackets( 80, bottom_y, 220, bottom_y, lwd=2, col="red") grid.brackets(440, bottom_y, 600, bottom_y, lwd=2, col="red")

## go vertical: the_plot grid.brackets(235, 200, 235, 300, lwd=2, col="red") grid.brackets(445, 125, 445, 25, lwd=2, col="red")

r ggplot2

No comments:

Post a Comment