当然,我可以为您提供优化后的箱线图代码,使其更美观并适用于学术论文。

library(ggplot2)

# 设定颜色和字体
colors <- c('#999999', '#E69F00')
font <- 'Arial'

# 生成箱线图
p <- ggplot(comparison_hp, aes(x = group, y = ISQ_new)) +
  geom_boxplot(width = 0.5, fill = colors[1], color = colors[2], outlier.shape = NA) +
  theme_bw(base_family = font) +
  labs(x = 'Group', y = 'ISQ Score') +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(color = 'black'),
        axis.text = element_text(size = 12, color = colors[2]),
        axis.title = element_text(size = 14, color = colors[2], face = 'bold'),
        legend.title = element_blank(),
        legend.text = element_text(size = 12, color = colors[2]),
        legend.position = 'none',
        plot.title = element_text(size = 16, color = colors[2], face = 'bold'))

# 添加中位数和置信区间标记
p <- p + geom_text(aes(label = paste0('Median: ', round(median(ISQ_new[group == 'Healthy']), 2))),
                   x = 1, y = 0.9, color = colors[2], hjust = 0, size = 4, fontface = 'bold') +
  geom_text(aes(label = paste0('Median: ', round(median(ISQ_new[group == 'Patient']), 2))),
            x = 2, y = 0.9, color = colors[2], hjust = 1, size = 4, fontface = 'bold') +
  geom_text(aes(label = paste0('95% CI: [', 
                               round(quantile(ISQ_new[group == 'Healthy'], probs = 0.025), 2), 
                               ', ',
                               round(quantile(ISQ_new[group == 'Healthy'], probs = 0.975), 2), 
                               ']'))),
            x = 1, y = 0.85, color = colors[2], hjust = 0, size = 4) +
  geom_text(aes(label = paste0('95% CI: [', 
                               round(quantile(ISQ_new[group == 'Patient'], probs = 0.025), 2), 
                               ', ',
                               round(quantile(ISQ_new[group == 'Patient'], probs = 0.975), 2), 
                               ']'))),
            x = 2, y = 0.85, color = colors[2], hjust = 1, size = 4)

# 添加图形标题
p <- p + ggtitle('Comparison of ISQ Score between Healthy and Patient Groups') +
  theme(plot.title = element_text(hjust = 0.5))

# 显示图形
print(p)

上述代码中,我们设置了一些参数,例如颜色、字体和标题。我们还使用geom_text()函数添加了中位数和置信区间的标记,并使用theme()函数调整了图形的外观。

请根据需要调整参数和样式,以满足您的特定需求。此外,您还可以根据需要添加其他元素,例如图例、标签和注释。

希望这个优化后的代码能够满足您的要求。如果您有其他问题,请随时提问。


原文地址: https://www.cveoy.top/t/topic/cf8V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录