SGH
SGH

Reputation: 53

Scientific notation in ggplot

Can anybody tell me how can I use scientific notation in my plot axis THIS WAY with ggplot? enter image description here

I would like to write the exponential part only in the corners, as you can see in the picture (that plot was made with matplotlib).

Upvotes: 1

Views: 839

Answers (1)

teunbrand
teunbrand

Reputation: 38063

Best I can come up with is to use the annotate() function and disabling coord clipping to place text outside the panel bounds. You may have to play a bit with the exact spacing and the expressions will throw warnings.


library(ggplot2)

df <- data.frame(
  x = 1:10,
  y = 10:1
)

ggplot(df, aes(x, y)) +
  geom_point() +
  annotate("text", -Inf, Inf, label = expression(""%*%10^1),
           hjust = 0, vjust = -1) +
  annotate("text", Inf, -Inf, label = expression(""%*%10^-3),
           hjust = 1, vjust = 2) +
  coord_cartesian(clip = "off") +
  theme(plot.margin = margin(t = 30, b = 10, l = 10, r = 10))
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

Created on 2020-09-11 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions