在主题里设置字体,具体可用字体可搜索
dat<- read_excel('dat.xlsx') %>%
mutate(BMI = 体重/(身高*0.01)^2,
组别 = ifelse(组别==0,'实验组','对照组')) %>%
dplyr::select(c(1,29:34))
df <- dat[,c(1,6:7)]
df <- pivot_longer(df, cols = c(2:3), names_to = "condition", values_to = "value")
df$condition <- factor(df$condition)
df$组别 <- factor(df$组别)
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
#data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
df2 <- data_summary(df, varname="value",
groupnames=c("组别", "condition"))
names(df2)[c(2,3)] <- c('阶段',"value")
df2$阶段 <- ifelse(df2$阶段=='抑郁','治疗前','治疗后')
ggplot(df2, aes(x=组别, y=value, fill=阶段)) +
geom_bar(stat="identity", color="black",
position=position_dodge()) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd), width=.2,
position=position_dodge(.9)) +
theme_minimal(base_family = "Times") +
theme(panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_rect(fill = NA),
plot.background = element_rect(fill = NA,colour = NA),
plot.title = element_text(hjust = 0.5),
axis.text.y = element_text(face = "bold")) +
labs(x = NULL, y = '均值',title = "两组抑郁评分变化")+
scale_fill_manual(values=c('#999999','#CCCCCC'))library(tidyr)
# 创建一个宽格式数据框
wide_data <- data.frame(
id = 1:3,
var1_1 = c(1, 2, 3),
var1_2 = c(4, 5, 6),
var2_1 = c(7, 8, 9),
var2_2 = c(10, 11, 12)
)
# 使用pivot_longer函数将数据转换为长格式
long_data <- pivot_longer(wide_data, cols = starts_with("var"),
names_to = "variable", values_to = "value")
# 查看转换后的长格式数据
print(long_data)可参考: <(3 封私信 / 44 条消息) R语言用怎么实现,可以让别人用一个函数,但是不能看底码? - 知乎>
<source code protection - Encrypting R script under MS-Windows - Stack Overflow>
---
title: "R 语言中常见 bug 处理收集"
author: "Congyuan He (hcy)"
date: "`r Sys.Date()`"
# 全局设置公式靠左 (针对 PDF)
classoption: fleqn
output:
html_document:
toc: true
toc_float:
collapsed: false # 侧边栏默认展开
number_sections: true
theme: flatly
highlight: tango # 代码高亮风格
code_folding: show # 默认显示代码,但允许点击折叠
self_contained: true # 方便单个文件传阅
---tiff( paste0('./outcome/cor_',save_name,'.tiff'), width = 3600, height = 3600, res = 300)
corrplot(cor_matrix,
method = "color",
type = "upper",
tl.cex = 0.9,
tl.col = "black",
tl.srt = 45,
addCoef.col = "black",
number.cex = 0.8,
col = colorRampPalette(c("#377EB8", "white", "#B22222"))(100),
mar = c(0, 0, 3, 0) )
dev.off()