简体   繁体   English

在 r 中输出具有适当宽度和高度的图

[英]output a plot with appropriate width and height in r

I have a plot combined with 6 subplots and I want to output the plot to a pdf file.我有一个结合 6 个子图的图,我想将图输出到 pdf 文件。

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 .而且我认为问题可能是由 DPI 引起的,但是当我为ggsave()以相同的widthheight提供更高的 DPI 时,情节没有任何变化。

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.然后我制作了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')

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.我只希望pcwork每个图都可以充满页面,并具有适当的字体大小以供人类查看。 Also, the layout should like this :此外,布局应该是这样的:

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

Any help will be highly appreciated!任何帮助将不胜感激!

Just changing the DPI isn't going to make the font size larger.仅更改 DPI 不会使字体变大。 You'll need to manually change the font size by editing the theme() of each ggplot.您需要通过编辑每个 ggplot 的theme()来手动更改字体大小。 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).根据我的经验,更改字体大小可能意味着还需要更改要保存的文件的大小(请注意,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)
  )

Created on 2020-11-19 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 11 月 19 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM