我正在嘗試繪制一個包含實際值的優勢比圖,四舍五入到小數點后第二位。我確實嘗試了其他執行緒的不同解決方案,包括格式和 sprintf 函式。不幸的是,它們不適用于我的代碼,可能是由于我的 ggplot 命令的結構。我真的很感激有關如何解決問題的提示。這是我正在使用的示例和代碼:
or<-as.numeric(c(1.909783, 1.536364, 2.787936, 1.898587, 1.216597, 1.350239))
lwr.ci<-as.numeric(c(1.4197895,1.1336468, 1.9416681, 1.0845309, 0.8674560, 0.6554979))
upr.ci<-as.numeric(c(2.568880, 2.082142, 4.003045, 3.323680, 1.706263, 2.781314))
n<-c(419,260,153,53,182, 31)
names<-as.factor(c("a","b","c","d","e","f"))
plab<-c("***","**","***","*","","")
ex<-as.data.frame(cbind(or,lwr.ci,upr.ci,n,names,plab))
ex$or<-as.numeric(ex$or)
ex$lwr.ci<-as.numeric(ex$lwr.ci)
ex$upr.ci<-as.numeric(ex$upr.ci)
p1 <- ggplot(ex, aes(or, fct_rev(names)))
theme_bw(base_family = "Times New Roman",base_size =24)
p1<-p1 geom_vline(aes(xintercept = 1), size = .5, linetype = 'dashed')
geom_errorbarh(aes(xmax =ifelse(upr.ci<13,upr.ci,13), xmin = lwr.ci), size = 2, height = 0, color = 'gray')
geom_point(size = 4, color = 'black')
theme(panel.grid = element_blank())
scale_x_continuous(breaks = c(0:13),limits = c(-3,13))
ylab('CL')
xlab('Odds Ratios')
ggtitle('title')
geom_text(parse=T,aes(label=paste(sprintf("%.2f",round(or,2)),sep ="","(","italic(n)==",n,")")),x= -1.5, size = 8,color='black',family="Times New Roman")
geom_text(aes(label=plab),vjust=-0.5,fontface='bold', size=8)
p1
提前致謝。
uj5u.com熱心網友回復:
%.2f決議運算式時,在字串中加上單引號sprintf以顯示固定位數:
p1
geom_vline(aes(xintercept = 1), size = .5, linetype = 'dashed')
geom_errorbarh(aes(xmax = ifelse(upr.ci < 13, upr.ci, 13), xmin = lwr.ci),
size = 2, height = 0, color = 'gray')
geom_point(size = 4, color = 'black')
theme(panel.grid = element_blank())
scale_x_continuous(breaks = c(0:13), limits = c(-3,13))
ylab('CL')
xlab('Odds Ratios')
ggtitle('title')
geom_text(aes(label = paste(sprintf("'%.2f'", or), " (", "italic(n)==", n ,")")),
x= -1.5, size = 8, color = 'black', family = "Times New Roman",
parse = TRUE)
geom_text(aes(label = plab), vjust = -0.5, fontface = 'bold', size = 8)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444188.html
