Title: | Classification and Visualisation of Effects |
---|---|
Description: | Classify effects by comparing the confidence intervals with thresholds. |
Authors: | Thierry Onkelinx [aut, cre] (<https://orcid.org/0000-0001-8804-4216>, Research Institute for Nature and Forest (INBO)), Research Institute for Nature and Forest (INBO) [cph, fnd] |
Maintainer: | Thierry Onkelinx <[email protected]> |
License: | GPL-3 |
Version: | 0.1.5 |
Built: | 2024-11-01 04:39:59 UTC |
Source: | https://github.com/inbo/effectclass |
plotly
objectSee classification()
for an explication on how the classification is done.
add_classification( p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE, sd, lcl = NULL, ucl = NULL, threshold, reference = 0, prob = 0.9, size = 20, link = c("identity", "log", "logit"), detailed = TRUE, signed = TRUE, labels = class_labels(lang = "en", detailed = detailed, signed = signed), text = NULL, hoverinfo = "text", ref_label = "reference", ref_colour = "#C04384" )
add_classification( p, x = NULL, y = NULL, ..., data = NULL, inherit = TRUE, sd, lcl = NULL, ucl = NULL, threshold, reference = 0, prob = 0.9, size = 20, link = c("identity", "log", "logit"), detailed = TRUE, signed = TRUE, labels = class_labels(lang = "en", detailed = detailed, signed = signed), text = NULL, hoverinfo = "text", ref_label = "reference", ref_colour = "#C04384" )
p |
a plotly object |
x |
the x variable. |
y |
the y variable. |
... |
Arguments (i.e., attributes) passed along to the trace |
data |
A data frame (optional) or crosstalk::SharedData object. |
inherit |
inherit attributes from |
sd |
the variable of the standard error on the link scale. |
lcl |
A vector of lower confidence limits. |
ucl |
A vector of upper confidence limits. |
threshold |
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
|
reference |
The null hypothesis. Defaults to 0. |
prob |
The coverage of the confidence interval when calculated from the
mean |
size |
Size of the point symbol. |
link |
the link between the natural scale and the link scale.
Defaults to |
detailed |
|
signed |
|
labels |
a vector of labels for the classification hover information.
See |
text |
textual labels. |
hoverinfo |
Which hover information to display.
Defaults to |
ref_label |
The label for the reference point.
Will be used for the points where |
ref_colour |
The colour for the reference point. |
Other plotly add-ons:
add_fan()
,
reference_shape()
,
reference_text()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
plotly
objectA fan plot consist of a set of transparent ribbons each representing a
different coverage of the uncertainty around an estimate.
The coverages are based on the assumption of a normal distribution with mean
link(y)
and standard error sd
.
add_fan( p, x = NULL, y = NULL, ..., sd, link = c("identity", "log", "logit"), max_prob = 0.9, step = 0.05, fillcolor = coarse_unsigned_palette[2], data = NULL, inherit = TRUE, text = NULL, hoverinfo = "text", name )
add_fan( p, x = NULL, y = NULL, ..., sd, link = c("identity", "log", "logit"), max_prob = 0.9, step = 0.05, fillcolor = coarse_unsigned_palette[2], data = NULL, inherit = TRUE, text = NULL, hoverinfo = "text", name )
p |
a plotly object |
x |
the x variable. |
y |
the variable median on the natural scale. |
... |
Arguments (i.e., attributes) passed along to the trace |
sd |
the variable of the standard error on the link scale. |
link |
the link between the natural scale and the link scale.
Defaults to |
max_prob |
The coverage of the widest band.
Defaults to |
step |
the step size between consecutive bands.
The function adds all bands with coverage |
fillcolor |
The fill colour of the fan. Defaults to a greyish blue. |
data |
A data frame (optional) or crosstalk::SharedData object. |
inherit |
inherit attributes from |
text |
textual labels. |
hoverinfo |
Which hover information to display.
Defaults to |
name |
Optional name of the trace for the legend. |
Other plotly add-ons:
add_classification()
,
reference_shape()
,
reference_text()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
Breaks a set of pretty breaks for changes.
change_breaks(n = 2, extra = NULL)
change_breaks(n = 2, extra = NULL)
n |
the number of breaks on either side of the reference |
extra |
An optional vector of additional breaks. The function always appends these breaks. Use this option when you want to force this values to be a part of the breaks. |
Other utils:
change_labels()
,
is_effectclass()
,
unlist()
Display logarithmic changes as percentage
change_labels(x)
change_labels(x)
x |
the logarithmic changes |
Other utils:
change_breaks()
,
is_effectclass()
,
unlist()
Return a standardised set of labels for the classification
class_labels( type = c("trend", "effect"), lang = c("en", "nl"), detailed = TRUE, signed = TRUE )
class_labels( type = c("trend", "effect"), lang = c("en", "nl"), detailed = TRUE, signed = TRUE )
type |
What type of effect.
Currently available are |
lang |
The language.
Currently available are |
detailed |
|
signed |
|
Other display functions:
format_ci()
++
strong positive effect: max(threshold) < lcl
+
positive effect: reference < lcl < max(threshold)
and
max(threshold) < ucl
+~
moderate positive effect: reference < lcl
and
ucl < max(threshold)
~
no effect: min(threshold) < lcl < reference
and
reference < ucl < max(threshold)
-~
moderate negative effect: min(threshold) < lcl
and
ucl < reference
-
negative effect: lcl < min(threshold)
and
min(threshold) < ucl < reference
--
strong negative effect: ucl < min(threshold)
?+
potential positive effect: min(threshold) < lcl < reference
and
max(threshold) < ucl
?-
potential negative effect: lcl < min(threshold)
and
reference < ucl < max(threshold)
?
unknown effect: lcl < min(threshold)
and max(threshold) < ucl
classification(lcl, ucl, threshold, reference = 0)
classification(lcl, ucl, threshold, reference = 0)
lcl |
A vector of lower confidence limits. |
ucl |
A vector of upper confidence limits. |
threshold |
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
|
reference |
The null hypothesis. Defaults to 0. |
Other classification functions:
coarse_classification()
,
remove_sign()
coarse_classification(y)
reduces the 10 scales from
y <- classification(x)
to the 4 scales below.
+
positive effect: reference < lcl
~
no effect: min(threshold) < lcl < reference
and
reference < ucl < max(threshold)
-
negative effect: ucl < reference
?
unknown effect: lcl < min(threshold)
or max(threshold) < ucl
coarse_classification(y)
reduces the 6 scales from
y <- remove_sign(classification(x))
into 3 scales.
coarse_classification(classification)
coarse_classification(classification)
classification |
The classification |
Other classification functions:
classification()
,
remove_sign()
The function rounds the estimate, lower and upper confidence interval to the same magnitude. The magnitude shows the width of the confidence interval with two significant digits.
format_ci( estimate, se, lcl, ucl, interval = 0.95, link = c("identity", "log", "logit"), max_digit = 4, percent = FALSE, sign = FALSE, change = FALSE )
format_ci( estimate, se, lcl, ucl, interval = 0.95, link = c("identity", "log", "logit"), max_digit = 4, percent = FALSE, sign = FALSE, change = FALSE )
estimate |
The estimate in the |
se |
The standard error in the |
lcl |
The lower confidence limit.
Ignored when |
ucl |
The upper confidence limit.
Ignored when |
interval |
The coverage of the confidence interval.
Only used when |
link |
The transformation of |
max_digit |
The maximum number of significant digits to display.
Defaults to |
percent |
Display the interval as a percentage
(= multiply by 100 and append |
sign |
Always add the sign to the text. (e.g. |
change |
Display interval as a change.
Subtract |
Other display functions:
class_labels()
format_ci(0.512345, 1) format_ci(0.512345, 1, interval = 0.9) format_ci(0.512345, 1, link = "log") format_ci(0.512345, 1, link = "logit") format_ci(0.512345, 10) format_ci(0.512345, 0.1) format_ci(0.512345, 0.01) format_ci(0.512345, 0.001) format_ci(0.512345, 0.0001) format_ci(0.512345, 0.00001) format_ci(0.512345, 0.00001, max_digit = 10) format_ci(0.512345, 0.5) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, percent = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, sign = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, percent = TRUE, sign = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234) format_ci(0.512345e-6, 1e-6) format_ci(0.512345e-7, 1e-7) format_ci(0.512345e-7, 1e-8) format_ci(0.512345e-7, 1e-9) format_ci(0.512345, 0.1, link = "log", percent = TRUE, change = FALSE) format_ci(0.512345, 0.1, link = "log", percent = TRUE, change = TRUE) format_ci(0, lcl = 0, ucl = 0) format_ci(1, lcl = 1, ucl = 1)
format_ci(0.512345, 1) format_ci(0.512345, 1, interval = 0.9) format_ci(0.512345, 1, link = "log") format_ci(0.512345, 1, link = "logit") format_ci(0.512345, 10) format_ci(0.512345, 0.1) format_ci(0.512345, 0.01) format_ci(0.512345, 0.001) format_ci(0.512345, 0.0001) format_ci(0.512345, 0.00001) format_ci(0.512345, 0.00001, max_digit = 10) format_ci(0.512345, 0.5) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, percent = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, sign = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234, percent = TRUE, sign = TRUE) format_ci(-0.1, lcl = -0.1999, ucl = 0.1234) format_ci(0.512345e-6, 1e-6) format_ci(0.512345e-7, 1e-7) format_ci(0.512345e-7, 1e-8) format_ci(0.512345e-7, 1e-9) format_ci(0.512345, 0.1, link = "log", percent = TRUE, change = FALSE) format_ci(0.512345, 0.1, link = "log", percent = TRUE, change = TRUE) format_ci(0, lcl = 0, ucl = 0) format_ci(1, lcl = 1, ucl = 1)
Check If an Object Is a Valid Effectclass Object
is_effectclass(x, message = c("none", "warning", "error"))
is_effectclass(x, message = c("none", "warning", "error"))
x |
The object to test. |
message |
What to do when the object is not a valid effectclass object.
|
A single TRUE
or FALSE
value.
Other utils:
change_breaks()
,
change_labels()
,
unlist()
plotly
references
Returns a list shapes you can pass to the shapes
argument of
plotly::layout()
Create plotly
references
Returns a list shapes you can pass to the shapes
argument of
plotly::layout()
reference_shape( threshold, reference = 0, colour = "black", line = FALSE, horizontal = TRUE )
reference_shape( threshold, reference = 0, colour = "black", line = FALSE, horizontal = TRUE )
threshold |
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
|
reference |
The null hypothesis. Defaults to 0. |
colour |
The colour for the references.
Defaults to |
line |
display the |
horizontal |
Display horizontal reference when |
Other plotly add-ons:
add_classification()
,
add_fan()
,
reference_text()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
plotly
reference text
Returns a list text you can pass to the annotations
argument of
plotly::layout()
Create plotly
reference text
Returns a list text you can pass to the annotations
argument of
plotly::layout()
reference_text( threshold, reference = 0, offset, text = c("reference", "important decrease", "important increase") )
reference_text( threshold, reference = 0, offset, text = c("reference", "important decrease", "important increase") )
threshold |
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
|
reference |
The null hypothesis. Defaults to 0. |
offset |
An numeric vector with the offset between |
text |
A character vector with three elements with the text to display
on the reference line, bottom threshold line and upper threshold line.
Defaults to |
Other plotly add-ons:
add_classification()
,
add_fan()
,
reference_shape()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 library(plotly) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, text = ~display) |> add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |> layout( hovermode = "x unified", shapes = reference_shape(threshold = 1), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.1, text = ~display) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE), annotations = reference_text(threshold = 1) ) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE ) |> layout(shapes = reference_shape(threshold = 1)) plot_ly(z, x = ~x, y = ~estimate) |> add_fan(sd = ~sd, step = 0.3) |> add_classification( lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE, text = ~display ) |> layout( shapes = reference_shape(threshold = 1, line = TRUE) ) # trend plot_ly(data = trend, x = ~year, y = ~index) |> add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |> add_classification(sd = ~sd, threshold = th) |> layout( hovermode = "x unified", hoverdistance = 1, shapes = reference_shape(threshold = th, reference = ref), annotations = reference_text(threshold = th, reference = ref) )
**
strong effect: ++
or --
*
effect: +
or -
*~
moderate effect: +~
or -~
~
no effect: ~
?+
potential effect: ?+
or ?-
?
unknown effect: ?
remove_sign(classification)
remove_sign(classification)
classification |
The classification |
Other classification functions:
classification()
,
coarse_classification()
A scale for effect points
scale_effect( ..., detailed = TRUE, signed = TRUE, fill = TRUE, colour = TRUE, drop = FALSE, labels = class_labels(lang = "en", detailed = detailed, signed = signed) )
scale_effect( ..., detailed = TRUE, signed = TRUE, fill = TRUE, colour = TRUE, drop = FALSE, labels = class_labels(lang = "en", detailed = detailed, signed = signed) )
... |
Arguments passed on to
|
detailed |
|
signed |
|
fill |
return |
colour |
|
drop |
Drop unused levels.
This is always |
labels |
the labels for the legend. |
Other ggplot2 add-ons:
stat_effect()
,
stat_fan()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 oldw <- getOption("warn") options(warn = -1) library(ggplot2) theme_set(theme_grey(base_family = "Helvetica")) update_geom_defaults("point", list(size = 5)) ggplot(z, aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1) + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, ref_line = "none") + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, errorbar = FALSE) + coord_flip() # plot indices ggplot(trend, aes(x = year, y = index, ymin = lcl, ymax = ucl, sd = sd)) + geom_line() + stat_effect(threshold = th, reference = ref) # plot pairwise differences change_set <- function(z, base_year) { n_year <- max(z$dt) total_change <- lapply( seq_len(n_year) - 1, function(i) { if (i > 0) { y <- tail(z, -i) } else { y <- z } data.frame( from = base_year + i, to = base_year + y$dt, total = cumsum(y$change), sd = sqrt(cumsum(y$sd ^ 2)) ) } ) total_change <- do.call(rbind, total_change) total_change <- rbind( total_change, data.frame( from = total_change$to, to = total_change$from, total = -total_change$total, sd = total_change$sd ) ) total_change$lcl <- qnorm(0.025, total_change$total, total_change$sd) total_change$ucl <- qnorm(0.975, total_change$total, total_change$sd) return(total_change) } head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, aes(colour = total), ref_line = "none", errorbar = FALSE, shape_colour = FALSE ) + scale_colour_gradient2() head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, ref_line = "none", errorbar = FALSE ) options(warn = oldw)
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 oldw <- getOption("warn") options(warn = -1) library(ggplot2) theme_set(theme_grey(base_family = "Helvetica")) update_geom_defaults("point", list(size = 5)) ggplot(z, aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1) + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, ref_line = "none") + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, errorbar = FALSE) + coord_flip() # plot indices ggplot(trend, aes(x = year, y = index, ymin = lcl, ymax = ucl, sd = sd)) + geom_line() + stat_effect(threshold = th, reference = ref) # plot pairwise differences change_set <- function(z, base_year) { n_year <- max(z$dt) total_change <- lapply( seq_len(n_year) - 1, function(i) { if (i > 0) { y <- tail(z, -i) } else { y <- z } data.frame( from = base_year + i, to = base_year + y$dt, total = cumsum(y$change), sd = sqrt(cumsum(y$sd ^ 2)) ) } ) total_change <- do.call(rbind, total_change) total_change <- rbind( total_change, data.frame( from = total_change$to, to = total_change$from, total = -total_change$total, sd = total_change$sd ) ) total_change$lcl <- qnorm(0.025, total_change$total, total_change$sd) total_change$ucl <- qnorm(0.975, total_change$total, total_change$sd) return(total_change) } head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, aes(colour = total), ref_line = "none", errorbar = FALSE, shape_colour = FALSE ) + scale_colour_gradient2() head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, ref_line = "none", errorbar = FALSE ) options(warn = oldw)
Display points with classified effect
stat_effect( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ..., threshold, reference = 0, detailed = TRUE, signed = TRUE, shape_colour = TRUE, errorbar = TRUE, error_colour = TRUE, size = 6, labels = class_labels(lang = "en", detailed = detailed, signed = signed), ref_line = c("all", "ref", "none") )
stat_effect( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ..., threshold, reference = 0, detailed = TRUE, signed = TRUE, shape_colour = TRUE, errorbar = TRUE, error_colour = TRUE, size = 6, labels = class_labels(lang = "en", detailed = detailed, signed = signed), ref_line = c("all", "ref", "none") )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to
|
threshold |
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
|
reference |
The null hypothesis. Defaults to 0. |
detailed |
|
signed |
|
shape_colour |
Colour the background of the labels according to the
classification.
Defaults to |
errorbar |
Display the uncertainty as error bars.
Defaults to |
error_colour |
Colour the error bars according to the classification.
Defaults to |
size |
Size of the symbols. |
labels |
the labels for the legend. |
ref_line |
Which reference lines to display.
|
Other ggplot2 add-ons:
scale_effect()
,
stat_fan()
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 oldw <- getOption("warn") options(warn = -1) library(ggplot2) theme_set(theme_grey(base_family = "Helvetica")) update_geom_defaults("point", list(size = 5)) ggplot(z, aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1) + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, ref_line = "none") + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, errorbar = FALSE) + coord_flip() # plot indices ggplot(trend, aes(x = year, y = index, ymin = lcl, ymax = ucl, sd = sd)) + geom_line() + stat_effect(threshold = th, reference = ref) # plot pairwise differences change_set <- function(z, base_year) { n_year <- max(z$dt) total_change <- lapply( seq_len(n_year) - 1, function(i) { if (i > 0) { y <- tail(z, -i) } else { y <- z } data.frame( from = base_year + i, to = base_year + y$dt, total = cumsum(y$change), sd = sqrt(cumsum(y$sd ^ 2)) ) } ) total_change <- do.call(rbind, total_change) total_change <- rbind( total_change, data.frame( from = total_change$to, to = total_change$from, total = -total_change$total, sd = total_change$sd ) ) total_change$lcl <- qnorm(0.025, total_change$total, total_change$sd) total_change$ucl <- qnorm(0.975, total_change$total, total_change$sd) return(total_change) } head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, aes(colour = total), ref_line = "none", errorbar = FALSE, shape_colour = FALSE ) + scale_colour_gradient2() head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, ref_line = "none", errorbar = FALSE ) options(warn = oldw)
# All possible classes z <- data.frame( estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5), sd = c(rep(0.8, 3), rep(0.3, 7)) ) z$lcl <- qnorm(0.05, z$estimate, z$sd) z$ucl <- qnorm(0.95, z$estimate, z$sd) classification(z$lcl, z$ucl, threshold = 1) -> z$effect c( "?" = "unknown\neffect", "?+" = "potential\npositive\neffect", "?-" = "potential\nnegative\neffect", "~" = "no effect", "+" = "positive\neffect", "-" = "negative\neffect", "+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect", "++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect" )[as.character(z$effect)] -> z$x z$x <- factor(z$x, z$x) z$display <- paste( "estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl) ) # Simulated trend set.seed(20190521) base_year <- 2000 n_year <- 20 trend <- data.frame( dt = seq_len(n_year), change = rnorm(n_year, sd = 0.2), sd = rnorm(n_year, mean = 0.1, sd = 0.01) ) trend$index <- cumsum(trend$change) trend$lcl <- qnorm(0.025, trend$index, trend$sd) trend$ucl <- qnorm(0.975, trend$index, trend$sd) trend$year <- base_year + trend$dt trend$display <- paste( "index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl) ) th <- 0.25 ref <- 0 oldw <- getOption("warn") options(warn = -1) library(ggplot2) theme_set(theme_grey(base_family = "Helvetica")) update_geom_defaults("point", list(size = 5)) ggplot(z, aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1) + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, ref_line = "none") + coord_flip() ggplot(z[3:5, ], aes(x = effect, y = estimate, ymin = lcl, ymax = ucl)) + stat_effect(threshold = 1, errorbar = FALSE) + coord_flip() # plot indices ggplot(trend, aes(x = year, y = index, ymin = lcl, ymax = ucl, sd = sd)) + geom_line() + stat_effect(threshold = th, reference = ref) # plot pairwise differences change_set <- function(z, base_year) { n_year <- max(z$dt) total_change <- lapply( seq_len(n_year) - 1, function(i) { if (i > 0) { y <- tail(z, -i) } else { y <- z } data.frame( from = base_year + i, to = base_year + y$dt, total = cumsum(y$change), sd = sqrt(cumsum(y$sd ^ 2)) ) } ) total_change <- do.call(rbind, total_change) total_change <- rbind( total_change, data.frame( from = total_change$to, to = total_change$from, total = -total_change$total, sd = total_change$sd ) ) total_change$lcl <- qnorm(0.025, total_change$total, total_change$sd) total_change$ucl <- qnorm(0.975, total_change$total, total_change$sd) return(total_change) } head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, aes(colour = total), ref_line = "none", errorbar = FALSE, shape_colour = FALSE ) + scale_colour_gradient2() head(trend, 10) |> change_set(base_year) |> ggplot(aes(x = from, y = to, ymin = lcl, ymax = ucl)) + stat_effect( threshold = th, reference = ref, ref_line = "none", errorbar = FALSE ) options(warn = oldw)
A fan plot consist of a set of transparent ribbons each representing a
different coverage of the uncertainty around an estimate.
The coverages are based on the assumption of a normal distribution with mean
link(y)
and standard error link_sd
.
stat_fan( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, geom = "ribbon", ..., link = c("identity", "log", "logit"), max_prob = 0.9, step = 0.05 )
stat_fan( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, geom = "ribbon", ..., link = c("identity", "log", "logit"), max_prob = 0.9, step = 0.05 )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom |
Use a different |
... |
Other arguments passed on to
|
link |
the link function to apply on the |
max_prob |
The coverage of the widest band.
Defaults to |
step |
the step size between consecutive bands.
The function adds all bands with coverage |
Other ggplot2 add-ons:
scale_effect()
,
stat_effect()
set.seed(20191218) z <- data.frame( year = 1990:2019, dx = rnorm(30, sd = 0.2), s = rnorm(30, 0.5, 0.01) ) z$index <- 3 + cumsum(z$dx) library(ggplot2) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() + geom_line() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(step = 0.3) ggplot(z, aes(x = year, y = exp(index), link_sd = s)) + stat_fan(link = "log") + geom_line() ggplot(z, aes(x = year, y = plogis(index), link_sd = s)) + stat_fan(link = "logit") + geom_line() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "rect") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "bar") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "errorbar") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "linerange") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "pointrange") z <- expand.grid(year = 1990:2019, category = c("A", "B")) z$dx <- rnorm(60, sd = 0.1) z$index <- rep(c(0, 2), each = 30) + cumsum(z$dx) z$s <- rnorm(60, rep(c(0.5, 1), each = 30), 0.05) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() + geom_line() + facet_wrap(~category) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(aes(fill = category)) + geom_line(aes(colour = category))
set.seed(20191218) z <- data.frame( year = 1990:2019, dx = rnorm(30, sd = 0.2), s = rnorm(30, 0.5, 0.01) ) z$index <- 3 + cumsum(z$dx) library(ggplot2) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() + geom_line() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(step = 0.3) ggplot(z, aes(x = year, y = exp(index), link_sd = s)) + stat_fan(link = "log") + geom_line() ggplot(z, aes(x = year, y = plogis(index), link_sd = s)) + stat_fan(link = "logit") + geom_line() ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "rect") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "bar") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "errorbar") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "linerange") ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(geom = "pointrange") z <- expand.grid(year = 1990:2019, category = c("A", "B")) z$dx <- rnorm(60, sd = 0.1) z$index <- rep(c(0, 2), each = 30) + cumsum(z$dx) z$s <- rnorm(60, rep(c(0.5, 1), each = 30), 0.05) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan() + geom_line() + facet_wrap(~category) ggplot(z, aes(x = year, y = index, link_sd = s)) + stat_fan(aes(fill = category)) + geom_line(aes(colour = category))
Flatten Lists
unlist(x, recursive = TRUE, use.names = TRUE)
unlist(x, recursive = TRUE, use.names = TRUE)
x |
an R object, typically a list or vector. |
recursive |
logical. Should unlisting be applied to list
components of |
use.names |
logical. Should names be preserved? |
base::unlist
Other utils:
change_breaks()
,
change_labels()
,
is_effectclass()