forest_plot: forest_plot

View source: R/8-forest_plot.R

forest_plotR Documentation

forest_plot

Description

Gestohlen von survminer::ggforest()

prepare_forest Extrahiere Parameter

Usage

forest_plot(x, ...)

## S3 method for class 'data.frame'
forest_plot(
  x,
  main = NULL,
  include.gof = NULL,
  replace.lable.dots = TRUE,
  scale.log = FALSE,
  plot = TRUE,
  ...
)

## Default S3 method:
forest_plot(
  x,
  data = insight::get_data(x),
  main = NULL,
  include.indercept = TRUE,
  include.referenze = TRUE,
  include.gof = TRUE,
  include.label = FALSE,
  replace.lable.dots = TRUE,
  standardize = FALSE,
  scale.log = FALSE,
  plot = TRUE,
  ...
)

prepare_forest(
  ...,
  names = NULL,
  standardize = FALSE,
  include.indercept = TRUE,
  include.referenze = TRUE,
  include.label = FALSE,
  group_name = "group"
)

prepare_forest1(
  x,
  data = insight::get_data(x),
  main = NULL,
  include.indercept = TRUE,
  include.referenze = include.referenze,
  include.label = FALSE,
  standardize = FALSE,
  scale.log = FALSE
)

Arguments

x

fit

...

model-fits

main

Beschriftung

scale.log

fuer die Beschriftung

data

data

include.indercept

logical

include.label

logical or character

standardize

parameters::model_parameters: The method used for standardizing the parameters. Can be NULL (default; no standardization), "refit" (for re-fitting the model on standardized data) or one of "basic", "posthoc", "smart", "pseudo".

names

name

group_name

Variable name of trhe group

Value

ggplot

data.frame

data.frame

main effect Es fehlen noch die N sowie die Referenz ist noch nicht vorhanden

am Ende $ aber name kann auch bei factoren dazischen liegen

Examples

set.seed(1)
n <- 10 * 2 * 3
dat <- data.frame(
  y = rnorm(n),
  sex = gl(2, n / 2, labels = c("male", "female")),
  rx = gl(3, n / 3, labels = c("Obs",  "Tev", "Tev+5FU")),
  age = 1:n,
  bmi = rnorm(n )
)
dat <- transform(dat,
                 y = y + 
                   as.numeric(sex) / 2 + 
                   as.numeric(rx) 
)


fit1 <- lm(y ~ sex + rx  ,  dat)
fit2 <- lm(y ~ sex + rx + age * bmi,  dat)

forest_plot(fit1, plot=T)
forest_plot(fit1, plot=F)
forest_plot(fit1, plot="ggplot_forest", main="Hallo")
prepare_forest(fit2)
ggplot_forest( prepare_forest(fit1,fit2) )+
ggforce::facet_col(
    facets = ~group,
    scales = "free_y",
    space = "free"
  )
  
 ggplot_table(prepare_forest(fit1))

fit1 <- lm(y ~ sex + rx + age + bmi,  dat)
forest_plot(fit1, standardize = TRUE)

# require(surv)
# require(survminer)
 require(survival)
 require(stpvers)

forest_plot(glm(status ~ sex + rx + adhere,
data = colon, family = binomial()))
#forest_plot(coxph(Surv(time, status) ~ sex + rx + adhere,
#             data = colon), data=colon)


# Alternative


#' library('ggplot2') 

Outcome_order <-
  c('Outcome C', 'Outcome A', 'Outcome B', 'Outcome D')

#this is the first dataset you have
df1 <-
  data.frame(
    Outcome = c("Outcome A", "Outcome B", "Outcome C", "Outcome D"),
    estimate = c(1.50, 2.60, 1.70, 1.30),
    conf.low = c(1.00, 0.98, 0.60, 1.20),
    conf.high = c(2.00, 3.01, 1.80, 2.20)
  )




# add a group column
df1$group <- "X"
# create a second dataset, similar format to first
df2 <- df1
# different group
df2$group <- "Y"
# and we adjust the values a bit, so it will look different in the plot
df2[, c("estimate", "conf.low", "conf.high")] <-
  df2[, c("estimate", "conf.low", "conf.high")] + 0.5

# combine the two datasets
df = rbind(df1, df2)
# you can do the factoring here
df$Outcome = factor (df$Outcome, level = Outcome_order)

df





p <- ggplot(df,
            aes(
              x = Outcome,
              y = estimate,
              ymin = conf.low ,
              ymax = conf.high,
              col = group,
              fill = group
            )) +
  #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(-0, 5)) +
  coord_flip() +
  # GGally::geom_stripped_cols()+
  #theme_void()
  theme_minimal() +
  theme( 
    
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())



dotCOLS = c(  "#74C476","#BAE4B3","#f9b282")
barCOLS = c("#006D2C" , "#74C476","#A63603")


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

# require(magrittr)
# require(tidyverse)
# require(stp25plot)
# #devtools::install_github("NightingaleHealth/ggforestplot")
# require(stp25tools)
require(ggplot2)

#library("extrafont")
require(survival)
#data(package = "survival", colon)
colon<- Label(colon, sex="Geschlecht")
#colon$sex <- factor(as.numeric(colon$sex), 0:1, c("male", "female"))
 
fit1 <- lm(status ~ sex + rx + adhere, data = colon)
fit2 <- lm(status ~ sex + rx* adhere, data = colon)


A <- prepare_forest("Base Model" = fit1, 
                    fit2,
                    include.referenze=FALES
)

 

#A$estimate[ is.na(A$estimate)] <- 0
#A <- A[A$term !="(Intercept)",]

# Quelle: https://ianasilver.com/making-a-forest-plot-with-ggplot2/
ggplot(data=A, aes(x=term, y=estimate , ymin=conf.low , ymax=conf.high)) +
  geom_pointrange()+ # Makes range for ggplot values based on the data and AES specified in first line
  geom_hline(yintercept=0, lty=2, size =1) +  # add a dotted line at x=0 after flip
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=0.5, cex=1)+ # Makes whiskers on the range (more aesthetically pleasing)
  facet_wrap(~group)+ # Makes DV header (Can handle multiple DVs)
  coord_flip() + # flip coordinates (puts labels on y axis)
  geom_point(shape = 15, size = 2) + # specifies the size and shape of the geompoint
  ggtitle("")+ # Blank Title for the Graph
  xlab("") + # Label on the Y axis (flipped specification do to coord_flip) Independent Variables
  ylab("b (95% CI)") + # Label on the X axis (flipped specification do to coord_flip)
  scale_y_continuous(limits = c(-.250,.52), breaks = c(-.50,-.25,0,.25,.50))+ # limits and tic marks on X axis (flipped specification do to coord_flip)
  theme(line = element_line(colour = "black", size = 1), # My personal theme for GGplots
        strip.background = element_rect(fill="gray90"), 
        legend.position ="none", 
        axis.line.x = element_line(colour = "black"), 
        axis.line.y = element_blank(), 
        panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(), 
        axis.ticks = element_blank(),
        axis.title.x = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(family="Times New Roman",colour = "Black", margin = margin(t = 0, r = 20, b = 0, l = 0)),
        plot.title = element_text(family="Times New Roman", colour = "Black", margin = margin(t = 0, r = 0, b = 20, l = 0)),
        axis.text=element_text(family="Times New Roman", size=14, color = "Black"), 
        text=element_text(family="Times New Roman",size=15), plot.margin = margin(t = 2, r = 2, b = 2, l = 2, unit = "cm"))




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