简体   繁体   中英

Creating a multiple dot plot + box plot + line plot with ggplot2

I would like to create a multiple dot plot + box plot + line plot with ggplot2. I'm able to crate a single one like this: 在此处输入图片说明

But I would like to add dots and lines in this one: 在此处输入图片说明

These are my data:

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 *

This is the code for the first 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"))

Here is the code for the second plot:

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"))

How can I fix this?

Thank you so much!

You can use facets to do this. Since the data in your code isn't easy to reproduce, I used the ChickWeight dataset included in R for this example. First, I edited the code you wrote for the single plot to facet the graph (here by Chick , in your data it would be var ). Then, I made some theme edits to remove the appearance of facetting and make the figure look like a single plot (0 spacing, remove panel borders etc.).

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"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM