关于Meta分析的文章已经有很多,但是在出图时候遇到中文不显示,这里主要解决,如何保存图片里面显示中文的问题。
meta分析流程这里不做主要介绍。
进入主题:
meta森林图数据
这使用meta
案例数据Olkin1995
,计算病例与对照组的RR效应值。
library(meta)
data(Olkin1995)
head(Olkin1995)
这里是数据的展示各个研究的总人数及结局人数。meta森林图
m2 <- metabin(ev.exp, n.exp, ev.cont, n.cont,
data = Olkin1995, subset = year < 1970, studlab = paste0(author," ",year),
method = "Inverse")
m2
forest(m2)
summary(m2)
# save
pdf( "Meta-forest-English.pdf",width = 10,height = 6)
forest(m2,comb.random=T)
dev.off()
这里我们使用pdf()
进行保存
meta中文保存
这里我们将四个研究更改为中文名字,然后plot显示,发现显示的是空白。
# change to Chinese
df= Olkin1995 %>% filter(year < 1970) %>%
mutate(author=c("宁王","隆王","兔子","华熙"))
df
m2 <- metabin(ev.exp, n.exp, ev.cont, n.cont,
data = df, studlab = paste0(author," ",year),
method = "Inverse")
m2
forest(m2)
# save
pdf( "Meta-forest-CHN.pdf",width = 10,height = 6)
forest(m2,comb.random=T)
dev.off()
出错啦,出错啦,不显示中文。然后用pdf()
进行保存,也不显示中文
Plot中文保存
Stacfamily = 'STHeitiSC-Light'
或者 family="Arial Unicode MS"
即可显示中文,然后我们保存
par(mfrow=c(1,2))
mydata = matrix( c( 2:6, c( 2,4,2,6,3 ) ), nrow= 2 )
mylabs = c( "新吉能", "天赐", "华里", "智飞", "天岳" )
barplot( mydata, beside=T, horiz= "T",
names.arg= mylabs, las= 1,
col= c( "red", "blue" ))
mydata = matrix( c( 2:6, c( 2,4,2,6,3 ) ), nrow= 2 )
mylabs = c( "新吉能", "天赐", "华里", "智飞", "天岳" )
barplot( mydata, beside=T, horiz= "T",
names.arg= mylabs, las= 1,
col= c( "red", "blue" ),family="Arial Unicode MS")
但是,pdf保存就出错,stackOverflow [1] 这里提示用 family="GB1"
即可解决问题
## save
pdf( "plotname1.pdf" )
barplot( mydata, beside=T, horiz= "T",
names.arg= mylabs, las= 1,
col= c( "red", "blue" ),family="GB1")
dev.off()
## save
pdf( "plotname2.pdf",family="GB1")
barplot( mydata, beside=T, horiz= "T",
names.arg= mylabs, las= 1,
col= c( "red", "blue" ))
dev.off()
Plot中文保存森林图
这里给了两种方式,基本上都是利用family = 'GB1'
,但是用 library(showtext) [2]也可以用family = 'STHeitiSC-Light'
或者 family="Arial Unicode MS"
## save
pdf( "Meta-forest-CHN.pdf",family="GB1",height = 6,width = 12)
forest(m2)
dev.off()
## save
library(showtext)
cairo_pdf( "plotname1.pdf",width = 12,height = 6)
showtext.begin() ## turn on showtext
forest(m2,comb.random=T,
family = 'GB1' )
showtext.end() ## turn off showtext
dev.off()
## save
library(showtext)
cairo_pdf( "plotname2.pdf",width = 12,height = 6)
showtext.begin() ## turn on showtext
forest(m2,comb.random=T,
family = 'STHeitiSC-Light' )
showtext.end() ## turn off showtext
dev.off()
## save
library(showtext)
cairo_pdf( "plotname3.pdf",width = 12,height = 6)
showtext.begin() ## turn on showtext
forest(m2,comb.random=T,
family = 'Arial Unicode MS' )
showtext.end() ## turn off showtext
dev.off()
自己去试试吧。
参考资料
[1]
stackOverflow: https://stackoverflow.com/questions/12948701/how-to-plot-chinese-characters-on-pdf [2]
showtext: https://statr.me/2014/01/using-system-fonts-in-r-graphs/