0

I have a plot combined with 6 subplots and I want to output the plot to a pdf file.

But the font size is too small for human seeing.

I reference this website.

And I think maybe the problem is caused by DPI, but the plot nothing change when I give higher DPI to ggsave() with same width and height.

I'd like to make some fake data to illustrate my question.

library(tidyverse)
library(patchwork)
set.seed(1234)
# DATA

# data 1
type<- sample(LETTERS, 5, replace = T) %>% map_chr(~paste0(rep(.,7), collapse = ''))
df0 <- expand.grid(
  type = type,
  year = 2014:2018) %>% 
  mutate(value = runif(25, min = 0, max = 1))
# data 2
name <- sample(LETTERS, 58, replace = T) %>% map_chr(~paste0(rep(.,7), collapse = '')) %>% paste0(1:58)
df <- expand.grid(
  name = name,
  year = 2014:2018,
  month = 1:12) %>% 
  mutate(value = runif(3480, min =0, max = 1),
         x.lab = paste0(year,'_', month))
# data 3
facet = sample(LETTERS, 5, replace = T) %>% map_chr(~paste0(rep(.,7), collapse = ''))
df2 <- expand.grid(
  year = 2014:2018,
  month = 1:12,
  facet = facet) %>% 
  mutate(value = runif(300, min = 0, max = 1))

Then I make 6 plots that imitate my real plot.

# PLOT

# p1 and p2
p1_2 <- df0 %>% ggplot() + 
  geom_area(aes(x = year, y = value, fill = type)) +
  scale_fill_manual(values =c('#D30f8C', '#6B58A6','#FCAF17',  '#0871B9', '#00B3B1'))  +
  theme_classic()
p1 <- p1_2 +
  labs(x = 'Year', fill = 'Disease Type', tag = '(A)')

p2 <- p1_2 +
  labs(x = 'Year', fill = 'Disease Type', tag = '(B)')

# p3 and p4
p3_4 <- df %>% 
  ggplot(aes(y = name, x = x.lab, fill = value)) +
  geom_tile() + 
  theme_classic() + 
  scale_fill_continuous(limits = c(0,1))+
  theme(axis.text.x = element_blank())

p3 <- p3_4 +
  labs(fill = 'Incidence', y = 'Disease name', x = 'Year', tag = "(E)")
p4 <- p3_4 +
  labs(fill = 'Incidence', y = 'Disease name', x = 'Year', tag = "(F)")

# p5 and p6
p5_6 <- ggplot(df2, 
             aes(x = month, y = year, fill = value)) +
  geom_tile(color = 'black') + 
  scale_x_continuous( breaks=seq(from = 1, to = 12, length.out = 12)) +
  coord_polar() +
  ylab("Year")+
  xlab('Month')+ labs(fill = 'Incidence') +
  facet_wrap(facet ~ ., nrow = 1, ncol = 5) +
  theme_classic()  

p5 <- p5_6 + labs(tag = '(C)') 
p6 <- p5_6 + labs(tag = '(D)') 

# COMBINE

pcwork <- (p1+p3) / (p2+p4) /p5 / p6 + plot_layout(guides = 'collect')

Finally, I tried some method to output my plot but all that is not my expected.

# OUTPUT

# font too small
# axis y text too closely in (E) and (F)
ggsave(filename = "foo.pdf", pcwork,
       width = 20, height = 30, dpi = 150, units = "in", device='pdf')

# the spacing between axis y text in (E) and (F) is appropriate now
# but there is large spacing between (C) and (D)
ggsave(filename = "foo2.pdf", pcwork,
       width = 20, height = 50, dpi = 150, units = "in", device='pdf', limitsize = F)

# see if the font size could increase by increasing dpi:
# nothing change
ggsave(filename = "foo3.pdf", pcwork,
       width = 20, height = 50, dpi = 150*100, units = "in", device='pdf', limitsize = F)

# widen the plot
ggsave(filename = "foo4.pdf", pcwork,
       width = 60, height = 30, dpi = 150*100, units = "in", device='pdf', limitsize = F)

I just want every plot in pcwork could be full of the page with appropriate font size for human seeing. Also, the layout should like this :

# (A) XX (E) XX
# (B) XX (F) XX
# (C) XXXXXXXXX
# (D) XXXXXXXXX

Any help will be highly appreciated!

zhiwei li
  • 1,635
  • 8
  • 26

1 Answers1

0

Just changing the DPI isn't going to make the font size larger. You'll need to manually change the font size by editing the theme() of each ggplot. I've included an example for one of the figures below. In my experience changing the font size can mean needing to also change the size of the file you are saving (note the x-axis year labels are very squished).

library(tidyverse)
library(patchwork)
set.seed(1234)

type<- sample(LETTERS, 5, replace = T) %>% map_chr(~paste0(rep(.,7), collapse = ''))
df0 <- expand.grid(
  type = type,
  year = 2014:2018) %>% 
  mutate(value = runif(25, min = 0, max = 1))

df0 %>% ggplot() + 
  geom_area(aes(x = year, y = value, fill = type)) +
  scale_fill_manual(values =c('#D30f8C', '#6B58A6','#FCAF17',  '#0871B9', '#00B3B1'))  +
  labs(x = 'Year', fill = 'Disease Type', tag = '(A)') +
  theme_classic() +
  theme(
    axis.text = element_text(size = 25),
    axis.title = element_text(size = 30),
    legend.text = element_text(size = 25),
    legend.title = element_text(size = 30)
  )

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

Jeffrey Brabec
  • 481
  • 6
  • 11