【问题标题】:Combining multiple data frames in one plot using ggplot2使用ggplot2在一个图中组合多个数据框
【发布时间】:2020-03-17 11:07:52
【问题描述】:

我想使用ggplot2 在一个绘图上绘制多个数据框。 我有大约 30 个数据帧,每个数据帧都包含相同的变量(x=value、y=index 和标准误差),如下例所示。

例如)

df1 <-
       value        SE              index
1  -3.447418 0.4308221           19.00978
2  -3.999097 0.4308221          147.79562
3  -4.316288 0.4308221          268.78998
4  -4.449099 0.4308221          332.87519
5  -3.696987 0.4308221          447.74797
6  -3.313633 0.4308221          565.46903
7  -3.335039 0.4308221          709.58848
8  -2.486115 0.4308221          838.49382
9  -1.230000 0.4308221          993.37466
10 -2.558116 0.4308221         1150.04461

df2
.
.
.
.
.
df30

我想用不同颜色绘制所有 30 个数据框,并为每个值显示误差线。我是 R 新手,我几乎无法使用以下代码绘制带有错误栏的单个数据框。

A plot image


df1 = read.table("data_1.txt", header=TRUE, sep="\t")

p = ggplot(df1, aes(x=index, y=value)) + 
    geom_line(size=1, colour = "coral") + 
    geom_point(size=2.5, colour = "coral") + 
    ylab("value") + xlab("index")

gp = p + scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) + 
     scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) + 
     geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") + 
     theme_classic()

plot(gp)

我一直在研究Displaying multiple data frames in ggplot2R : ggplot2 plot several data frames in one plot,但我找不到最佳解决方案。有人可以告诉我如何做我正在寻找的东西吗?

非常感谢您提前提供的帮助。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    如果您创建数据框列表,您将能够使用 dplyr 包中的 bind_rows,例如

    
        df_plot <- bind_rows(df_list, .id = original_df)
    
        ggplot(df_plot, aes(index, value, group = original_df, colour = original_df) +
         geom_line(size=1) + 
         geom_point(size=2.5) + 
         labs(x = 'index', y = "value") + 
         scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) + 
         scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) + 
         geom_errorbar(aes(ymax=value+sd, 
                           ymin=value-sd), 
                       width=20,
                       size=0.2, 
                       colour = "black") + 
     theme_classic()
    

    【讨论】:

      【解决方案2】:

      您可以向每个数据框添加一个新列来标识它。

      df1$id <- "df1"  # or any other description you want
      df2$id <- "df2"
      

      然后你加入所有数据框:

      df.all <- rbind(df1, df2, ..., df30)
      

      现在您可以使用group=color= 美学选项将所有数据绘制在一起:

      p = ggplot(df.all, aes(x=index, y=value, group=id, color=id)) + 
          geom_line(size=1, colour = "coral") + 
          geom_point(size=2.5, colour = "coral") + 
          ylab("value") + xlab("index") +
          scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) + 
          scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) + 
          geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") + 
          theme_classic()
      

      不过,我担心一个图上的 30 个重叠数据集可能看起来很拥挤。您可能还想尝试使用 facets,它们是每个单独图的小面板,如下所示。

      p = ggplot(df.all, aes(x=index, y=value)) + 
          geom_line(size=1, colour = "coral") + 
          geom_point(size=2.5, colour = "coral") + 
          ylab("value") + xlab("index") +
          scale_y_continuous(limits=c(-5.5, 0.5), breaks=seq(-5.5, 0.5, 0.5)) + 
          scale_x_continuous(limits=c(0, 1530), breaks=seq(0, 1530, 250)) + 
          geom_errorbar(aes(ymax=value+sd, ymin=value-sd), width=20, size=0.2, colour = "black") + 
          theme_classic() +
          facet_wrap(~id)
      

      【讨论】:

        猜你喜欢
        • 2019-02-08
        • 2019-07-29
        • 1970-01-01
        • 2010-12-05
        • 2012-08-16
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多