绘制包含分类变量的优势比,其上限和下限为95%CI

时间:2015-08-27 01:16:01

标签: r plot ggplot2

我正在处理有关农场管理实践与疾病预防相关效果的数据。

有关如何在R中创建绘图的任何帮助,显示变量的优势比为95%。自变量的代码是0 =没有在农场实践,1 =实践。 Orf.no.Orf.2012.2014是因变量,0 =没有疾病,1 =疾病存在。提前致谢

这是数据样本加上一些输出:

Vet.Advice  Quarantine  Purchase.History Orf.no.Orf.2012.2014   
1           1           2                1  
0           0           0                1  
1           0           0                1  
0           0           0                1  
0           0           1                0  
1           0           0                1  
0           0           0                1  
0           1           0                1  
1           1           0                1  
0           0           1                1  
0           1           2                1  
0           0           0                1  


                             OR      2.5 %     97.5 %
(Intercept)               3.8251348 1.37283503 12.4437518
Footbath1                 1.7367032 0.91826113  3.3476932
Handwashing1              0.7927673 0.27360243  2.0484978
Handgels1                 1.4887936 0.58547658  4.3287735
Clean.Needles1            0.4719316 0.23958494  0.9120537
Vet.Vacc.Advice1          0.8666398 0.42396797  1.8096052
Disinfect.Trough1         0.7122465 0.36323418  1.3935948
Quarantine1               1.0648841 0.52899720  2.1936099
Quarantine2               0.2801078 0.06141656  1.1778524
Purchase.Disease.History1 0.5648420 0.26631744  1.1974933
Purchase.Disease.History2 0.4856445 0.13433826  1.7068997

1 个答案:

答案 0 :(得分:1)

你走了。

library(ggplot2)

#reading in data. You should be able
#to get this from your model by doing something like
#model_data <- exp(cbind(coef(model),confint(model)))
model_data <- read.table(text="
                                                     OR      2.5%     97.5%
(Intercept)               3.8251348 1.37283503 12.4437518
                         Footbath1                 1.7367032 0.91826113  3.3476932
                         Handwashing1              0.7927673 0.27360243  2.0484978
                         Handgels1                 1.4887936 0.58547658  4.3287735
                         Clean.Needles1            0.4719316 0.23958494  0.9120537
                         Vet.Vacc.Advice1          0.8666398 0.42396797  1.8096052
                         Disinfect.Trough1         0.7122465 0.36323418  1.3935948
                         Quarantine1               1.0648841 0.52899720  2.1936099
                         Quarantine2               0.2801078 0.06141656  1.1778524
                         Purchase.Disease.History1 0.5648420 0.26631744  1.1974933
                         Purchase.Disease.History2 0.4856445 0.13433826  1.7068997",
                         header=T)



#do some rearranging/renaming
colnames(model_data) <- c("OR","lower","upper")
model_data$variable <- rownames(model_data)

#plot without intercept (generally not reported)
p1 <- ggplot(model_data[-1,], aes(y=variable)) +
  geom_point(aes(x=OR)) + #plot OR
  geom_segment(aes(x=lower,xend=upper,yend=variable)) #plot confint

#add line for OR = 1
p1 <- p1 + geom_vline(xintercept=1) + theme_bw()
p1

enter image description here