/
changing_funnel_plot_options.Rmd
165 lines (117 loc) · 6.11 KB
/
changing_funnel_plot_options.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
---
title: "Changing funnel plot options"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 2
vignette: >
%\VignetteIndexEntry{Changing funnel plot options}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
\usepackage[utf8]{inputenc}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.height = 5,
fig.width = 7
)
```
## Setup
```{r setup}
library(FunnelPlotR)
```
This brief vignette shows how to go about changing some of the parameters available in the `FunnelPlotR` package.
Firstly, lets set up some data, the same as the README and other vignette:
```{r dtsetup}
library(COUNT)
data(medpar)
medpar$provnum<-factor(medpar$provnum)
medpar$los<-as.numeric(medpar$los)
# Logistic model to predict LOS, LOS is quite overdispersed
mod<- glm(los ~ hmo + died + age80 + factor(type), family="poisson", data=medpar)
#Get predicted value for ratio
medpar$prds<- predict(mod, newdata = medpar, type="response")
# Draw plot, returning just the plot object
funnel_plot(medpar, denominator=prds, numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE)
```
## Highlighting a data point
You can pick out data point(s) using the `highlight` option. Here we we use the example above to highlight the hospital labels '030002' in the data set.
```{r highlight}
# Draw plot, returning just the plot object
funnel_plot(medpar, denominator=prds, numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, highlight="030002")
```
## Themes
You can alter themes in the `FunnelPlotR` packages by using the theme argument. There are a couple of options included with the package `funnel_clean` (the default) and `funnel_grey`, but you can write your own theme using any valid `ggplot2` theme and pass it to the plot.
```{r plottheme1}
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, theme = funnel_grey() )
```
If you are not familiar with themes, you can create a theme with the `theme` function from `ggplot2`. I will create a new theme, including the `funnel_grey` theme, but with some different (exaggerated) elements.
```{r plottheme2}
library(ggplot2)
new_funnel_theme <-
funnel_grey()+
theme(plot.title = element_text(face="bold", colour="red", size=6), # Change plot title
legend.background = element_rect(fill="brown"), # Alter legend background colour
axis.title.y = element_text(angle=0) #Rotate y axis label
)
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, theme = new_funnel_theme)
```
## Plot colours
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 eight hex colours to the `plot_cols` argument, in the order 95% Poisson (lower/upper), 99.8% Poisson (lower/upper), 95% OD (lower/upper) and 99.8% OD (lower/upper). At present all eight values are required, whether you are plotting all eight limits or not.
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", "#FF7F0EFF", "#1F77B4FF","#1F77B4FF", "#9467BDFF", "#9467BDFF", "#2CA02CFF", "#2CA02CFF")`
Here I will change the upper 95% Poisson limit to black ("#000000"):
```{r colours}
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, theme = funnel_grey(),
plot_cols = c("#FF7F0EFF", "#000000", "#1F77B4FF","#1F77B4FF", "#9467BDFF", "#9467BDFF", "#2CA02CFF", "#2CA02CFF"))
```
## Changing scales
`FunnelPlotR` automatically sets it's scales from the data you present to it but, on occasions, the scale rules might fall down for a particular dataset, or you may want to fix the plot to a particular scale. You can do this using the `xrange` and `yrange` arguments. Each takes a vector of two values, the minimum and the maximum:
```{r funnelscales}
## Changing labels
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, x_range=c(0, 400), y_range=c(0,2))
```
`ggplot2` will warn you, as above, if you are excluding any points from your plot.
## Changing labels
You can change the plot labels and axis labels easily using the options: `title`, `x_label` and `y_label`.
```{r funnellabels1}
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99 ,label = "outlier"
, draw_unadjusted = TRUE, title = "Vignette funnel plot"
, x_label = "x-axis", y_label = "y-axis")
```
There are different labelling options for the data points too, using the `label` option. The default is to label outliers, but you can turn labels off, label the highlighted points, or both the highlighted points and the outliers ('both')
```{r funnellabels2}
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99
, draw_unadjusted = TRUE, title = "Vignette funnel plot"
, x_label = "x-axis", y_label = "y-axis"
, highlight= "030002", label = "highlight")
```
## Cutting out the ggplot object
Since `FunnelPlotR` uses `ggplot2`, you could always extract the plot and alter it manually like any other `ggplot2` object. The easiest way is to extract it with `plot()`. Below we'll add a (completely useless) vertical line to demonstrate adding more elements:
```{r cutoutplot}
# Original funnel plot object
fp <-
funnel_plot(medpar, denominator=prds,numerator=los
, group = provnum, limit=99, label = "outlier"
, draw_unadjusted = TRUE)
# Extract just the plot
my_plot <- plot(fp)
# Add an additional geom to plot
my_plot +
geom_vline(aes(xintercept=400), linetype = "dashed", colour="red", linewidth=2)
```