Diff of /R/fromParameterPlot.R [000000] .. [f2e496]

Switch to unified view

a b/R/fromParameterPlot.R
1
# The plotting functions for the AnalysisResults object
2
# (predict from parameters)
3
4
##' @import ggplot2
5
6
#' @include study.R results.R eventPrediction_package.R
7
NULL
8
9
10
11
##' @param text Text to display be in title, e.g. output
12
##' from the getSummaryText() function.
13
##' @param options Use this to customize the output.
14
##' @param show.separate.arms Logical, if TRUE (and if x is a two arm study) the expected event
15
##' curves for the separate arms are displayed on the graph, otherwise do not show the curves.
16
##' @rdname plot-methods
17
##' @name plot
18
##' @aliases plot,AnalysisResults,missing-method
19
##' @export
20
setMethod( "plot", 
21
  signature( x = "AnalysisResults",y="missing" ),
22
  function(x,text=getFromParameterText(x, options = options), 
23
        options = DisplayOptions(text.width=110), show.title=TRUE,show.separate.arms=!isSingleArm(x@study),
24
             ylim=NULL) {
25
26
    ####### draw plot ######
27
    
28
    daysinyear <- standarddaysinyear()
29
    
30
    study <- x@study
31
    if(isSingleArm(study)) show.separate.arms <- FALSE
32
    
33
    ts <- x@grid$time
34
    recruit.tot <- x@grid$recruit.tot
35
    events1 <- x@grid$events1
36
    events2 <- x@grid$events2
37
    events.tot <- x@grid$events.tot
38
    
39
    N <- study@N 
40
    Y <- study@study.duration
41
    
42
    oldmar <- par()$mar
43
    oldlas <- par()$las
44
    if(show.title){
45
      chr.pos <- which(unlist(strsplit(text,NULL)) == '\n') 
46
      chr.count <- length(chr.pos) 
47
      mar_val <- 1+chr.count
48
      
49
    }
50
    else{
51
      mar_val <- 0.4
52
    }
53
       
54
    par( mar= c(5,4,mar_val,2)+0.1, las=1 )
55
    
56
    plot(range(ts),range(x@grid$recruit.tot),type='n',xlab=" ",ylab=" ", axes=F, ylim=ylim)
57
    
58
    #Y axes
59
    if(is.null(ylim)){ 
60
      ylim <- c(0,N)
61
    }
62
    else{
63
      N <- ylim[2]-ylim[1]
64
    }  
65
        
66
    rounding <- -floor(log(N,10)-0.3)
67
    
68
    startpoint <- round(ylim[1],rounding)
69
    endpoint <- ylim[2]
70
    
71
    axis(side=2,at=(seq(startpoint,endpoint,10^(-rounding))), adj=1, cex.axis=1.00)
72
    
73
    mtext("N",side=2,at=((ylim[2]-ylim[1])/2),line=3) 
74
    
75
    #X Axis 
76
    if(options@StartDate=="0") {
77
      #Numbers
78
      axis(side=1,at=(0:Y), adj=1, cex.axis=0.85)
79
    }
80
    else{
81
      #Dates  
82
      startd <- as.Date( options@StartDate, format="%d/%m/%Y")
83
      date2  <- as.Date(startd+(0:ceiling(ts[length(ts)]))*daysinyear/12, format="%d/%m/%Y")
84
      date  <- format(date2, format="%b %Y")
85
              
86
      every <- floor(length(date)/50)
87
      ats <- seq(0,length(date)-1,every+1)
88
      date <- date[c(TRUE,rep(FALSE,every))]
89
            
90
      axis(side=1,at=ats, labels=as.character(date),adj=1,cex.axis=0.85, las=2)
91
    }
92
    
93
    
94
    #X Axes Labels
95
    if( options@StartDate=="0" ) {
96
      mtext('Time (Months)',side=1,at=(Y/2),line=2)
97
    } else{
98
      #Dates - not outputting  
99
      #mtext('Time',side=1,at=-1.5,line=2)
100
    }
101
    
102
    #Add Lines
103
    box()
104
    lines(ts,recruit.tot,lty=1,col="black",lwd=2)
105
    if(show.separate.arms ){  
106
      lines(ts,events2,lty=8,col="red",lwd=2)
107
      lines(ts,events1,lty=8,col="blue",lwd=2)
108
    }
109
    lines(ts,events.tot,lty=8,col="black",lwd=2)
110
    if( nrow(x@critical.data)>0 ) abline( v = x@critical.data$time )
111
    
112
    if( nrow(x@predict.data) > 0  )  {
113
      abline(v = x@predict.data$time, lty = 2)
114
      if(show.separate.arms){ 
115
        abline(h = x@predict.data$events2,lty=2,col="red")
116
        abline(h = x@predict.data$events1,lty=2, col="blue")
117
      }
118
    }
119
    
120
    if(show.separate.arms){ 
121
      legend( "topleft", c("Recruitment", "Events : Total", "Events : Control", "Events : Experimental" ),
122
              col=c( "black", "black", "blue", "red" ),
123
              lty=c(1,8,8,8),text.col=c("black","black","blue","red"), lwd=c(2,2,2,2), bty="n")
124
    }
125
    else{
126
      legend( "topleft", c("Recruitment", "Events : Total" ),
127
              col=c( "black", "black" ),
128
              lty=c(1,8),text.col=c("black","black"), lwd=c(2,2), bty="n")  
129
    }
130
    
131
    
132
    ####### Add in Summary Info and Results ######
133
    if( show.title==TRUE ) {
134
      mtext(text,side=3,cex=1,adj=0)
135
    } 
136
    par( mar=oldmar, las=oldlas )
137
    
138
})
139