r - 使用 ggplot2 创建多点图 + 箱线图 + 线图

标签 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 *

这是第一个情节的代码。

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 中包含的 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

关于r - 使用 ggplot2 创建多点图 + 箱线图 + 线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52743835/

相关文章:

r - dplyr - 根据条件从两个不同的数据帧中减去

r - R中for循环中的2个变量

r - 如何读取用 R 加密的 .xls 文件?

r - 使用 aes_string 在变量名称中添加空格

r - 为什么 ggplotly 在 rmarkdown 中不能像 ggplot 一样工作

r - 可以在格子和 ggplot2 图中使用多边形()或等效项吗?

R eulerr 包 - 显示错误的欧拉图

r - 如何在不指定列名的情况下将行添加到特定位置(索引)的小标题? (右)

r - 当我安装 ggplot2 的开发版本时会发生什么

r - ggmap/ggplot2 中多个图例的对齐方式