R语言 ggplot2 绘图:导出图片为图像文件
要将做好的图片导出为图像文件,你可以使用 ggsave 函数来保存绘图结果。
以下是导出图片的代码示例:
# 首先我们要将group字段的数据类型改为因子型,并命名相应的水平
comparison_hp$group <- factor(comparison_hp$group,
levels = c(1, 2),
labels = c('Patient', 'Healthy'))
# 安装并加载 ggsignif 包
install.packages('ggsignif')
library(ggsignif)
# 制作箱线图(调整箱状图宽度)
plot <- ggplot(comparison_hp, aes(x = factor(group), y = ISQ_adjusted)) +
geom_boxplot(width = 0.5) + # 调整宽度为 0.5
scale_fill_manual(values = c('black', 'grey'),
name = 'Group',
labels = c('Patient', 'Healthy')) +
labs(title = 'Boxplot of Adjusted ISQ Scores',
x = expression(bold('Group')),
y = expression(bold('Adjusted ISQ Score'))) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(color = 'black', size = 12, face = 'bold'),
axis.text.y = element_text(color = 'black', size = 12, face = 'bold'),
axis.title.x = element_text(color = 'black', size = 12, face = 'bold'),
axis.title.y = element_text(color = 'black', size = 12, face = 'bold')) +
guides(fill = FALSE) +
coord_cartesian(ylim = c(-60, 60)) +
geom_signif(comparisons = list(c('Patient', 'Healthy')),
map_signif_level = TRUE,
textsize = 4)
# 导出图片
ggsave('boxplot.png', plot, width = 6, height = 4, dpi = 300)
在上面的代码中,我们使用 ggsave 函数将绘图结果保存为 'boxplot.png' 文件。你可以根据需要修改文件名、宽度、高度和dpi(每英寸点数)等参数。
通过以上修改,你可以将绘制好的图片导出为图像文件。请确保在导出之前已经安装并加载了相应的包。
原文地址: https://www.cveoy.top/t/topic/czPa 著作权归作者所有。请勿转载和采集!