ggplot_forest: Draw a Forestplot of Measures of Effects

ggplot_forestR Documentation

Draw a Forestplot of Measures of Effects

Description

Intern wird geom_pointrange verwendet

Alternating Background Colour

Add alternating background color along the y-axis. The geom takes default aesthetics odd and even that receive color codes. The codes would preferably be in the 8-hex ARGB format to allow for transparency if the geom is meant to be used as visual background.

Horizontal Study Effects with Confidence Intervals

Builds a custom version of geom_pointrangeh.

Usage

ggplot_forest(
  data,
  name = term,
  estimate = estimate,
  conf.low = conf.low,
  conf.high = conf.high,
  se = NULL,
  groups = NULL,
  colour = NULL,
  shape = NULL,
  logodds = FALSE,
  col.strip = c(even = "#00000000", odd = "#33333333"),
  col = NULL,
  cex = 1,
  cex.pch = 1.2,
  cex.pch.legende = cex.pch,
  dodge.width = 0.5,
  pch = c(21L, 22L, 23L, 24L, 25L),
  main = NULL,
  xlab = NULL,
  xlim = NULL,
  xtickbreaks = NULL,
  legend.title = ggplot2::waiver(),
  ...
)

geom_stripes(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  show.legend = NA,
  inherit.aes = TRUE
)

geom_effect2(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  dodge.width = 0.5,
  position = ggplot2::position_dodge(width = dodge.width),
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

theme_forest(
  base_size = 13,
  base_line_size = base_size/22,
  base_rect_size = base_size/22
)

Arguments

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

name, estimate, se, pvalue

the variable

groups, colour, shape

the variable

logodds

logical (defaults to FALSE)

col.strip, col

Farbe strips und Symbole col.strip =NULL keine strips

cex

size

cex.pch

pch size symbol

dodge.width

abstand bei Gruppen zwischen den Balken

legend.title

Beschriftung main, xlab

...

main, xlab, graphical parameters

mapping

Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

stat

The statistical transformation to use on the data for this layer. When using a ⁠geom_*()⁠ function to construct a layer, the stat argument can be used the override the default coupling between geoms and stats. The stat argument accepts the following:

  • A Stat ggproto subclass, for example StatCount.

  • A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_count(), give the stat as "count".

  • For more information and other ways to specify the stat, see the layer stat documentation.

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 position argument accepts the following:

  • The result of calling a position function, such as position_jitter(). This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as a string, strip the function name of the position_ prefix. For example, to use position_jitter(), give the position as "jitter".

  • For more information and other ways to specify the position, see the layer position documentation.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

df

A data frame

Details

Visualize multiple measures of effect with their confidence intervals in a vertical layout.

stolen from https://github.com/NightingaleHealth/ggforestplot

Anmerkung: ggforestplot scheint nicht mehr betreut zu werden - manche ggplot Funktionen sind nict mehr aktuell!

Author(s)

Ilari Scheinin

Examples


 # ----------------------------------------
 # Draw a Forestplot of Measures of Effects
 # ----------------------------------------

require(magrittr)
require(ggplot2)
require(stp25plot)

load(
  "C:/Users/wpete/Dropbox/1_Projekte/002-NURMI/791_Martina_Gregori/dummy-forest-df.Rdata"
)

data$std.error[is.na(data$estimate)] <- 0
data$conf.low[is.na(data$estimate)] <- 0
data$conf.high[is.na(data$estimate)] <- 0
data$estimate[is.na(data$estimate)] <- 0
data <- data[-which(data$var == "age"), ]
data <- data[-which(data$var == "(Intercept)"), ]

data$level <- factor(data$level,
                     c("M/UM", "HM" , "10 km"),
                     c("M/UM", "HM", "10 km (Reference)"))

dotCOLS = c(  "#74C476","#9E9AC8","#f9b282")
barCOLS = c("#006D2C" , "#756BB1","#A63603")


ggplot_forest(
  data,
  name = group,
  groups = level ,
  title = "Associations to Running",
  xlab = "Estimate (95% CI)",
  legend.title = "legend title using guide",
  col = barCOLS,
  cex.pch = 3,
  cex = 1.1,
  # cex.pch =  1,
  pch = c(19, 15, 17)
)

#----------------------------
# Alternative geom_pointrange
#----------------------------

ggplot(data = data, aes(x = group  , y = estimate)) +
  geom_pointrange(
    mapping = aes(
      ymin = conf.low ,
      ymax = conf.high,
      color = level,
      shape = level
    ),
    position = ggplot2::position_dodge(width =  0.5)
  ) +
  GGally::geom_stripped_cols(odd = "#00000000", even = "#11111111") +
  ggplot2::guides(
    colour = guide_legend(
      reverse = TRUE,
      title = "legend.title",
      override.aes =  list(linewidth = 1)
    ),
    shape = guide_legend(reverse = TRUE, title = "legend.title")
  ) +
  scale_color_manual(values = dotCOLS) +
  stp25plot::theme_forest(base_size = 12) +
  coord_flip()

#-----------------------------
#' # Alternative geom_errorbar
#-----------------------------

ggplot(data = data, aes(x = group, y = estimate)) +
  geom_point(
    mapping = aes(
      color = level,
      shape = level
    ),
    position = ggplot2::position_dodge(width =  0.5)
  ) +
  geom_errorbar(
    mapping = aes(
      ymin = conf.low ,
      ymax = conf.high,
      color = level,
      width = 0.2
    ),
    position = ggplot2::position_dodge(width =  0.5)
  ) +
  GGally::geom_stripped_cols(odd = "#00000000", even = "#11111111") +
  coord_flip() +
  scale_color_manual(values = dotCOLS) +
  stp25plot::theme_forest(base_size = 12)

#---------------------------
# Alternative geom_linerange
#---------------------------

p <- ggplot(data,
            aes(
              x = group,
              y = estimate,
              ymin = conf.low ,
              ymax = conf.high,
              col = level,
              fill = level
            )) +
  #specify position here
  #geom_hline(yintercept = c(0, 20, 40, 60), lty = 2) +
  # geom_vline(xintercept = seq(0, 15) + .5, lty = 2, col = "gray") +
  geom_linerange(linewidth = 3, position = position_dodge(width = .7)) +
  #specify position here too
  geom_point(
    size = 3,
    shape = 21,
    colour = "white",
    stroke = 0.5,
    position = position_dodge(width = .7)
  ) +
  # scale_fill_manual(values = barCOLS) +
  # scale_color_manual(values = dotCOLS) +
  
  guides(
    colour = guide_legend(reverse=TRUE),
    fill = guide_legend(reverse=TRUE)
  ) +
  
  scale_x_discrete(name = "") +
  scale_y_continuous(name = "Estimate 95% CI", limits = c(-10, 5)) +
  coord_flip() +
  
  stp25plot::theme_forest(base_size = 12)
# theme_minimal() +
# theme(
#   panel.grid.major = element_blank(),
#   panel.grid.minor = element_blank())

p  +
  scale_fill_manual(values = barCOLS) +
  scale_color_manual(values = dotCOLS)


stp4/stp25plot documentation built on March 29, 2025, 4:26 p.m.