简体   繁体   English

如何在ggplot2中获得准确的字体,线条,点和图形大小?

[英]How do I get exact font, line, point and figure sizes in ggplot2?

For a final article submission I have been asked to update my figures so that they meet the following specifications: 对于最终文章提交,我被要求更新我的数据,以便它们符合以下规范:

  1. axis lines are 0.25 mm 轴线为0.25毫米
  2. axis lines all around with ticks facing in 轴线四周都有刻度线
  3. data lines are 0.5 mm 数据线为0.5毫米
  4. font is 10pt 字体是10pt
  5. figures should be 80 or 169 mm wide 数字应为80或169毫米宽
  6. must be 300 dpi 必须是300 dpi

What I've tried: 我尝试过的:

library(ggplot2)
library(cowplot)
theme_set(theme_bw())

x <- rnorm(100)
mydata <- data.frame(x = x,
                     y = x^2 + runif(100),
                     z = rep(letters[1:4], 25))

p <- ggplot(data = mydata, aes(x, y)) + 
  geom_point(aes(color = z)) + 
  geom_smooth(color = 'black', se = FALSE, size = 0.5) +
  theme(text = element_text(family = 'Times', size = 10, color = 'black'),
        axis.ticks.length = unit(-0.1, 'cm'),
        axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),
        axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),
        panel.grid = element_blank(),
        axis.line = element_line(size = 0.25),
        legend.position = c(0.5, 0.75))

p
ggsave(plot = p, 
       filename = 'myplot.png', 
       width = 80, height = 50, dpi = 300, units = 'mm')

p2 <- cowplot::plot_grid(plotlist = list(p, p, p, p), nrow = 1)
ggsave(plot = p2,
       filename = 'mymultipleplot.png',
       width = 169, height = 50, dpi = 300, units = 'mm')

Which returns the following two plots: 返回以下两个图:

在此输入图像描述

在此输入图像描述

I can figure out how to handle some of the issues here (eg legend positions), but am having difficulty with the following: 我可以弄清楚如何处理这里的一些问题(例如传说位置),但我遇到以下困难:

  1. How can I get ticks around top and right axes? 如何围绕顶轴和右轴进行刻度?
  2. How can I get the sizes correct ... 如何才能使尺寸正确...
    • These look much bigger than 10 pt. 这些看起来比10磅大得多。 (download them or open in new window to see unscaled version) (下载或在新窗口中打开以查看未缩放版本)
    • The sizes are not maintained in the two figures despite being specified in the theme (font, line). 尽管在主题(字体,行)中指定,但两个图中的尺寸不会保持不变。
    • I don't know how to confirm that the lines are the correct size (in points or mm)... does ggsave do its own scaling? 我不知道如何确认线条的大小是正确的(以ggsave或毫米为单位)...... ggsave是否有自己的缩放?

update For my present task I exported as svg files and edited them in Inkscape. 更新对于我目前的任务,我导出为svg文件并在Inkscape中进行编辑。 It took a few hours but was easier than getting ggplot to contort to the specifications. 花了几个小时,但比让ggplot扭曲到规格更容易。

But, it would be helpful to know for the future how to do this programmatically within ggplot2. 但是,知道将来如何在ggplot2中以编程方式执行此操作将会很有帮助。

Answer to question: 1) as Henrik told in comments: 回答问题:1)Henrik在评论中说:

For question 1 (How can I get ticks around top and right axes?), see the new sec.axis argument in scale_ in ggplot 2.2.0. 对于问题1(如何在顶部和右侧轴上打勾?),请参阅ggplot 2.2.0中scale_中的新sec.axis参数。 Try eg ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous(sec.axis = dup_axis()) + scale_y_continuous(sec.axis = dup_axis()) 试试例如ggplot(mpg,aes(displ,hwy))+ geom_point()+ scale_x_continuous(sec.axis = dup_axis())+ scale_y_continuous(sec.axis = dup_axis())

2) the problem here is that you have the same resolution with different sizes. 2)这里的问题是你有不同大小的相同分辨率。 Since the height of the two figures is the same, you can fix this problem scaling the font size by hand multiplying the font-size with the ratio of the width: eg 由于两个数字的高度相同,您可以通过手动将字体大小与宽度的比率相乘来解决此问题:例如,

theme(text = element_text(family = 'Times', size = 10*(80/169), color = 'black')

The whole code should look like this: 整个代码应如下所示:

library(ggplot2)
library(cowplot)
theme_set(theme_bw())

x <- rnorm(100)
mydata <- data.frame(x = x,
                     y = x^2 + runif(100),
                     z = rep(letters[1:4], 25))

p1 <- ggplot(data = mydata, aes(x, y)) + 
  geom_point(aes(color = z)) + scale_x_continuous(sec.axis = dup_axis()) + 
  scale_y_continuous(sec.axis = dup_axis()) + 
  geom_smooth(color = 'black', se = FALSE, size = 0.5) +
  theme(text = element_text(family = 'Times', size = 10*(80/169), color = 'black'),
        axis.ticks.length = unit(-0.1, 'cm'),
        axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),


        axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),

        panel.grid = element_blank(),
        axis.line = element_line(size = 0.25),
        legend.position = c(0.5, 0.75))

p2 <- ggplot(data = mydata, aes(x, y)) + 
  geom_point(aes(color = z)) + scale_x_continuous(sec.axis = dup_axis()) + 
  scale_y_continuous(sec.axis = dup_axis()) + 
  geom_smooth(color = 'black', se = FALSE, size = 0.5) +
  theme(text = element_text(family = 'Times', size = 10, color = 'black'),
        axis.ticks.length = unit(-0.1, 'cm'),
        axis.text.x = element_text(margin = margin(t = 4, unit = 'mm')),


        axis.text.y = element_text(margin = margin(r = 4, unit = 'mm')),

        panel.grid = element_blank(),
        axis.line = element_line(size = 0.25),
        legend.position = c(0.5, 0.75))


p1
ggsave(plot = p1, 
       filename = 'myplot.png', 
       width = 80, height = 50, dpi = 300, units = 'mm')

p2multi <- cowplot::plot_grid(plotlist = list(p2, p2, p2, p2), nrow = 1)

ggsave(plot = p2multi ,
       filename = 'mymultipleplot.png',
       width = 169, height = 50, dpi = 300, units = 'mm')

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

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