1

I am using geom_line, geom_point, and geom_text to plot something like the picture below:

enter image description here

I am grouping, and coloring my data frame, but I want the geom_text not to be so close to each other. I want to put the one text on top, and the other on bottom. Or at least, hide the one of the two. Is there any way I can do this?

joran
  • 169,992
  • 32
  • 429
  • 468
Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • 1
    As far as I know, unless you are into manually tuning the positions, `FField` might be the best semi-automated solution. http://stackoverflow.com/questions/6996538/dynamic-position-for-ggplot2-objects-especially-geom-text – Vlo Aug 01 '14 at 14:42
  • any way of providing an array to `hjust` `vjust`? (according to the number of groups..) – Paschalis Aug 01 '14 at 14:51
  • 1
    No, there's no facility for providing multiple values to hjust and vjust. In addition to the question linked above, there is also [this](http://stackoverflow.com/q/7611169/324364) similar question, with many of the same options mentioned. – joran Aug 01 '14 at 15:57

1 Answers1

5

You can specify custom aesthetics in different geom_text() calls. You can include only a subset of the data (such as just one group) in each call, and give each geom_text() a custom hjust or vjust value for each subset.

ggplot(dat, aes(x, y, group=mygroups, color=mygroups, label=mylabel)) +
geom_point() + 
geom_line() + 
geom_text(data=dat[dat$mygroups=='group1',], aes(vjust=1)) + 
geom_text(data=dat[dat$mygroups=='group2',], aes(vjust=-1))
user2034412
  • 4,102
  • 2
  • 23
  • 22
  • This is what I was trying to do! Thanks! – Paschalis Aug 01 '14 at 16:11
  • @Paschalis Note that while this is clever, it will only work in limited circumstances: if all points in each group have the same relative position to each other. i.e. all points in group 1 are above each corresponding point in group 2. – joran Aug 01 '14 at 16:16