【问题标题】:output a plot with appropriate width and height in r在 r 中输出具有适当宽度和高度的图
【发布时间】:2020-11-19 09:30:26
【问题描述】:

我有一个包含 6 个子图的图,我想将图输出到 pdf 文件。

但字号太小,人类无法看到。

我引用了这个website

我认为问题可能是由 DPI 引起的,但是当我给 ggsave() 提供更高的 DPI 并使用相同的 widthheight 时,情节没有任何变化。

我想制作一些假数据来说明我的问题。

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))

然后我制作了 6 个模仿我真实情节的情节。

# 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')

最后,我尝试了一些方法来输出我的情节,但这一切都不是我的预期。

# 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)

我只希望pcwork 中的每个情节都可以以适当的字体大小填满页面,以供人类查看。另外,布局应该是这样的:

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

任何帮助将不胜感激!

【问题讨论】:

    标签: r ggplot2 plot tidyverse dpi


    【解决方案1】:

    仅更改 DPI 不会使字体变大。您需要通过编辑每个 ggplot 的 theme() 来手动更改字体大小。我已经为下面的数字之一提供了一个示例。根据我的经验,更改字体大小可能意味着还需要更改要保存的文件的大小(注意 x 轴年份标签非常挤压)。

    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)
      )
    

    reprex package (v0.3.0) 于 2020 年 11 月 19 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      相关资源
      最近更新 更多