'split vilon plot with overly crude and adjusted estimates from linear regression

I want to create a split vilon plot for comparing distance and age and overly crude and adjusted estimates from linear regression on it. my outcome variable lung function. any help would be really appreciated

structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 11L, 12L, 13L, 

14L, 15L, 16L, 17L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 61L, 62L, 63L, 64L, 65L, 66L, 67L ), age = c("child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult", "child", "child", "adult", "child", "adult", "adult", "adult"), sex = c("M", "F", "M", "M", "M", "F", "F", "F", "M", "M", "M", "F", "F", "F", "M", "M", "M", "F", "F", "F", "F", "M", "M", "F", "F", "F", "F", "M", "M", "F", "F", "F", "F", "M", "F", "F", "F", "F", "F", "M", "F", "M", "F", "F", "F", "M", "F", "M", "M"), distance = c("a", "a", "a", "a", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "a", "a", "a", "b", "b", "b", "b", "b", "b", "a", "b", "b", "b", "b", "b", "b", "a", "b", "b", "b", "b", "b", "b", "a", "b", "b", "b", "b"), edu = c("x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z" ), lungfunction = c(45L, 23L, 25L, 45L, 70L, 69L, 90L, 23L, 25L, 45L, 70L, 69L, 90L, 50L, 25L, 45L, 70L, 69L, 90L, 50L, 62L, 45L, 70L, 69L, 90L, 50L, 62L, 45L, 70L, 69L, 90L, 50L, 62L, 45L, 23L, 69L, 90L, 50L, 62L, 45L, 23L, 25L, 90L, 50L, 62L, 45L, 23L, 25L, 45L), no2 = c(40L, 70L, 50L, 60L, 30L, 25L, 80L, 70L, 50L, 60L, 30L, 25L, 80L, 89L, 50L, 60L, 30L, 25L, 80L, 89L, 10L, 60L, 30L, 25L, 80L, 89L, 10L, 40L, 30L, 25L, 80L, 89L, 10L, 40L, 70L, 25L, 80L, 89L, 10L, 40L, 70L, 50L, 80L, 89L, 10L, 40L, 70L, 50L, 60L ), pm25 = c(15L, 20L, 36L, 48L, 25L, 36L, 28L, 20L, 36L, 48L, 25L, 36L, 28L, 15L, 36L, 48L, 25L, 36L, 28L, 15L, 25L, 48L, 25L, 36L, 28L, 15L, 25L, 15L, 25L, 36L, 28L, 15L, 25L, 15L, 20L, 36L, 28L, 15L, 25L, 15L, 20L, 36L, 28L, 15L, 25L, 15L, 20L, 36L, 48L )), row.names = c(NA, -49L), class = "data.frame")

enter image description here

crude <- lm(lungfunction~distance, data = publication_dummytable2) summary(crude)

adjusted <- lm(lungfunction~distance+age+edu+no2+pm25, data = publication_dummytable2) summary(adjusted)



Solution 1:[1]

It is not clear in the question of what columns belong to crude and adjusted values. Here is something using no2 and pm25:

library(tidyverse)
library(ggh4x)
library(see)

data <- structure(list(id = c(
  1L, 2L, 3L, 4L, 5L, 6L, 7L, 11L, 12L, 13L,
  14L, 15L, 16L, 17L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 31L, 32L,
  33L, 34L, 35L, 36L, 37L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 51L,
  52L, 53L, 54L, 55L, 56L, 57L, 61L, 62L, 63L, 64L, 65L, 66L, 67L
), age = c(
  "a", "a", "b", "a", "b", "b", "b", "a", "a", "b",
  "a", "b", "b", "b", "a", "a", "b", "a", "b", "b", "b", "a", "a",
  "b", "a", "b", "b", "b", "a", "a", "b", "a", "b", "b", "b", "a",
  "a", "b", "a", "b", "b", "b", "a", "a", "b", "a", "b", "b", "b"
), sex = c(
  "M", "F", "M", "M", "M", "F", "F", "F", "M", "M",
  "M", "F", "F", "F", "M", "M", "M", "F", "F", "F", "F", "M", "M",
  "F", "F", "F", "F", "M", "M", "F", "F", "F", "F", "M", "F", "F",
  "F", "F", "F", "M", "F", "M", "F", "F", "F", "M", "F", "M", "M"
), distance = c(
  "a", "a", "a", "a", "b", "b", "b", "a", "a",
  "a", "a", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "a",
  "a", "a", "a", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b",
  "a", "a", "a", "a", "b", "b", "b", "a", "a", "a", "a", "b", "b",
  "b"
), edu = c(
  "x", "x", "x", "y", "y", "y", "z", "x", "x", "x",
  "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x", "x",
  "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z", "x",
  "x", "x", "y", "y", "y", "z", "x", "x", "x", "y", "y", "y", "z"
), lungfunction = c(
  45L, 23L, 25L, 45L, 70L, 69L, 90L, 23L, 25L,
  45L, 70L, 69L, 90L, 50L, 25L, 45L, 70L, 69L, 90L, 50L, 62L, 45L,
  70L, 69L, 90L, 50L, 62L, 45L, 70L, 69L, 90L, 50L, 62L, 45L, 23L,
  69L, 90L, 50L, 62L, 45L, 23L, 25L, 90L, 50L, 62L, 45L, 23L, 25L,
  45L
), no2 = c(
  40L, 70L, 50L, 60L, 30L, 25L, 80L, 70L, 50L, 60L,
  30L, 25L, 80L, 89L, 50L, 60L, 30L, 25L, 80L, 89L, 10L, 60L, 30L,
  25L, 80L, 89L, 10L, 40L, 30L, 25L, 80L, 89L, 10L, 40L, 70L, 25L,
  80L, 89L, 10L, 40L, 70L, 50L, 80L, 89L, 10L, 40L, 70L, 50L, 60L
), pm25 = c(
  15L, 20L, 36L, 48L, 25L, 36L, 28L, 20L, 36L, 48L,
  25L, 36L, 28L, 15L, 36L, 48L, 25L, 36L, 28L, 15L, 25L, 48L, 25L,
  36L, 28L, 15L, 25L, 15L, 25L, 36L, 28L, 15L, 25L, 15L, 20L, 36L,
  28L, 15L, 25L, 15L, 20L, 36L, 28L, 15L, 25L, 15L, 20L, 36L, 48L
)), row.names = c(NA, -49L), class = "data.frame")

data %>%
  mutate(age = age %>% recode("a" = "Adults", "b" = "Children")) %>%
  pivot_longer(c(no2, pm25)) %>%
  ggplot(aes(name, lungfunction, fill = distance)) +
  geom_violinhalf(data = ~ .x %>% filter(distance == "a"), flip = TRUE) +
  geom_violinhalf(data = ~ .x %>% filter(distance == "b")) +
  facet_nested(~ age + sex)

Created on 2022-04-04 by the reprex package (v2.0.0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo