wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
651 stars 37 forks source link

extra space #46

Closed karlropkins closed 3 years ago

karlropkins commented 3 years ago

Hi, First quick note: Love the package; usually saves me loads of time. Not sure if this is issue or something I am missing... When I run below it adds extra space before superscripted -3 and maybe squashes yr and superscripted -1 together too much. Any fix or workaround for this?

require(ggplot2)
require(ggtext)

d <- data.frame(a = 1:10, b=1:10)
ggplot(d) + 
  geom_point(aes(a, b)) + 
  xlab("some text<br>NO<sub>2</sub> [&mu;g.m<sup>-3</sup>.yr<sup>-1</sup>]") +
  theme(axis.title.x = element_markdown())
clauswilke commented 3 years ago

It would help if you could post the output you get (e.g., by running your code through the reprex package). From what I see, the rendering is correct, but the r and the - clash somewhat. This may be font related. A different font might look better. One thing you could try is use a proper minus sign &minus; or an en-dash &ndash;.

require(ggplot2)
#> Loading required package: ggplot2
require(ggtext)
#> Loading required package: ggtext

d <- data.frame(a = 1:10, b=1:10)
ggplot(d) + 
  geom_point(aes(a, b)) + 
  xlab("some text<br>NO<sub>2</sub> [&mu;g.m<sup>-3</sup>.yr<sup>-1</sup>]") +
  theme(axis.title.x = element_markdown())

ggplot(d) + 
  geom_point(aes(a, b)) + 
  xlab("some text<br>NO<sub>2</sub> [&mu;g.m<sup>&minus;3</sup>.yr<sup>&minus;1</sup>]") +
  theme(axis.title.x = element_markdown())

Created on 2020-08-21 by the reprex package (v0.3.0)

karlropkins commented 3 years ago

Rplot.pdf

karlropkins commented 3 years ago

(sorry replex new to me but attached is what I get)

karlropkins commented 3 years ago

Interesting. The reprex work fine using my original code. Did some further checking and it is just when I run code directly in Rstudio. Fine in R console and using reprex. Appreciate any suggestion, but now assuming it is something to do with my rstudio, I'll update so if that fixes it. Thanks for your help/time

require(ggplot2)
#> Loading required package: ggplot2
require(ggtext)
#> Loading required package: ggtext

d <- data.frame(a = 1:10, b=1:10)
ggplot(d) + 
  geom_point(aes(a, b)) + 
  xlab("some text<br>NO<sub>2</sub> [&mu;g.m<sup>-3</sup>.yr<sup>-1</sup>]") +
  theme(axis.title.x = element_markdown())

Created on 2020-08-21 by the reprex package (v0.3.0)

clauswilke commented 3 years ago

The problem may be the specific graphics device you're using (which you may not even be aware of).

To have complete control over the graphics device, you can do something like this:

p <- ggplot(d) + 
  geom_point(aes(a, b)) + 
  xlab("some text<br>NO<sub>2</sub> [&mu;g.m<sup>-3</sup>.yr<sup>-1</sup>]") +
  theme(axis.title.x = element_markdown())

pdf("test.pdf")  # pdf() graphics device
print(p)
dev.off()

In case of doubt, the ragg graphics devices tend to work the best, but they only produce bitmap output.