[英]R ggplot ggsave produces different plot element sizes than simply exporting in RStudio
When exporting a plot using ggsave()
, nearly all elements (notably, text) of my ggplot are enlarged compared to when viewing and exporting my plot in R studio. 使用
ggsave()
导出绘图时,与在R studio中查看和导出绘图时相比,我的ggplot几乎所有元素(特别是文本)都被放大了。 The reason I'm using ggsave()
is so that I can get the 300 dpi and 85mm width needed for publication. 我使用
ggsave()
的原因是我可以获得出版所需的300 dpi和85mm宽度。
How can I ensure that the ggsave()
output matches what RStudio exports? 如何确保
ggsave()
输出与RStudio导出的匹配?
1. RStudio export (correct proportions): 1. RStudio导出(正确比例):
2. ggsave() export (incorrect proportions): 2. ggsave()导出(比例不正确):
3. My code 我的代码
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"
)
I suspect my issue has something to do with element_text()
being in point units, while the units I've specified with ggsave()
are in mm. 我怀疑我的问题与
element_text()
以点为单位有关,而我用ggsave()
指定的单位是mm。
These two questions are very close to helping me solve my issue, but I don't quite know how to implement them as a solution! 这两个问题非常接近帮助我解决我的问题,但我不太清楚如何将它们作为解决方案来实现!
Change these arguments: 改变这些论点:
width=85, maybe to 850
height=53, maybe to 530
Manipulating with width and hight of plot will give you needed output. 操作图的宽度和高度将为您提供所需的输出。
(As per my comment on Piotr's answer): My hacky workaround is this: width=85 * (14/5), height=53 * (14/5), which is the ratio provided in my second linked question. (根据我对Piotr的回答的评论):我的hacky解决方法是:width = 85 *(14/5),height = 53 *(14/5),这是我在第二个链接问题中提供的比率。 So for now until something better comes along, I'm applying this ratio to all my ggsaves, then resizing to 85 mm in an outside program.
所以现在直到更好的东西出现,我将这个比例应用到我的所有ggsaves,然后在外部程序中调整到85 mm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.