75
75
# ' @importFrom stats quantile
76
76
# ' @importFrom lifecycle badge
77
77
# ' @export
78
-
79
-
80
- dagp_plot = function (drawsDF ,densityPlot = FALSE , abbrevLabels = FALSE ) { # case where untidy posterior draws are provided
78
+ dagp_plot = function (drawsDF , densityPlot = FALSE , abbrevLabels = FALSE ) { # case where untidy posterior draws are provided
81
79
q95 <- density <- reasonableIntervalWidth <- credIQR <- shape <- param <- NULL # # place holder to pass devtools::check
82
80
83
81
if (densityPlot == TRUE ) {
84
82
if (abbrevLabels ) { # # shorten labels if desired
85
83
drawsDF = drawsDF %> %
86
84
tidyr :: gather() %> %
87
- dplyr :: mutate(key = abbreviate(key , minlength = 10 ))} else {
88
- drawsDF = drawsDF %> %
89
- tidyr :: gather()
90
- }
85
+ dplyr :: mutate(key = abbreviate(key , minlength = 10 ))
86
+ } else {
87
+ drawsDF = drawsDF %> %
88
+ tidyr :: gather()
89
+ }
91
90
plot = drawsDF %> % # # start with tidy draws
92
91
ggplot2 :: ggplot(ggplot2 :: aes(x = value ,
93
- y = ggplot2 :: after_stat(density ))) +
92
+ y = ggplot2 :: after_stat(density ))) +
94
93
ggplot2 :: geom_density(ggplot2 :: aes(fill = key )) +
95
- ggplot2 :: facet_wrap( ~ key , scales = " free_x" ) +
94
+ ggplot2 :: facet_wrap(~ key , scales = " free_x" ) +
96
95
ggplot2 :: theme_minimal() +
97
96
ggplot2 :: theme(legend.position = " none" )
98
97
@@ -104,48 +103,62 @@ dagp_plot = function(drawsDF,densityPlot = FALSE, abbrevLabels = FALSE) { # case
104
103
if (abbrevLabels ) { # # shorten labels if desired
105
104
drawsDF = drawsDF %> %
106
105
addPriorGroups() %> %
107
- dplyr :: mutate(param = abbreviate(param , minlength = 10 ))} else {
108
- drawsDF = drawsDF %> %
109
- addPriorGroups()
110
- }
106
+ dplyr :: mutate(param = abbreviate(param , minlength = 10 ))
107
+ } else {
108
+ drawsDF = drawsDF %> %
109
+ addPriorGroups()
110
+ }
111
111
drawsDF = drawsDF %> %
112
- dplyr :: mutate(priorGroup = ifelse(is.na(priorGroup ),999999 ,priorGroup )) %> %
113
- dplyr :: filter(! is.na(priorGroup )) # #if try works, erase this line
114
- priorGroups = unique(drawsDF $ priorGroup )
115
- numPriorGroups = length(priorGroups )
116
- for (i in 1 : numPriorGroups ) {
117
- df = drawsDF %> % dplyr :: filter(priorGroup == priorGroups [i ])
112
+ dplyr :: mutate(priorGroup = ifelse(is.na(priorGroup ), 999999 , priorGroup )) %> %
113
+ dplyr :: filter(! is.na(priorGroup )) # # if try works, erase this line if desired
118
114
119
- # create one plot per group
120
- # groups defined as params with same prior
121
- plotList [[i ]] = df %> % dplyr :: group_by(param ) %> %
122
- dplyr :: summarize(q05 = stats :: quantile(value ,0.05 ),
123
- q25 = stats :: quantile(value ,0.55 ),
124
- q45 = stats :: quantile(value ,0.45 ),
125
- q50 = stats :: quantile(value ,0.50 ),
126
- q55 = stats :: quantile(value ,0.55 ),
127
- q75 = stats :: quantile(value ,0.75 ),
128
- q95 = stats :: quantile(value ,0.95 )) %> %
129
- dplyr :: mutate(credIQR = q75 - q25 ) %> %
130
- dplyr :: mutate(reasonableIntervalWidth = 1.5 * stats :: quantile(credIQR ,0.75 )) %> %
131
- dplyr :: mutate(alphaLevel = ifelse(.data $ credIQR > .data $ reasonableIntervalWidth , 0.3 ,1 )) %> %
132
- dplyr :: arrange(alphaLevel ,.data $ q50 ) %> %
133
- dplyr :: mutate(param = factor (param , levels = param )) %> %
134
- ggplot2 :: ggplot(ggplot2 :: aes(y = param , yend = param )) +
135
- ggplot2 :: geom_segment(ggplot2 :: aes(x = q05 , xend = q95 , alpha = alphaLevel ), linewidth = 4 , color = " #5f9ea0" ) +
136
- ggplot2 :: geom_segment(ggplot2 :: aes(x = q45 , xend = q55 , alpha = alphaLevel ), linewidth = 4 , color = " #11114e" ) +
137
- ggplot2 :: scale_alpha_continuous(range = c(0.6 ,1 )) +
138
- ggplot2 :: guides(alpha = " none" ) +
139
- ggplot2 :: theme_minimal(12 ) +
140
- ggplot2 :: labs(y = ggplot2 :: element_blank(),
141
- x = " parameter value" ,
142
- caption = ifelse(i == numPriorGroups ," Credible Intervals - 10% (dark) & 90% (light)" ," " ))
115
+ priorGroups = unique(drawsDF $ priorGroup )
116
+ numPriorGroups = length(priorGroups )
117
+ for (i in 1 : numPriorGroups ) {
118
+ df = drawsDF %> % dplyr :: filter(priorGroup == priorGroups [i ])
143
119
144
- }
120
+ # create one plot per group
121
+ # groups defined as params with same prior
122
+ plotList [[i ]] = df %> %
123
+ dplyr :: group_by(param ) %> %
124
+ dplyr :: summarize(
125
+ q05 = stats :: quantile(value , 0.05 ),
126
+ q25 = stats :: quantile(value , 0.25 ), # <- fixed from 0.55
127
+ q45 = stats :: quantile(value , 0.45 ),
128
+ q50 = stats :: quantile(value , 0.50 ),
129
+ q55 = stats :: quantile(value , 0.55 ),
130
+ q75 = stats :: quantile(value , 0.75 ),
131
+ q95 = stats :: quantile(value , 0.95 ),
132
+ .groups = " drop"
133
+ ) %> %
134
+ dplyr :: mutate(credIQR = q75 - q25 ) %> %
135
+ dplyr :: mutate(reasonableIntervalWidth = 1.5 * stats :: quantile(credIQR , 0.75 )) %> %
136
+ dplyr :: mutate(alphaLevel = ifelse(.data $ credIQR > .data $ reasonableIntervalWidth , 0.3 , 1 )) %> %
137
+ dplyr :: arrange(alphaLevel , .data $ q50 ) %> %
138
+ dplyr :: mutate(param = factor (param , levels = param )) %> %
139
+ ggplot2 :: ggplot(ggplot2 :: aes(y = param , yend = param )) +
140
+ ggplot2 :: geom_segment(ggplot2 :: aes(x = q05 , xend = q95 , alpha = alphaLevel ),
141
+ linewidth = 4 , color = " #5f9ea0" ) +
142
+ ggplot2 :: geom_segment(ggplot2 :: aes(x = q45 , xend = q55 , alpha = alphaLevel ),
143
+ linewidth = 4 , color = " #11114e" ) +
144
+ ggplot2 :: scale_alpha_continuous(range = c(0.6 , 1 )) +
145
+ ggplot2 :: guides(alpha = " none" ) +
146
+ ggplot2 :: theme_minimal(12 ) +
147
+ ggplot2 :: labs(
148
+ y = NULL , # <- use NULL/string here, not element_blank()
149
+ x = " parameter value" ,
150
+ caption = ifelse(i == numPriorGroups ,
151
+ " Credible Intervals - 10% (dark) & 90% (light)" ,
152
+ " " )
153
+ ) +
154
+ ggplot2 :: theme(
155
+ axis.title.y = ggplot2 :: element_blank() # hides the Y title cleanly
156
+ )
157
+ }
145
158
146
- nCol <- ifelse(numPriorGroups == 1 , 1 , floor(1 + sqrt(numPriorGroups )))
147
- cowplot :: plot_grid(plotlist = plotList , ncol = nCol )
159
+ nCol <- ifelse(numPriorGroups == 1 , 1 , floor(1 + sqrt(numPriorGroups )))
160
+ cowplot :: plot_grid(plotlist = plotList , ncol = nCol )
148
161
},
149
- error = function (c ) dagp_plot(drawsDF , densityPlot = T )) # end try
162
+ error = function (c ) dagp_plot(drawsDF , densityPlot = TRUE )) # end try
150
163
} # end else
151
164
} # end function
0 commit comments