【问题标题】:Creating a multiple dot plot + box plot + line plot with ggplot2使用 ggplot2 创建多点图 + 箱线图 + 线图
【发布时间】:2018-10-10 15:32:43
【问题描述】:

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

但我想在这个中添加点和线:

这些是我的数据:

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 *

这是第一个情节的代码。

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

我该如何解决这个问题?

非常感谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用构面来执行此操作。由于代码中的数据不容易重现,因此我在此示例中使用了 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"))
    

    【讨论】:

    • 非常感谢,我修改了一点代码,现在效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多