2016-11-14 70 views
14

對於最終的文章提交我一直在問,讓他們符合以下規格來更新我的數字:如何在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') 

它返回以下兩個地塊:

enter image description here

enter image description here

我可以計算出如何處理一些問題在這裏(例如傳說中的位置),但在以下方面遇到困難:

  1. 我怎樣才能得到圍繞頂部和右側軸的蜱?
  2. 我怎樣才能獲得正確的尺寸...
    • 這些看起來比10磅大得多。 (下載它們或在新窗口中打開以查看未縮放版本)
    • 儘管在主題(字體,行)中指定了尺寸,但兩個圖中的尺寸不會保持不變。
    • 我不知道如何確認線是正確的大小(以點或毫米)... ggsave做它自己的縮放嗎?

更新我現在的工作,我導出爲SVG文件和Inkscape中編輯它們。它花了幾個小時,但比讓ggplot扭曲到規格更容易。

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

+6

問題1(我怎樣才能解決頂部和右側軸蜱?),看到'scale_新的'sec.axis'說法'[ggplot 2.2.0](https://blog.rstudio.org/2016/11/14/ggplot2-2-2-0/)。嘗試'ggplot(mpg,aes(displ,hwy))+ geom_point()+ scale_x_continuous(sec.axis = dup_axis())+ scale_y_continuous(sec.axis = dup_axis())''。 – Henrik

+0

對我來說,它看起來像軸標籤「x」和「y」大約10磅的權利,並且所有其他文本更小。在300 dpi(ppi)的分辨率下,10 pt是10/72 * 300像素或約42像素。 – mvkorpel

+0

您應該更具體地說明兩個圖中未保留尺寸的含義。兩個圖中的線條寬度和字體大小看起來相同。 – mvkorpel

回答

0

回答質疑: 1)亨裏克告訴記者,在註釋:

問題1(我怎樣才能解決頂部和右側軸蜱),請參閱scale_新sec.axis論點? ggplot 2.2.0。嘗試ggplot(MPG,AES(顯示終端,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')