如何修复图中随机显示的变量

时间:2019-07-09 15:54:48

标签: r ggplot2

我尝试构建5个不同时间段内21个变量的置信区间图。

以下内容包括数据代码和置信区间图代码。

w<-c("w1","w2","w3","w4","w5","w6","w7","w8","w9","w10","w11","w12",
     "w13","w14","w15","w16","w17","w18","w19","w20","w21","w1","w2","w3","w4","w5","w6","w7","w8","w9","w10","w11","w12",
     "w13","w14","w15","w16","w17","w18","w19","w20","w21","w1","w2","w3","w4","w5","w6","w7","w8","w9","w10","w11","w12",
     "w13","w14","w15","w16","w17","w18","w19","w20","w21","w1","w2","w3","w4","w5","w6","w7","w8","w9","w10","w11","w12",
     "w13","w14","w15","w16","w17","w18","w19","w20","w21","w1","w2","w3","w4","w5","w6","w7","w8","w9","w10","w11","w12",
     "w13","w14","w15","w16","w17","w18","w19","w20","w21")

Period<-c(rep(0,21),rep(1,21),rep(2,21),rep(3,21),rep(4,21))

means<-c(0.02,0.02,0.03,0.03,0.03,0.06,0.05,0.05,0.02,0.02,0.08,
         0.03,0.04,0.11,0.12,0.02,0.05,0.02,0.07,0.05,0.07,0.03,
         0.09,0.06,0.03,0.03,0.02,0.03,0.03,0.02,0.02,0.07,0.03,0.03
         ,0.04,0.07,0.04,0.03,0.02,0.06,0.19,0.07,0.02,0.04,0.06,0.06
         ,0.06,0.03,0.04,0.04,0.03,0.06,0.04,0.03,0.05,0.05,0.03,0.18,
         0.03,0.04,0.03,0.04,0.04,0.05,0.04,0.10,0.04,0.04,0.05,0.05
         ,0.03,0.02,0.02,0.04,0.04,0.09,0.02,0.03,0.04,0.13,0.04,
         0.05,0.04,0.04,0.07,0.03,0.03,0.05,0.03,0.07,0.05,0.03,0.08,
         0.05,0.04,0.04,0.07,0.05,0.03,0.03,0.01,0.03,0.03,0.06,0.12)

down<-c(rep(0.00,5),0.01,rep(0.00,7),0.01,0.01,0.00,0.00,0.00,0.01
        ,0.00
        ,0.01,
        rep(0.00,19),0.06,0.01
        ,rep(0.00,10),0.01,0.00,0.00,0.00,0.00,0.05,rep(0.00,5),
        0.00,0.00,0.01,rep(0.00,9),0.01,rep(0.00,3),0.02,rep(0.00,4)
        ,rep(0.00,20),0.03)


up<-c(0.06,0.07,0.10,0.10,0.09,0.14,0.14,0.13,0.07,0.07,0.20,0.11,0.11,
      0.22,0.23,0.07,0.13,0.08,0.16,0.13,0.15,0.10,0.22,0.17,0.08,0.09,
      0.08,0.09,0.11,0.07,0.07,0.19,0.09,0.10,0.13,0.19,0.12,0.09,
      0.08,0.14,0.31,0.16,0.07,0.11,0.16,0.17,0.16,0.09,0.13,0.11,0.10,
     0.14,0.12,0.11,0.14,0.15,0.09,0.32,0.09,0.12,0.10,0.12,0.12,
     0.17,0.13,0.22,0.13,0.12,0.13,0.13,0.08,0.08,0.05,0.13,0.13,
     0.20,0.08,0.10,0.11,0.24,0.13,0.13,0.13,0.11,0.19,0.11,0.10,
     0.14,0.11,0.17,0.15,0.11,0.18,0.12,0.14,0.13,0.19,0.14,0.10,
     0.09,0.05,0.10,0.09,0.16,0.21)

prits<-as.data.frame(cbind(w,means,down,up,Period))

p = ggplot(data=prits,
           aes(x = Period,y = means, ymin = down, ymax = up))+
  geom_pointrange(aes(col=Period))+

  xlab('Period')+ ylab("95% Confidence Interval")+
  geom_errorbar(aes(ymin=down, ymax=up,col=Period),width=0.5,cex=1)+ 
  facet_wrap(~w,strip.position="left",nrow=9,scales = "free_y") +
  theme(plot.title=element_text(size=16,face="bold"),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x=element_text(face="bold"),
        axis.title=element_text(size=12,face="bold"),
        strip.text.y = element_text(hjust=0,vjust = 1,angle=180,face="bold"))+
  coord_flip()


p

当我运行前面提到的代码时,我希望可以绘制一个7行3列的图。我的问题是21个变量W1,...,W21在图中随机显示。例如,我希望第一列具有以下顺序W1,W2,...,W7实例我有W1,W12 ,W15,W18,W20,W4,W7。有没有一种方法可以通过按列或行(名称)的顺序来解决此问题。我也想问一下,如何将绘图的间隔更改为3个特定值(0,0.5,1),因为否则许多值会崩溃彼此。

0 个答案:

没有答案