Rotating individual text with geom text dataframe

933 views
Skip to first unread message

Thomas

unread,
Mar 3, 2014, 7:01:13 PM3/3/14
to ggp...@googlegroups.com
Assume the following example has n labels that need to be place inside of n boxes. Angle is a set aesthetic that can't draw its value from a data frame variable. Is there a simple way of telling geom_text to rotate specific labels? Obviously breaking it into 2 geom_text calls and 2 data sets would work... but is there something more elegant?

ex.dat<-data.frame(x1=c(200,100), 
                    x2=c(300,200), 
                    y1=c(0,0), 
                    y2=c(0.01,0.02), 
                     a=c(0,90),
                    r=c("label","rotated\nlabel"))

p<-ggplot() + 
  geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75)+
  geom_rect(data=ex.dat, aes(xmin=x1, xmax=x2, ymin=y1,ymax=y2), alpha=0, color="black" )+
  geom_text(data=ex.dat, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r), size=5, angle=a) #this doesn't work, but it works if you remove the "angle = a"
x11() 
p

Thanks,
Thomas

Ista Zahn

unread,
Mar 3, 2014, 7:38:11 PM3/3/14
to Thomas, ggplot2
On Mon, Mar 3, 2014 at 7:01 PM, Thomas <thoma...@maine.edu> wrote:
> Assume the following example has n labels that need to be place inside of n
> boxes. Angle is a set aesthetic that can't draw its value from a data frame
> variable.

Why?

Is there a simple way of telling geom_text to rotate specific
> labels?

Yes, by mapping angle to a variable, e.g.,

ggplot() +
geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl),
alpha=0.6, adjust=0.75)+
geom_rect(data=ex.dat, aes(xmin=x1, xmax=x2, ymin=y1,ymax=y2),
alpha=0, color="black" )+
geom_text(data=ex.dat, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r,
angle = a), size=5)


Obviously breaking it into 2 geom_text calls and 2 data sets would
> work... but is there something more elegant?

There is, but you seem resistant to the idea of mapping angle to a
variable, for reasons that are unclear to me.

Best,
Ista

>
> ex.dat<-data.frame(x1=c(200,100),
> x2=c(300,200),
> y1=c(0,0),
> y2=c(0.01,0.02),
> a=c(0,90),
> r=c("label","rotated\nlabel"))
>
> p<-ggplot() +
> geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6,
> adjust=0.75)+
> geom_rect(data=ex.dat, aes(xmin=x1, xmax=x2, ymin=y1,ymax=y2), alpha=0,
> color="black" )+
> geom_text(data=ex.dat, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=r),
> size=5, angle=a) #this doesn't work, but it works if you remove the "angle =
> a"
> x11()
> p
>
> Thanks,
> Thomas
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Thomas

unread,
Mar 3, 2014, 8:39:01 PM3/3/14
to ggp...@googlegroups.com
Thanks Ista. I was not resistant to it. I had tried i,t but I had put "angle=a"  outside of the aesthetic which was causing the problems)
Reply all
Reply to author
Forward
0 new messages