我正在生成一个ggplot plot
并将其另存为.png
图像。虽然 Rstudio 中生成的绘图根据 y 轴的值进行拉伸,但当我将其另存为时,我会得到一个正方形的图像.png
.
如何自动获得最佳拉伸图像.png
form?
# Function to store ggplot plot object as .png image file
savePlot <- function(myPlot, filename) {
png(filename)
print(myPlot)
dev.off()
}
# ggplot object
normalized_bar_plot = ggplot(dat, aes(factor(temp), norm, fill = type)) +
geom_bar(stat="identity", position = "dodge") + ylab("Normalized count")+xlab(features[i])+
scale_fill_brewer(palette = "Set1")
filename = paste0("image_", features[i], ".png")
savePlot(normalized_bar_plot, filename)
为了节省ggplot
数字,我会用ggsave
。默认情况下,这会选择绘图设备的大小。因此,如果您在屏幕上的绘图设备中正确设置纵横比,这将转换为保存的图像文件。此外,它还支持通过设置图像的宽度、高度和dpiwidth
, height
and dpi
输入参数。
例如:
ggplot(dat, aes(factor(temp), norm, fill = type)) +
geom_bar(stat="identity", position = "dodge") + ylab("Normalized count")+xlab(features[i])+
scale_fill_brewer(palette = "Set1")
# ggsave will save the last generated image, it will also pick up which file format
# to use from the file extension (e.g. png).
ggsave('~/saved_image.png', width = 16, height = 9, dpi = 100)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)