繁体   English   中英

R ggplot ggsave产生的图元素大小不同于在RStudio中简单导出

[英]R ggplot ggsave produces different plot element sizes than simply exporting in RStudio

使用ggsave()导出绘图时,与在R studio中查看和导出绘图时相比,我的ggplot几乎所有元素(特别是文本)都被放大了。 我使用ggsave()的原因是我可以获得出版所需的300 dpi和85mm宽度。

如何确保ggsave()输出与RStudio导出的匹配?

1. RStudio导出(正确比例):

RStudio导出

2. ggsave()导出(比例不正确):

ggsave出口

我的代码

library(ggplot2)
library(cowplot)
library(grid)

# Make plot
fig4 <- ggplot()
# Add Landings lines
fig4 <-
  fig4 + geom_line(
    data = NorRus,
    aes(year, tonnes, color = fishing_entity),
    size = 0.75
    )
# Change colors. 
fig4 <-
  fig4 + scale_color_manual(
    values = bluesPalette,
    name = NULL
    )
# adjust y axis labels, remove scientific notation
fig4 <-
  fig4 + scale_y_continuous(
    expand = c(0,0), #removes stupid gap btwn plot & axes
    breaks = seq(0, 2500000, 500000),
    limits = c(0, 2500000),
    labels = divide1000() # divide units by 1000 w function specified above 
  )
# adjust x axis labels
fig4 <-
  fig4 + scale_x_continuous(
    expand = c(0,0), # removes stupid gap btwn plot & axes
    breaks = seq(1950, 2014, 10),
    limits = c(1950, 2014)
  )
# Add titles to axes
fig4 <- 
  fig4 + labs(
    y = Land10e3,
    x = "Year"
  )
# Adjust axis margins
fig4 <- 
  fig4 + theme(
    axis.title.y=element_text(margin=margin(0,20,0,0)),
    axis.title.x=element_text(margin=margin(20,0,0,0))
  )
# Adjust text sizes
fig4 <- 
  fig4 + theme(
    legend.title=element_text(size=10), # Legend title
    legend.text=element_text(size=8), # Legend items
    axis.title=element_text(size=10), # Axis titles
    axis.text=element_text(size=8) # Axis labels
  )
# move legend to inside
fig4 <- 
  fig4 + theme(
    legend.justification = c(1, 1), 
    legend.position = c(1, 1)
  )
# Plot
plot(fig4)

# Export plot w ggsave
ggsave(
  "R exports/fig4-ggsave.tiff",
  plot = fig4, 
  device = "tiff", 
  dpi=150, 
  width=85, 
  height=53, 
  units = "mm"
  )

我怀疑我的问题与element_text()以点为单位有关,而我用ggsave()指定的单位是mm。

这两个问题非常接近帮助我解决我的问题,但我不太清楚如何将它们作为解决方案来实现!

  1. ggplots2 ggsave文本大小没有变化
  2. ggplot geom_text字体大小控件

改变这些论点:

width=85, maybe to 850
height=53, maybe to 530

操作图的宽度和高度将为您提供所需的输出。

(根据我对Piotr的回答的评论):我的hacky解决方法是:width = 85 *(14/5),height = 53 *(14/5),这是我在第二个链接问题中提供的比率。 所以现在直到更好的东西出现,我将这个比例应用到我的所有ggsaves,然后在外部程序中调整到85 mm。

暂无
暂无

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

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