[R] GGPLOT Clipping Regions

hadley wickham h.wickham at gmail.com
Mon Jul 20 13:43:19 CEST 2009


On Fri, Jul 17, 2009 at 3:39 AM, Alex Brown<fishtank at compsoc.man.ac.uk> wrote:
>
> Sure thing, an example.:
>
> ggplot
> (data.frame(name=letters[1:3],value=c(1,4,9),type=c("fun","fun","not
> fun")),aes(x=name,y=value,fill=type,label=paste("value is ",value))) +
> geom_bar() + coord_flip() + geom_text(hjust=0)
>
> I'd like the option to allow the text " value is 9 " to spill out of the
> plot region into the unused border partially occupied by the legend.

Ah ok.  Unfortunately, there's currently no way for ggplot2 to figure
out how to increase the range of the x axis to avoid clipping text
within the plot.  This means you have to extend the y axis yourself,
e.g. with + ylim(0, 12)

> Also, is it possible to move each label right by one em (1 nominal font
> height)?  I think you can do that in grid graphics, but I can't see how to
> do it here.  I suppose I could always add an em space to the start of the
> text, but I was thinking of something like x = value + unit(1,"char")

You can't do it with ems, but you can do it with data units:

df <- data.frame(
  name = letters[1:3],
  value = c(1,4,9),
  type = c("fun","fun","not fun")
)

ggplot(df, aes(name, value, fill = type)) +
  geom_bar() +
  geom_text(aes(label=paste("value is ",value), y = value + 0.5), hjust = 0)
  coord_flip() +

Hadley

PS.  You might also want to join the ggplot2 mailing list:
http://groups.google.com/group/ggplot2

-- 
http://had.co.nz/




More information about the R-help mailing list