【R】ggplotでgeom_rect

そうか、こういう描き方があるのか!という、例。

library(ggplot2)
library(RColorBrewer)

dat <- data.frame(
  "CredInt" = factor(rep(c("80perc", "60perc", "40perc"), 8)),
  "Lower_CI" = c(
    sample(runif(100, -10, -8), 24)
  ),
  "Upper_CI" = c(
    sample(runif(100, 4, 10), 24)
  ),
  Group = rep(c("A", "B"), each = 2),
  Parameter = rep(1:8, each = 3))


ggplot(dat, aes(x = Parameter)) +
  geom_rect(aes(xmin = Parameter - 0.5,
                xmax = Parameter + 0.5,
                ymin = Lower_CI,
                ymax = Upper_CI,
                fill = CredInt)) + 
  scale_fill_hue(h = c(180, 300)) +
  facet_wrap(~ Group, scales = "free_x") + 
  theme_minimal()
> dat
   CredInt  Lower_CI Upper_CI Group Parameter
1   80perc -8.935795 9.250168     A         1
2   60perc -9.982060 4.832350     A         1
3   40perc -8.477996 7.312053     B         1
4   80perc -9.588579 7.899133     B         2
5   60perc -9.913579 7.737327     A         2
6   40perc -9.684712 8.838006     A         2
7   80perc -9.218625 6.067092     B         3
8   60perc -9.914940 5.178232     B         3
9   40perc -8.376714 7.806981     A         3
10  80perc -9.462718 8.447120     A         4
11  60perc -8.757000 6.525563     B         4
12  40perc -9.601854 5.236060     B         4
13  80perc -9.901114 4.465690     A         5
14  60perc -8.267036 7.399517     A         5
15  40perc -8.730298 9.066749     B         5
16  80perc -8.283829 9.449048     B         6
17  60perc -8.117686 8.815240     A         6
18  40perc -9.246132 8.405065     A         6
19  80perc -9.001243 9.669925     B         7
20  60perc -8.463448 9.154178     B         7
21  40perc -8.949357 9.093500     A         7
22  80perc -8.148043 7.323363     A         8
23  60perc -8.106243 4.661192     B         8
24  40perc -8.190131 5.192106     B         8

Add a Comment

メールアドレスが公開されることはありません。