table
plot
package
Author

Reddy Lee

Published

March 28, 2024

Modified

April 6, 2024


1 Setup

Code
library(tidyverse)
library(gtsummary)

2 Plot

Code
mtcars |> 
  mutate(am = as.factor(am)) |>
  ggplot(aes(x = mpg, y = hp, color = am)) +
  geom_point() 
Figure 1: Scatter plot of mpg vs hp colored by am

3 Table

Code
mtcars |> 
  head(5) |>
  knitr::kable()
Table 1: First 5 rows of mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2

3.1 Tables with gtsummary

Code
CO2 |> 
  select(!c(Plant, conc)) |> 
  tbl_summary() 

Characteristic

N = 84

1
Type
    Quebec 42 (50%)
    Mississippi 42 (50%)
Treatment
    nonchilled 42 (50%)
    chilled 42 (50%)
uptake 28 (18, 37)
1

n (%); Median (Q1, Q3)

Code
CO2 |> 
  select(!c(Plant, conc)) |> 
  tbl_summary(by = Type, missing = "no") 

Characteristic

Quebec
N = 42

1

Mississippi
N = 42

1
Treatment

    nonchilled 21 (50%) 21 (50%)
    chilled 21 (50%) 21 (50%)
uptake 37 (30, 40) 19 (14, 28)
1

n (%); Median (Q1, Q3)

Code
CO2 |> 
  select(!c(Plant, conc)) |> 
  tbl_summary(by = Type, missing = "no") |> 
  add_p()
The following errors were returned during `add_p()`:
✖ For variable `Treatment` (`Type`) and "p.value" statistic: The package
  "cardx" (>= 0.2.0) is required.
✖ For variable `uptake` (`Type`) and "p.value" statistic: The package "cardx"
  (>= 0.2.0) is required.

Characteristic

Quebec
N = 42

1

Mississippi
N = 42

1

p-value

Treatment


    nonchilled 21 (50%) 21 (50%)
    chilled 21 (50%) 21 (50%)
uptake 37 (30, 40) 19 (14, 28)
1

n (%); Median (Q1, Q3)

Back to top