绘制森林图(R)

#object: 绘制森林图
#writer: mike
#time: 2020.10.13


#构造数据集

names <- c("a","b","c","d","e","f","g","h","l","m");
or <- c(0.9,2,0.3,0.4,0.5,1.3,2.5,1.6,1.9,1.1);
lower <- c(0.75,1.79,0.18,0.2,0.38,1.15,2.41,1.41,1.66,0.97);
upper <- c(1.05,2.11,0.42,0.6,0.62,1.45,2.59,1.79,2.14,1.23);
sample <- c(450,420,390,400,470,390,400,388,480,460);
factors <- c("risk","risk","protec","protec","protec","risk","risk","risk","risk","risk");

#构造数据框
data <- data.frame(names,or,lower,upper,sample,factors);

#载入包
library("ggplot2");

#颜色无法设置,颜色的设置不需要使用foctor()函数,但是必须使用col 而不是colors或者colours,注意这里一旦设置了颜色,就会和下面的涂层重合不到一起
#不知道为什么会这样,好像是如果加上颜色之后,那么就要对后加的数据进行分类,并分配颜色。
p <- ggplot(data, aes(x=or, y=names));


#如果在point这里设置颜色,只会改变点的颜色,而上面会改变,与某一个数据有关的所有图像的颜色
p <- p + geom_point(size=3.6) + geom_errorbarh(aes(xmax=upper, xmin=lower, height=0.3)) + scale_x_continuous(limits=c(0.1,5), breaks=seq(0,5,0.5)) + geom_vline(aes(xintercept=1)) + xlab("oddRatio") + ylab("")

#在图形中加入标签
#x坐标
lx <- c(rep(2.87,11));
#y坐标
ly <- c(11,10,9,8,7,6,5,4,3,2,1);

#标签
llab <- c("p value","0.01","0.01","0.01","0.01","0.01","0.01","0.01","0.01","0.01","0.05");
#构建另一个数据框
data1 <- data.frame(lx,ly,llab);
p <- p + geom_text(data=data1, aes(x=lx,y=ly,label=llab))
p

 

绘制森林图(R)

 

上一篇:Date()日期函数浏览器兼容问题踩坑


下一篇:栈与队列