0

I am applying labels to a plot similar to this example. The labels would be the numeric value of the points on the line. They keep getting cut off. I am trying to figure out how to reliably adjust the y-axis limits so all labels fit in the plot window. I have not been able to find anything that works consistently. Thank you for your help!

library(ggplot2)

mymin <- -1000000
mymax <- -mymin
myinterval <- 100000

myseq <- seq(mymin,mymax,myinterval)

temp <- data.frame(X=myseq,Y=myseq,Name=rep("labels",length(myseq)))

p <- ggplot(temp,aes(x=X,y=Y)) + geom_line() + 
    geom_text(data=subset(temp,Y<0),aes(x=X,y=Y,label=Name),angle=90,hjust=1)+
    geom_text(data=subset(temp,Y>0),aes(x=X,y=Y,label=Name),angle=90,hjust=0)+
    scale_y_continuous(labels = scales::label_number_si())+
    scale_x_continuous(labels = scales::label_number_si())
    
p

My session info:

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default
 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] scales_1.1.1    ggplot2_3.3.2    
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Use `expand` in your x & y scales to change how much space is included at either end. As an aside, `geom_text(aes(label = Name, hjust = ifelse(Y < 0, 1, 0)), angle = 90)` will set your `hjust` with just one `geom_text` call and without subsetting your data, or you could add a variable to the data with that same `ifelse` statement and map it to `hjust` inside of your `aes`. I pretty rarely see `hjust` used as an aesthetic, but it's legal – camille Jul 29 '21 at 16:24
  • 1
    Dosn't fix all, and might look bad sometimes, but `p + coord_cartesian(clip = "off")` will resolve some situations. – Jon Spring Jul 29 '21 at 16:29
  • Thank you very much! The expand and coord_cartesian approaches are both very helpful, but they require I set arbitrary limit amounts which may be too much or two little in some cases. Is there a way to extract the characteristics of the geom_text labels - specifically the y-distance they take up (since I'm angling at 90) so I could then calculate the exact limits to use in the expand argument? – Matthew Jul 29 '21 at 18:51
  • related problem, still one of the open questions here https://stackoverflow.com/questions/55686910/how-can-i-access-dimensions-of-labels-plotted-by-geom-text-in-ggplot2 – tjebo Jul 29 '21 at 20:20

1 Answers1

0

It's not perfect, but expand_limits is a quick way to get what you want. It's not automatic, but it would be pretty easy to use it on a plot-by-plot basis.

p <- ggplot(temp,aes(x=X,y=Y)) + geom_line() + 
  geom_text(data=subset(temp,Y<0),aes(x=X,y=Y,label=Name),angle=90,hjust=1)+
  geom_text(data=subset(temp,Y>0),aes(x=X,y=Y,label=Name),angle=90,hjust=0)+
  scale_y_continuous(labels = scales::label_number_si())+
  scale_x_continuous(labels = scales::label_number_si())+
  expand_limits(y = c(-1100000,1100000))

p

enter image description here

Dan Spencer
  • 151
  • 5
  • 2
    Thank you very much! The expand_limits approach is very helpful, but requires I set arbitrary limit amounts which may be too much or two little in some cases. Is there a way to extract the characteristics of the geom_text labels - specifically the y-distance they take up (since I'm angling at 90) so I could then calculate the exact limits to use in the expand argument? Th – Matthew Jul 29 '21 at 18:52
  • If there is a way to extract that information from a grob, I haven't heard of it. I understand what you mean about wanting to make this happen automatically. `expand_limits` will not work cleanly in certain scenarios (like when using `facet_grid` or `facet_wrap`, for example). – Dan Spencer Jul 29 '21 at 19:19