Independent y axis LengthOfStay (LOS)

Dear Posit Community
Issue = LOS values so much lower than TotalAmount and Revenue
Repex = at the end of the post
Aim = these 3 variables must be in the same plot for doctors to understand the impact of HAC (Hospital Acquired Complications) on cost and revenue, and length of stay
Request = to have an independent y axis for LOS but still in the same plot
I hope you will help me in this.
G

# data
appendicitis <- tibble::tribble(
                     ~drg,  ~HAC,    ~fy,      ~variable,  ~n,     ~min,     ~max,   ~median,      ~iqr,     ~mean,       ~sd,       ~se,       ~ci,
                   "G07A",  "No", "FY23",  "TotalAmount", 18L,  6183.31,  45712.1, 13648.705,  9378.748, 15971.993,  9961.483,  2347.944,  4953.729,
                   "G07A",  "No", "FY23",      "Revenue", 18L, 16409.21, 23549.27,  17341.87,  2543.158, 18305.757,  2127.115,   501.366,  1057.789,
                   "G07A",  "No", "FY23", "LengthOfStay", 18L,    1.151,    7.756,     3.359,      2.43,     3.866,     1.811,     0.427,     0.901,
                   "G07A", "Yes", "FY23",  "TotalAmount",  4L, 27620.41, 73944.01, 45659.025, 33327.578, 48220.618, 22505.973, 11252.987, 35812.026,
                   "G07A", "Yes", "FY23",      "Revenue",  4L, 18880.28, 56790.64, 34602.445, 30623.757, 36218.952, 19378.046,  9689.023, 30834.795,
                   "G07A", "Yes", "FY23", "LengthOfStay",  4L,    9.078,   21.965,    14.783,    11.041,    15.152,      6.78,      3.39,    10.789
                   )
appendicitis 
#> # A tibble: 6 × 13
#>   drg   HAC   fy    variable         n    min    max median    iqr   mean     sd
#>   <chr> <chr> <chr> <chr>        <int>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
#> 1 G07A  No    FY23  TotalAmount     18 6.18e3 4.57e4 1.36e4 9.38e3 1.60e4 9.96e3
#> 2 G07A  No    FY23  Revenue         18 1.64e4 2.35e4 1.73e4 2.54e3 1.83e4 2.13e3
#> 3 G07A  No    FY23  LengthOfStay    18 1.15e0 7.76e0 3.36e0 2.43e0 3.87e0 1.81e0
#> 4 G07A  Yes   FY23  TotalAmount      4 2.76e4 7.39e4 4.57e4 3.33e4 4.82e4 2.25e4
#> 5 G07A  Yes   FY23  Revenue          4 1.89e4 5.68e4 3.46e4 3.06e4 3.62e4 1.94e4
#> 6 G07A  Yes   FY23  LengthOfStay     4 9.08e0 2.20e1 1.48e1 1.10e1 1.52e1 6.78e0
#> # ℹ 2 more variables: se <dbl>, ci <dbl>

# drg = diagnosis related group
# HAC = Hospital Acquired Complications
# fy = financial year
# TotalAmount = cost
# Revenue = funding received
# LengthOfStay = hospital stay in days

# library
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggpubr)
#> Loading required package: ggplot2
library(ggh4x)
#> Warning: package 'ggh4x' was built under R version 4.3.3

appendicitis %>% 
  ggbarplot(x = 'drg', y = 'mean', fill = 'HAC',
            position = position_dodge()) + 
  facet_nested(~variable + HAC)

Created on 2024-04-13 with reprex v2.1.0

Does this help?

theme_set(theme_void())
ggplot(appendicitis)+
  geom_col(aes(x = drg, y = mean, fill = HAC), position = "dodge")+
  facet_wrap(~variable, scales = "free_y")+
  theme(legend.position = "top",
        plot.margin = margin(12, 12, 12, 12, "pt"),
        text = element_text(size = unit(10, "pt"), color = "#2f2f2f"),
        strip.background = element_rect(fill = "#f2f2f2", color = "#000000"),
        strip.text = element_text(
          size = unit(10, "pt"), 
          color = "#000000", 
          margin = margin(6, 0, 6, 0, "pt")
          ),
        axis.ticks = element_line(color = "#2f2f2f"),
        axis.ticks.length = unit(6, "pt"),
        axis.text.x = element_text(angle = 0, hjust = .5),
        axis.text.y = element_text(angle = 0, hjust = 1),
        axis.title.x = element_text(),
        axis.title.y = element_text(angle = 90),
        axis.line = element_line(color = "#2f2f2f"),
        legend.key = element_rect(color = "#2f2f2f", fill = NULL),
  )
1 Like

Dear @mduvekot Thanks for your prompt response. It works. If you could help make the same scale and same value of y ticks for total amount and revenue, I would really appreciate. Reason = we usually compare cost (total amount) and revenue for HAC yes and no. One more = I want to insert another plot for n with independent y scale and ticks. n represents the volume of patients or the number of admissions. Thank you so much. G

It seems the package ggh4x (see link above) is built specifically to allow you to do things like that.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.