簡體   English   中英

如何在ggplot2中獲得准確的字體,線條,點和圖形大小?

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

對於最終文章提交,我被要求更新我的數據,以便它們符合以下規范:

  1. 軸線為0.25毫米
  2. 軸線四周都有刻度線
  3. 數據線為0.5毫米
  4. 字體是10pt
  5. 數字應為80或169毫米寬
  6. 必須是300 dpi

我嘗試過的:

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

返回以下兩個圖:

在此輸入圖像描述

在此輸入圖像描述

我可以弄清楚如何處理這里的一些問題(例如傳說位置),但我遇到以下困難:

  1. 如何圍繞頂軸和右軸進行刻度?
  2. 如何才能使尺寸正確...
    • 這些看起來比10磅大得多。 (下載或在新窗口中打開以查看未縮放版本)
    • 盡管在主題(字體,行)中指定,但兩個圖中的尺寸不會保持不變。
    • 我不知道如何確認線條的大小是正確的(以ggsave或毫米為單位)...... ggsave是否有自己的縮放?

更新對於我目前的任務,我導出為svg文件並在Inkscape中進行編輯。 花了幾個小時,但比讓ggplot扭曲到規格更容易。

但是,知道將來如何在ggplot2中以編程方式執行此操作將會很有幫助。

回答問題:1)Henrik在評論中說:

對於問題1(如何在頂部和右側軸上打勾?),請參閱ggplot 2.2.0中scale_中的新sec.axis參數。 試試例如ggplot(mpg,aes(displ,hwy))+ geom_point()+ scale_x_continuous(sec.axis = dup_axis())+ scale_y_continuous(sec.axis = dup_axis())

2)這里的問題是你有不同大小的相同分辨率。 由於兩個數字的高度相同,您可以通過手動將字體大小與寬度的比率相乘來解決此問題:例如,

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

整個代碼應如下所示:

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