Home > Enterprise >  geom_vline() for each facet
geom_vline() for each facet

Time:07-09

I have been using ggarrange to get the following plot:

enter image description here

with the code:

pg1 <- rebias %>%
  ggplot(aes(g1_hat))  
  geom_density(alpha = .3)  
  labs( x = "g1 values", y = "Density")  
  geom_vline(aes(xintercept=mean(g1_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1) 
  geom_vline(aes(xintercept = sqrt(g1)),
               color = "blue", linetype="dashed", size=1) 
  scale_x_continuous(breaks = c(0.20, 0.25, 0.30, 0.35))  
  theme_bw(12)

pg2 <- rebias %>%
  ggplot(aes(g2_hat))  
  geom_density(alpha = .3)  
  labs( x = "g2 values", y = "Density")  
  geom_vline(aes(xintercept=mean(g2_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1) 
  geom_vline(aes(xintercept = sqrt(g2)),
               color = "blue", linetype="dashed", size=1) 
  scale_x_continuous(breaks = c(0.20, 0.25, 0.30, 0.35))  
  theme_bw(12)


pra <- rebias %>%
  ggplot(aes(ra_hat))  
  geom_density(alpha = .3)  
  labs( x = "ra values", y = "Density")  
  geom_vline(aes(xintercept=mean(ra_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1)  
  geom_vline(aes(xintercept = ra),
               color = "blue", linetype = "dashed", size=1) 
  scale_x_continuous(breaks = c(0.20, 0.25, 0.30, 0.35, 0.4, 0.45))  
  theme_bw(12)


pb1 <- rebias %>%
  ggplot(aes(b1_hat))  
  geom_density(alpha = .3)  
  labs( x = "b1 values", y = "Density")  
  geom_vline(aes(xintercept=mean(b1_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1)  
  geom_vline(aes(xintercept = sqrt(b1)),
               color = "blue", linetype = "dashed", size=1) 
  theme_bw(12)

pb3 <- rebias %>%
  ggplot(aes(b3_hat))  
  geom_density(alpha = .3)  
  labs( x = "b3 values", y = "Density")  
  geom_vline(aes(xintercept=mean(b3_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1)  
  geom_vline(aes(xintercept = sqrt(b3)),
               color = "blue", linetype = "dashed", size=1) 
  theme_bw(12)


prf <- rebias %>%
  ggplot(aes(rf_hat))  
  geom_density(alpha = .3)  
  labs( x = "rf values", y = "Density")  
  geom_vline(aes(xintercept=mean(rf_hat, na.rm=T)),
               color = "red", linetype = "solid", size=1)  
  geom_vline(aes(xintercept = mean(rf) ),
               color = "blue", linetype = "dashed", size=1) 
  theme_bw(12)

ggarrange(pg1,pb1, pra,pg2,  pb3, prf,  common.legend=T)

However this is long and less appealing than the facet_wrap() option. When I try the facet_wrap I get as far as:

rebias |>
  pivot_longer(c(g1_hat, g2_hat, b1_hat, b3_hat, ra_hat, re_hat, rf_hat), 
               names_to="key", values_to="value") |>
  ggplot(aes(value))   
  geom_density(alpha = .3)  
  geom_vline(aes(xintercept=mean(value)),
               color = "red", linetype = "solid", size=1) 
  facet_wrap(~key, nrow = 2)  
  theme_bw(12)

enter image description here

But as you can see, the means are not adapting to each facet.

  • How I come closer to the first figure using facet_wrap()?

Data for the MWE:

structure(list(b1 = c(0.05, 0.05, 0.05), b3 = c(0.05, 0.05, 0.05
), g1 = c(0.07, 0.07, 0.07), g2 = c(0.07, 0.07, 0.07), ra = c(0.4,
0.4, 0.4), re = c(-0.05, 0, 0.05), rf = c(0, 0, 0), as = c(0.447213595499958,
0.447213595499958, 0.447213595499958), es = c(0.894427190999916,
0.894427190999916, 0.894427190999916), ab = c(0.447213595499958,
0.447213595499958, 0.447213595499958), eb = c(0.894427190999916,
0.894427190999916, 0.894427190999916), cs = c(0, 0, 0), cb = c(0,
0, 0), rc = c(0, 0, 0), PB_B = c(0.0436808637120807, 0.0428878252615651,
0.0421230690149219), PS_S = c(0.0436808637120806, 0.0428878252615649,
0.0421230690149221), PS_PB_S = c(0.0467385241719263, 0.0458899730298745,
0.0450716838459666), PS_PB_B = c(0.0467385241719264, 0.0458899730298746,
0.0450716838459665), S_B = c(0.273298269240854, 0.302500154537562,
0.332064217550403), B_S = c(0.273298269240854, 0.302500154537562,
0.332064217550403), all_B = c(0.308645406187966, 0.337315121362517,
0.366428223008927), all_S = c(0.308645406187966, 0.337315121362517,
0.366428223008927), ncpg1 = c(0, 0, 0), dfg1 = c(1, 1, 1), powg1 = c(0.05,
0.05, 0.05), ncpg2 = c(0, 0, 0), dfg2 = c(1, 1, 1), powg2 = c(0.05,
0.05, 0.05), ncpg12 = c(0, 0, 0), dfg12 = c(2, 2, 2), powg12 = c(0.0500000000000001,
0.0500000000000001, 0.0500000000000001), rmz1 = c(0.358228721212896,
0.358228721212896, 0.358228721212896), rmz2 = c(0.179114360606448,
0.179114360606448, 0.179114360606448), rdz1 = c(0.179114360606448,
0.179114360606448, 0.179114360606448), g1_diff = c(-0.0795751311064591,
0.00542486889354094, 0.0864248688935409), b1_hat = c(0.227, 0.226,
0.217), b3_hat = c(0.229, 0.223, 0.218), g1_hat = c(0.185, 0.27,
0.351), g2_hat = c(0.203, 0.219, 0.361), ra_hat = c(0.517, 0.435,
0.218), re_hat = c(0.098, 0.043, -0.151), rf_hat = c(0, 0, 0),
    as_hat = c(0.474, 0.445, 0.421), es_hat = c(0.914, 0.893,
    0.872), ab_hat = c(0.467, 0.462, 0.418), eb_hat = c(0.908,
    0.907, 0.87), b2_hat = c(0.019, -0.001, -0.021), b4_hat = c(0.015,
    0.011, -0.023)), row.names = c(NA, -3L), class = "data.frame")

CodePudding user response:

rebias |>
  pivot_longer(c(g1_hat, g2_hat, b1_hat, b3_hat, ra_hat, re_hat, rf_hat), 
               names_to="key", values_to="value") -> rebias_reshape
  
ggplot(rebias_reshape, aes(value))   
  geom_density(alpha = .3)  
  geom_vline(data = rebias_reshape |>
               dplyr::group_by(key) |> dplyr::summarize(mean = mean(value)),
             aes(xintercept=mean),
             color = "red", linetype = "solid", size=1) 
  facet_wrap(~key, nrow = 2)  
  theme_bw(12)

enter image description here

  • Related