關(guān)于Meta分析的文章已經(jīng)有很多,但是在出圖時候遇到中文不顯示,這里主要解決,如何保存圖片里面顯示中文的問題。
meta分析流程這里不做主要介紹。
進(jìn)入主題:
meta森林圖數(shù)據(jù)
這使用meta案例數(shù)據(jù)Olkin1995,計算病例與對照組的RR效應(yīng)值。
library(meta)
data(Olkin1995)
head(Olkin1995)
這里是數(shù)據(jù)的展示各個研究的總?cè)藬?shù)及結(jié)局人數(shù)。image
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()進(jìn)行保存
image
meta中文保存
這里我們將四個研究更改為中文名字,然后plot顯示,發(fā)現(xiàn)顯示的是空白。
# 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()進(jìn)行保存,也不顯示中文
image
image
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")
image

image
但是,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()
自己去試試吧。
image
參考資料
[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/