Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

showtext appears to be double spacing labels/titles #26

Closed
PDjupe opened this issue Oct 23, 2018 · 8 comments
Closed

showtext appears to be double spacing labels/titles #26

PDjupe opened this issue Oct 23, 2018 · 8 comments

Comments

@PDjupe
Copy link

PDjupe commented Oct 23, 2018

Love the ease of this package. But when I use the "\n" function to separate labels to a second line I am getting double spacing which I can't correct outside of photoshop. Here's an example with labels and titles showing the double spacing.

fpol %>% filter(pid7!="NA") %>% ggplot(., aes(x=q18_1, y=factor(pid7), fill=..x..)) +
geom_density_ridges_gradient(rel_min_height=.02) +
scale_fill_gradient2(low="dodgerblue3", high="firebrick3", mid="white", midpoint=50) +
theme_minimal() +
labs(x="", y="", title="Only Strong Republicans Are Happy\nwith Trump Admin's Management", caption="Source: February 2018") +
theme(legend.position = "none") +
scale_x_continuous(breaks=c(0,25,50,75,100), labels=c("Extremely\nUnhappy","25","Neither\nHappy\nNor\nUnhappy", "75", "Extremely\nHappy")) +
theme(text=element_text(family="JSans", size=20))

happy_w_trump

@larmarange
Copy link

Same issue observed

@yixuan
Copy link
Owner

yixuan commented Oct 24, 2018

I'll take a look some time later this week.

@yixuan
Copy link
Owner

yixuan commented Dec 14, 2018

I was trying to investigate this issue today, but I couldn't reproduce it. The spacing seems correct for the minimal example below. Can someone give me a (runnable) example that has double spacing?

library(showtext)
library(ggplot2)

showtext_auto()

dat = data.frame(x = c("AAA\nAAA", "BBB\nBBB", "CCC\nCCC"), y = 1:3)
x11()
ggplot(dat, aes(x = x, y = y)) +
    geom_bar(stat = "identity") +
    ggtitle("First Line\nsecond line") +
    theme_grey(base_family = "sans")

image

@PDjupe
Copy link
Author

PDjupe commented Dec 14, 2018 via email

@larmarange
Copy link

The issue is also observed when using showtext within a Rmarkdown document.

@yixuan
Copy link
Owner

yixuan commented Dec 14, 2018

OK, now I know the problem. The fact is not that the spacing is doubled, but that the font sizes are made smaller. ggsave() has a default dpi = 300 argument, while showtext_opts() has dpi = 96. Simply add the following line will fix the issue.

showtext_opts(dpi = 300)

The same reason applies to Rmarkdown. You need to make sure the dpi knitr option is consistent with the one in showtext.

@larmarange
Copy link

Would it be possible to find a way in Rmarkdown for showtext to detect automatically the dpi used by knitr?

@yixuan
Copy link
Owner

yixuan commented Jan 10, 2019

@larmarange I find that adding the following option to the document header automatically solves the problem. Can you confirm this?

---
output:
    html_document:
        fig_retina: 1
---

@yixuan yixuan closed this as completed Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants