我正在生成一个森林图来显示三个简单线性回归模型的结果,其中自变量是四个级别的分类。我想报告每个方面的整体p值和p-for-trend。
数据:
quantile = c('Qtile 1','Qtile 1','Qtile 1','Qtile 2','Qtile 2','Qtile 2','Qtile 3','Qtile 3','Qtile 3','Qtile 4','Qtile 4','Qtile 4')<br/>
estimate = c(1,1,1,1,2,3,2,4,5,3,4,5)
low95 = c(NA, NA, NA, .5, 1.5, 2.5, 1.5, 3.5, 4.5, 2.5, 3.5, 4.5)
high95 = c(NA, NA, NA, 1.5, 2.5, 3.5, 2.5, 4.5, 5.5, 3.5, 4.5, 5.5)
povarall = c(0.05, 0.06, 0.08, NA,NA,NA,NA,NA,NA,NA,NA,NA)
ptrend = c(0.04, 0.03, 0.06, NA,NA,NA,NA,NA,NA,NA,NA,NA)
dv = c('dv1', 'dv2', 'dv3','dv1', 'dv2', 'dv3','dv1', 'dv2', 'dv3','dv1', 'dv2', 'dv3')
df = data.frame(quantile, estimate, low95, high95, povarall, ptrend, dv)
我尝试使用文本geom并指定打印p值的确切位置,但未成功。
此代码:
ggplot(data=df,aes(x=estimate,y=quantile))+
geom_point()+
geom_errorbarh(aes(xmin=low95,xmax=high95),height=0.0, colour="blue")+
facet_grid(dv ~ .)+
geom_text(x=5, y=1,label = paste("p =",df$povarall) , parse = F)
产生以下错误:Error: Incompatible lengths for set aesthetics: x, y, label
我想要的情节在这里(使用Microsoft Paint添加p值):
请告诉我如何调整代码以获取上面链接的图片。如果我不必指定文本geom的确切位置,应该总是在每个方面的右下角绘制,这样会更好。
答案 0 :(得分:1)
一种方式:
df2 <- data.frame(p = paste('p =', povarall[1:3]), dv = c('dv1', 'dv2', 'dv3'))
ggplot(data = df, aes(x = estimate, y = quantile)) +
geom_point() +
geom_errorbarh(aes(xmin = low95, xmax = high95), height=0.0, colour = "blue") +
facet_grid(dv ~ .) +
geom_text(data = df2, aes(label = p), x = 5, y = 1, parse = F)