使用ggplot2创建多点图+箱形图+线图

时间:2018-10-10 15:32:44

标签: r ggplot2

我想用ggplot2创建多点图+箱形图+线图。 我可以像这样创建一个: enter image description here

但是我想在此添加点和线: enter image description here

这些是我的数据

pz  val Time    var
1   5.62    Pre VC (L) - D **
2   5.86    Pre VC (L) - D **
3   3.14    Pre VC (L) - D **
4   2.27    Pre VC (L) - D **
5   0.94    Pre VC (L) - D **
8   2.91    Pre VC (L) - D **
9   1.01    Pre VC (L) - D **
10  2.98    Pre VC (L) - D **
1   5.69    Post    VC (L) - D **
2   6.22    Post    VC (L) - D **
3   3.29    Post    VC (L) - D **
4   2.21    Post    VC (L) - D **
5   0.85    Post    VC (L) - D **
8   3.28    Post    VC (L) - D **
9   1.28    Post    VC (L) - D **
10  3.13    Post    VC (L) - D **
1   4.44    Pre FEV1 (L) - D **
2   4.5 Pre FEV1 (L) - D **
3   2.51    Pre FEV1 (L) - D **
4   1.51    Pre FEV1 (L) - D **
5   0.84    Pre FEV1 (L) - D **
8   2.65    Pre FEV1 (L) - D **
9   0.85    Pre FEV1 (L) - D **
10  1.25    Pre FEV1 (L) - D **
1   4.55    Post    FEV1 (L) - D **
2   4.71    Post    FEV1 (L) - D **
3   2.56    Post    FEV1 (L) - D **
4   1.53    Post    FEV1 (L) - D **
5   0.76    Post    FEV1 (L) - D **
8   3.29    Post    FEV1 (L) - D **
9   0.99    Post    FEV1 (L) - D **
10  2.33    Post    FEV1 (L) - D **
1   0.85    Pre Creatinine (mg/dl) - E *
2   0.82    Pre Creatinine (mg/dl) - E *
3   0.59    Pre Creatinine (mg/dl) - E *
4   0.34    Pre Creatinine (mg/dl) - E *
5   0.46    Pre Creatinine (mg/dl) - E *
6   0.25    Pre Creatinine (mg/dl) - E *
7   0.5 Pre Creatinine (mg/dl) - E *
8   0.5 Pre Creatinine (mg/dl) - E *
9   0.4 Pre Creatinine (mg/dl) - E *
10  0.5 Pre Creatinine (mg/dl) - E *
11  0.6 Pre Creatinine (mg/dl) - E *
1   0.85    Post    Creatinine (mg/dl) - E *
2   0.88    Post    Creatinine (mg/dl) - E *
3   0.5 Post    Creatinine (mg/dl) - E *
4   0.33    Post    Creatinine (mg/dl) - E *
5   0.45    Post    Creatinine (mg/dl) - E *
6   0.27    Post    Creatinine (mg/dl) - E *
7   0.6 Post    Creatinine (mg/dl) - E *
8   0.5 Post    Creatinine (mg/dl) - E *
9   0.58    Post    Creatinine (mg/dl) - E *
10  0.64    Post    Creatinine (mg/dl) - E *
11  0.74    Post    Creatinine (mg/dl) - E *

这是第一个ploT的代码。

d  <- read.csv("C:/Users/.../diet3.csv", sep=";")
d$group <- factor(d$group, levels=c("Pre", "Post"))
x <- ggplot(d, aes(y = val)) +
  geom_boxplot(aes(x = group, group = group), fill = 'grey') + 
  geom_point(aes(x = group), size = 5) +
  geom_line(aes(x = group), group = d$tie)+ 
  theme_bw() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

x + scale_fill_grey(start=0.8, end=0.5) +
  labs(x="BMI", y="Values", fill="Time") + 
  theme_bw() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

这是第二个情节的代码:

box10 <- read.csv("C:/Users/.../med1.csv", sep=";")
box10$Time <- factor(box10$Time, levels=c("Pre", "Post"))
box10$var <- factor(box10$var, 
                    levels=c("VC (L) - D **","FEV1 (L) - D **",
                             "Creatinine (mg/dl) - E *"))
p <- ggplot(box10, aes(x = box10$var, y = box10$val, fill = box10$Time)) +
  geom_boxplot() + 
  stat_boxplot(geom ='errorbar')

p + scale_fill_grey(start=0.8, end=0.5) +
  labs(x="Parameters", y="Values", fill="Time") + 
  theme_bw() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))

我该如何解决?

非常感谢您!

1 个答案:

答案 0 :(得分:2)

您可以使用构面来执行此操作。由于代码中的数据不容易复制,因此在此示例中,我使用了R中包含的ChickWeight数据集。首先,我编辑了您为单个图编写的代码以使其与图面(此处为Chick,在您的数据中为var)。然后,我进行了一些主题编辑,以删除刻面的外观,并使该图看起来像是单个图(0间距,删除面板边框等)。

require(ggplot2)
d <- ChickWeight #dataset included in R
d <- d[d$Time %in% c(10, 21),] #subset to get pre/post type data
d$group <- ifelse(d$Time == 10, "Pre", "Post")
d$group <- factor(d$group, levels = c("Pre", "Post"))

ggplot(d, aes(y = weight, x = group)) +
  geom_boxplot(aes(fill = group)) + 
  geom_point() +
  geom_line(aes(group = Chick))+ 
  theme_classic() +
  facet_grid(.~Diet) + #facet graph to get multiple groups of boxplots/lines/points
  #change theme elements so graph does not appear facetted
  theme(panel.border = element_blank(), #remove borders on facets
        panel.spacing = unit(0, "lines"), #remove spacing btween panels
        strip.background = element_rect(color = "white"))

enter image description here