Skip to content

Commit

Permalink
Added CEriskav.plot plotly option
Browse files Browse the repository at this point in the history
  • Loading branch information
andbe committed Dec 30, 2023
1 parent fe1f2b9 commit d552af8
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3method("setKmax<-",bcea)
S3method("setKmax<-",default)
S3method("setReferenceGroup<-",bcea)
S3method("setReferenceGroup<-",default)
S3method(CEriskav.plot,CEriskav)
S3method(bcea,bugs)
S3method(bcea,default)
S3method(bcea,rjags)
Expand Down Expand Up @@ -51,6 +52,7 @@ export("mixedAn<-")
export("setComparisons<-")
export("setKmax<-")
export("setReferenceGroup<-")
export(CEriskav.plot)
export(bcea)
export(best_interv_given_k)
export(ce_table)
Expand Down
45 changes: 45 additions & 0 deletions R/CEriskav.plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @rdname CEriskav.plot
#'
#' @template args-he
#' @param pos_legend Legend position
#'
#' @return \item{plot}{ A ggplot or plot_ly object containing the plot. Returned only
#' if `graph_type="ggplot2"` or `graph_type="plotly"`.}
#' @seealso [bcea()]
#'
#' @references
#' \insertRef{Baio2013}{BCEA}
#'
#' @importFrom grDevices colours
#'
#' @export
#'
CEriskav.plot.CEriskav <- function(he,
pos = "topright",
graph = c("base", "ggplot2" ,"plotly"),
...) {

graph <- match.arg(graph)
# extra_args <- list(...)

if (is_baseplot(graph)) {
CEriskav_plot_base(he,
pos_legend = pos)
} else if (is_ggplot(graph)) {
CEriskav_plot_ggplot(he,
pos_legend = pos)
} else if (is_plotly(graph)) {
CEriskav_plot_plotly(he,
pos_legend = pos)
}
}

#' Cost-effectiveness Plot Including a Parameter of Risk Aversion (CEriskav)
#'
#' @template args-he
#' @param pos_legend Legend position
#' @export
#'
CEriskav.plot <- function(he, ...) {
UseMethod('CEriskav.plot', he)
}
74 changes: 73 additions & 1 deletion R/CEriskav_plot_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @name CEriskav_plot_graph
#' @title Cost-effectiveness Plot Including a Parameter of Risk Aversion
#'
#' @description Choice of base R, \pkg{ggplot2}.
#' @description Choice of base R, \pkg{ggplot2} or \pkg{plotly}.
#'
#' @template args-he
#' @param pos_legend Legend position
Expand Down Expand Up @@ -159,3 +159,75 @@ CEriskav_plot_ggplot <- function(he, pos_legend) {
invisible(list(eib = eibr_plot,
evi = evir_plot))
}

#' @rdname CEriskav_plot_graph
#' @title CEriskav plotly version
#'
CEriskav_plot_plotly <- function(he, pos_legend) {
default_comp <- 1
linetypes <- rep(c(1,2,3,4,5,6), ceiling(he$R/6))[1:he$R]

# labels
text <- paste0("r = ", he$r)

# if the first value for r is small enough,
# consider close to 0 and print label accordingly
if (he$r[1] < 1e-8) text[1] <- paste("r","\U2B62","0")

legend_params <- make_legend_plotly(pos_legend)

eib_dat <-
melt(he$eibr[, default_comp, , drop = FALSE],
value.name = "eibr") |>
dplyr::rename(k = "Var1", r = "Var3") |>
dplyr::mutate(r = factor(.data$r, labels = text))

evi_dat <-
melt(he$evir,
value.name = "evir") |>
dplyr::rename(r = "Var2", k = "Var1") |>
dplyr::mutate(r = factor(.data$r, labels = text))

eibr_plot <-
plotly::plot_ly(data = eib_dat, linetype = ~r, x = ~k) |>
plotly::add_trace(
y = ~eibr,
type = "scatter",
mode = "lines",
linetypes = linetypes,
line = list(
color = "black"
)
) |>
plotly::layout(
title = "EIB as a function of the risk aversion parameter",
xaxis = list(title = "Willingness to pay"),
yaxis = list(title = "EIB"),
legend = list(title = list(text = "Risk aversion"))
) |>
plotly::config(displayModeBar = FALSE)

evir_plot <-
plotly::plot_ly(data = evi_dat, linetype = ~r, x = ~k) |>
plotly::add_trace(
y = ~evir,
type = "scatter",
mode = "lines",
linetypes = linetypes,
line = list(
color = "black"
)
) |>
plotly::layout(
title = "EVI as a function of the risk aversion parameter",
xaxis = list(title = "Willingness to pay"),
yaxis = list(title = "EVI"),
legend = list(title = list(text = "Risk aversion"))
) |>
plotly::config(displayModeBar = FALSE)

print(list(eibr_plot, evir_plot))

invisible(list(eib = eibr_plot,
evi = evir_plot))
}
2 changes: 1 addition & 1 deletion man/BCEA-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions man/CEriskav.plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/CEriskav_plot_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d552af8

Please sign in to comment.