geom_text does not appear using scale_y_log10
Asked Answered
D

4

8

I can add text to a ggplot using geom_text just fine (example using the cars database):

ggplot(cars, aes(speed, dist), color='blue') + 
geom_point(size=0.8) + 
geom_text(y=25, x=5, aes(label=paste("sigma == 1"), size=1.5), 
    hjust=0, parse=TRUE, color='blue')

But when I change the y scale to logarithmic, I can not get the text to appear on the graph:

ggplot(cars, aes(speed, dist), color='blue') + 
geom_point(size=0.8) + 
geom_text(y=25, x=5, aes(label=paste("sigma == 1"), size=1.5), 
    hjust=0, parse=TRUE, color='blue') + 
scale_y_log10()

I've tried varying the text size and position but can't seem to get it.

Dekameter answered 9/1, 2015 at 11:47 Comment(0)
N
7

This works for me:

require(ggplot2)
g <- ggplot(cars, aes(speed, dist), color='blue')
g <- g + geom_point(size=0.8)
g <- g + geom_text(aes(y=25, x=5, label=paste("sigma == 1"), size=1.5), hjust=0, parse=TRUE, color='blue')
g <- g + scale_y_log10()
g
Nymphet answered 9/1, 2015 at 13:7 Comment(2)
Interesting and odd. That works for me. And now, when I reissue the commands as I did above, that too is working. Very strange. Thank you for your help.Dekameter
Well then it was just the y=25 and x=5 being outside the aes which gave ggplot a hickup...Nymphet
S
3

This works better:

ggplot(cars, aes(speed, dist), color='blue') + 
    geom_point(size=0.8) + 
    geom_text(y=2, x=5, aes(label=paste("sigma == 1"), size=1.5), 
        hjust=0, parse=TRUE, color='blue') + 
    scale_y_log10()

enter image description here

Normal to log scale: 100 -> 2; 1000 -> 3; 0.1 -> -1; 0.01 -> -2

Starveling answered 26/6, 2018 at 0:27 Comment(1)
I like the idea of feeding the positions in on the scale of data and using the scales machinery to do the log transformations, rather than computing.Instantaneous
R
3

The issue in the original code is that the x and y values are not wrapped up by aes(), so the axis transformation is not applied to them. To fix, just move the aes() to include all the aesthetics.

ggplot(cars, aes(speed, dist), color='blue') + 
  geom_point(size=0.8) + 
  geom_text(aes(y=25, x=5, label=paste("sigma == 1"), size=1.5), 
            hjust=0, parse=TRUE, color='blue') + 
  scale_y_log10()
Reckford answered 8/6, 2022 at 21:48 Comment(1)
Even though the solution here is the same as the accepted answer, I think this is much better as it includes the explanation.Instantaneous
Y
1

Updating the solution with ggplot2 annotate() function to avoid warnings

library(ggplot2)
# Solution With annotate()
ggplot(cars, aes(speed, dist), color='blue')+
  geom_point(size=0.8)+
  annotate("text",y=25, x=5, label=paste("sigma == 1"), size=5, hjust=0, parse=TRUE, color='blue')+
  scale_y_log10()

with annotate

The old way Note Warnings


# old way Note the annotation is included in legend 
#         Note the warning about Warning aesthetics

ggplot(cars, aes(speed, dist), color='blue')+
  geom_point(size=0.8)+
  geom_text(aes(y=25, x=5, label=paste("sigma == 1"), size=1.5), hjust=0, parse=TRUE, color='blue')+
  scale_y_log10()
#> Warning in geom_text(aes(y = 25, x = 5, label = paste("sigma == 1"), size = 1.5), : All aesthetics have length 1, but the data has 50 rows.
#> ℹ Did you mean to use `annotate()`?

Note warning

Created on 2024-09-03 with reprex v2.1.0

Young answered 3/9 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.