Skip to content

Commit c0223c2

Browse files
committed
slimmed down colouring to 4 options
1 parent 05c177a commit c0223c2

6 files changed

Lines changed: 29 additions & 25 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Imports:
2424
ggplot2,
2525
scales,
2626
rlang
27-
RoxygenNote: 7.3.1
27+
RoxygenNote: 7.3.2
2828
Suggests:
2929
testthat (>= 3.0.0),
3030
knitr,

R/draw_plot.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ draw_plot<-function(mod_plot_agg, limits, x_label, y_label, title, label, multip
8686
geom_line(aes(x = number.seq, y = ul95, col = "95%", linetype = "95%"), linewidth = 1, data = limits, na.rm = TRUE) +
8787
geom_line(aes(x = number.seq, y = ll998, col = "99.8%", linetype = "99.8%"), linewidth = 1, data = limits, na.rm = TRUE) +
8888
geom_line(aes(x = number.seq, y = ul998, col = "99.8%", linetype = "99.8%"), linewidth = 1, data = limits, na.rm = TRUE) +
89-
scale_color_manual(values = plot_cols[1:4], name = "Control limits")+
89+
scale_color_manual(values = plot_cols, name = "Control limits")+
9090
scale_linetype_manual(values = c("95%"=2
9191
, "99.8%" = 1), guide = "none")+
9292
guides(colour = guide_legend(title.theme = element_text(
@@ -104,7 +104,7 @@ draw_plot<-function(mod_plot_agg, limits, x_label, y_label, title, label, multip
104104
geom_line(aes(x = number.seq, y = odul95, col = "95% Overdispersed", linetype = "95% Overdispersed"), linewidth = 1, data = limits, na.rm = TRUE) +
105105
geom_line(aes(x = number.seq, y = odll998, col = "99.8% Overdispersed", linetype = "99.8% Overdispersed"), linewidth = 1, data = limits, na.rm = TRUE) +
106106
geom_line(aes(x = number.seq, y = odul998, col = "99.8% Overdispersed", linetype = "99.8% Overdispersed"), linewidth = 1, data = limits, na.rm = TRUE) +
107-
scale_color_manual(values = plot_cols[5:8], name = "Control limits")+
107+
scale_color_manual(values = plot_cols, name = "Control limits")+
108108
scale_linetype_manual(values = c("95% Overdispersed"=2
109109
, "99.8% Overdispersed" = 1), guide = "none")+
110110
guides(colour = guide_legend(title.theme = element_text(
@@ -115,7 +115,7 @@ draw_plot<-function(mod_plot_agg, limits, x_label, y_label, title, label, multip
115115
), override.aes = list(linetype = c(2,1)) ))
116116
}
117117
if (draw_unadjusted == FALSE & draw_adjusted == FALSE){
118-
funnel_p
118+
funnel_p
119119
}
120120
}
121121

@@ -132,25 +132,25 @@ draw_plot<-function(mod_plot_agg, limits, x_label, y_label, title, label, multip
132132

133133

134134

135-
135+
136136

137137
# Label points
138138
if(!is.na(label)){
139-
139+
140140
if(label=="highlight"){
141141
funnel_p <- funnel_p +
142142
geom_label_repel(aes(label = ifelse(highlight == 1,
143143
as.character(group), NA))
144144
, size=2.5, point.padding=0, direction = "both", force = 2
145145
, min.segment.length=0, na.rm=TRUE, max.overlaps = max.overlaps)
146146
}
147-
147+
148148
if(draw_adjusted == FALSE & draw_unadjusted == FALSE){
149149
funnel_p
150150
}
151-
152-
153-
151+
152+
153+
154154
if(label=="outlier"){
155155

156156
funnel_p <- funnel_p +
@@ -178,7 +178,7 @@ draw_plot<-function(mod_plot_agg, limits, x_label, y_label, title, label, multip
178178
, min.segment.length=1, na.rm=TRUE, max.overlaps = max.overlaps)
179179
}
180180

181-
181+
182182
if(label=="both"){
183183
funnel_p <- funnel_p +
184184
geom_label_repel(aes(label = ifelse((highlight == 1 | outlier == 1) ,

R/funnel_plot.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
#' @param yrange Deprecated. Please use the `y_range` argument instead.
4545
#' @param y_range Manually specify the y-axis min and max, in form c(min, max), e.g. c(0.7, 1.3). Default, "auto", allows function to estimate range.
4646
#' @param theme a ggplot theme function. This can be a canned theme such as theme_bw(), a theme() with arguments, or your own custom theme function. Default is new funnel_clean(), but funnel_classic() is original format.
47-
#' @param plot_cols A vector of 8 colours for funnel limits, in order: 95\% Poisson (lower/upper), 99.8\% Poisson (lower/upper), 95\% OD-adjusted (lower/upper), 99.8\% OD-adjusted (lower/upper).
47+
#' @param plot_cols A vector of 4 colours for funnel limits, in order: 95\% Poisson (lower/upper), 99.8\% Poisson (lower/upper), 95\% OD-adjusted (lower/upper), 99.8\% OD-adjusted (lower/upper).
4848
#' Default has been chosen to avoid red and green which can lead to subconscious value judgements of good or bad.
49-
#' Default is hex colours: c("#FF7F0EFF", "#FF7F0EFF", "#1F77B4FF","#1F77B4FF", "#9467BDFF", "#9467BDFF", "#2CA02CFF", "#2CA02CFF")
49+
#' Default is hex colours: c("#FF7F0EFF", "#1F77B4FF", "#9467BDFF", "#2CA02CFF")
5050
#' @param SHMI_rounding TRUE/FALSE, for SHMI calculation (standardised ratio, with SHMI truncation etc.), should you round the expected values to 2 decimal places (TRUE) or not (FALSE)
5151
#' @param max.overlaps Exclude text labels that overlap too many things. Defaults to 10. (inherited from geom_label_repel)
5252
#'
@@ -122,8 +122,7 @@ funnel_plot <- function(.data, numerator, denominator, group
122122
, multiplier = 1, x_label = "Expected"
123123
, y_label , x_range = "auto", y_range = "auto"
124124
, plot_cols =
125-
c("#FF7F0EFF", "#FF7F0EFF", "#1F77B4FF","#1F77B4FF",
126-
"#9467BDFF", "#9467BDFF", "#2CA02CFF", "#2CA02CFF")
125+
c("#FF7F0EFF", "#1F77B4FF", "#9467BDFF", "#2CA02CFF")
127126
, theme = funnel_clean()
128127
, label_outliers, Poisson_limits, OD_adjust
129128
, xrange, yrange
@@ -193,10 +192,9 @@ funnel_plot <- function(.data, numerator, denominator, group
193192

194193

195194

196-
if(length(plot_cols) < 8){
197-
stop("Please supply a vector of 8 colours for funnel limits, in order: 95% Lower Poisson, 95% Upper Poisson
198-
, 99.8% Lower Poisson, 99.8% Upper Poisson, 95% Upper OD-adjusted, 95% Lower OD-adjusted,
199-
99.8% Lower OD-adjusted, 99.8% Upper OD-adjusted, even if you are only using one set of limits.")
195+
if(length(plot_cols) != 4){
196+
stop("Please supply a vector of 4 colours for funnel limits, in order: 95% Poisson, 99.8% Poisson
197+
, 95% OD-adjusted, 99.8% OD-adjusted, even if you are only using one set of limits.")
200198
}
201199

202200
if(!(label %in% c("outlier", "outlier_lower", "outlier_upper", "highlight"
@@ -233,6 +231,8 @@ funnel_plot <- function(.data, numerator, denominator, group
233231
"99.8% Overdispersed" = plot_cols[4]
234232
)
235233

234+
plot_cols
235+
236236
# map columns for tidyeval compliance
237237

238238
numerator <- quo_name(enquo(numerator))

dev/manual_test.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ mod_plot <-
1313
LOS_model %>%
1414
group_by(Organisation) %>%
1515
summarise(observed = sum(Death),
16-
predicted = sum(preds))
16+
predicted = sum(preds))
1717

1818
# install.packages("C:/Users/Christopher/Documents/R/FunnelPlotR_0.2.3.tar.gz", repos=NULL)
1919

20+
library(COUNT)
21+
data(medpar)
2022
for (i in seq_len(ncol(medpar))) {
2123
if (any(class(medpar[,i]) == "labelled")) {
2224
#browser()
2325
ind = which(class(medpar[,i])=="labelled")
2426
attr(medpar[,i],"class")[ind] <- "labelled_ch"
2527
}
2628
}
29+
30+
funnel_plot(.data = mod_plot, observed, predicted, Organisation, draw_unadjusted = TRUE, x_range = c(0,20), y_range = c(0,2))

man/funnel_plot.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/changing_funnel_plot_options.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ funnel_plot(medpar, denominator=prds,numerator=los
100100
You can change the colours of the limits. I am aiming to allow better colour theme support in future versions, but at present they are supplied as a vector of four hex colours to the `plot_cols` argument, in the order 95% Poisson, 99.8% Poisson, 95% OD and 99.8% OD. At present all four values are required, whether you are plotting all four limits or not.
101101
I have deliberately avoided using red and green colours as defaults because it encourages value judgements about 'good' v.s. 'bad' which may be unreasonable for a given data set. Default colours are paired for both low and high: `c("#FF7F0EFF", "#1F77B4FF", "#9467BDFF", "#2CA02CFF")`
102102

103-
Here I will change the upper 95% limit to black ("#000000"):
103+
Here I will change the 95% limit to black ("#000000"):
104104

105105
```{r colours}
106106
funnel_plot(medpar, denominator=prds,numerator=los
@@ -162,4 +162,4 @@ my_plot <- plot(fp)
162162
my_plot +
163163
geom_vline(aes(xintercept=400), linetype = "dashed", colour="red", linewidth=2)
164164
165-
```
165+
```

0 commit comments

Comments
 (0)