Discussion:
geom_text with angle sensitive to aspect ratio?
Christian Kreibich
2011-04-18 17:27:59 UTC
Permalink
I am trying to produce text annotations inside a plot along lines of
various angles. The following produces a plot with a diagonal line that
has the annotation sitting correctly alongside it:

d <- data.frame(x=c(0,100), y=c(0,100))
d.anno <- data.frame(x=c(50), y=c(50))

ggplot(d, aes(x, y)) + geom_line() +
geom_text(aes(x, y*1.08), data=d.anno, label='annotation', angle=45)

ggsave('ok.png', p, width=7, height=7)

The problem here is that the correctness depends on the aspect ratio of
the plot -- if you make the plot half as tall as it is wide, for
example, then the text no longer aligns:

ggsave('rats.png', p, width=7, height=3.5)

Unfortunately in my setting I cannot manually adjust the labels to look
right. I have tried to factor the resulting aspect ratio into the angle,
but this gets messy and imprecise once you have plot grids of varying
dimensions via facets.

Is there a better way to do this?
--
Cheers,
Christian
jrandall
2011-04-20 03:42:30 UTC
Permalink
Christian,

Unfortunately, I don't think there is an easy way to achieve exactly
what you are looking to do at the moment. The issue is that the
geom_text angle parameter is intended only to specify the angle of the
text on the final plot, without respect to the ratio of the x and y
scales. If ggplot2 is free to scale both axes independently, I don't
think there is a way for you to get the information about what the
ratio between them is.

However, if you are willing to decide on an aspect ratio and manully
set it, that _is_ possible and should solve your problem. For
example, you can force a 1:1 ratio of the axis scales by using
coord_fixed(ratio=1).
ggplot(d, aes(x, y)) + geom_line() + geom_text(aes(x, y*1.08), vjust=0, data=d.anno, label='annotation', angle=45) + coord_fixed(ratio=1)
ggsave('ok.png', p, width=7, height=7)
ggplot(d, aes(x, y)) + geom_line() + geom_text(aes(x, y*1.08), vjust=0, data=d.anno, label='annotation', angle=26.56505) + coord_fixed(ratio=1/2)
ggsave('ok.png', p, width=7, height=3.5)
You may also be interested to know that angle is an aesthetic that you
can set to be based on your data, such that you can calculate it
(using trig functions on the slope of the line and multiplying by the
aspect ratio). To facilitate making your example a bit more
automated, I might change the data set to have start and end points
for each line and calculate everything else automatically.
ratio = 1/2 ### set to the desired fixed aspect ratio
d <- data.frame(xmin=c(0,0,0), xmax=c(50,100,100), ymin=c(0,0,0), ymax=c(100,100,50), annotation=c("slope 1/2","slope 1","slope 2"))
ggplot(data=d, mapping=aes(x=xmin, xend=xmax, y=ymin, yend=ymax, label=annotation)) + geom_segment() + geom_text(mapping=aes(x=(xmax-xmin)/2,y=(ymax-ymin)/2, angle=atan2((ymax-ymin)*ratio,(xmax-xmin))*180/pi), vjust=-0.5) + coord_fixed(ratio=ratio)
Should do what you want, provided you don't mind deciding on the
aspect ratio ahead of time.

Hope this helps!

Josh.
I am trying to produce text annotations inside a plot along lines of
various angles. The following produces a plot with a diagonal line that
  d <- data.frame(x=c(0,100), y=c(0,100))
  d.anno <- data.frame(x=c(50), y=c(50))
  ggplot(d, aes(x, y)) + geom_line() +
    geom_text(aes(x, y*1.08), data=d.anno, label='annotation', angle=45)
  ggsave('ok.png', p, width=7, height=7)
The problem here is that the correctness depends on the aspect ratio of
the plot -- if you make the plot half as tall as it is wide, for
  ggsave('rats.png', p, width=7, height=3.5)
Unfortunately in my setting I cannot manually adjust the labels to look
right. I have tried to factor the resulting aspect ratio into the angle,
but this gets messy and imprecise once you have plot grids of varying
dimensions via facets.
Is there a better way to do this?
--
Cheers,
Christian
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
More options: http://groups.google.com/group/ggplot2
Loading...