【问题标题】:ggplot2: plotting multiple graphs in the same plotggplot2:在同一个图中绘制多个图
【发布时间】:2015-08-20 07:50:49
【问题描述】:

我尝试使用ggplot2 在同一个图上绘制多个图。知道怎么做吗?我有一个名为draw.data 的函数,我在R 中使用它来绘制曲线,它使用ggplot。我想知道是否有任何参数我可以设置为TRUE 以强制ggplot 通过函数draw.data 的下一次调用在同一窗口上绘制下一个图形。

代码如下:

draw.data <- function(xy){
  # Bibliothek für ggplot-Funktion
  # Dependencies: > library("ggplot2") must be imported!

  x.lab <- "concentration [M]"
  y.lab <- "normalised luminescence [%]"

  my.data <- data.frame(xy)

  my_labels <- parse(text = paste("1E", seq(-10, -4, 1), sep = ""))

  # Find max, min and difference
  # y.max <- max(my.data$y)
  # y.min <- min(my.data$y)

  y.max <- 1
  y.min <- 0

  diff <- y.max - y.min

  # Find percentage and apply to new column 
  my.data$y <- apply(my.data, 1, function(z) ((z["y"] - y.min)/diff)*100)

  ggp.temp <- ggplot(my.data, aes(x,y)) +
    #geom_point(aes(x = x, y = y, color = as.factor(x))) +
    #geom_point(aes(x = x, y = y)) +
    #geom_line(aes(x = x, y = y)) +
    #geom_line(aes(x = x, y = y, color = as.factor(x))) +
    geom_line() +
    # Draw 2 lines at 50% and 90% through the y-axis
    geom_hline(yintercept = c(50, 90), linetype = "dotted") + # draw dotted horizontal lines at 50 and 90
    scale_x_continuous(x.lab, breaks = seq(-10, -4, 1), labels = my_labels) + 
    labs(title = "Graph", x = x.lab, y = y.lab)

  return (ggp.temp)
}

这就是我希望情节的样子。

        x          y
 [1,] -10 1.14259527
 [2,]  -9 1.15024188
 [3,]  -8 1.10517450
 [4,]  -7 1.00961311
 [5,]  -6 0.71238360
 [6,]  -5 0.20355333
 [7,]  -4 0.04061895
 [8,] -10 1.11022461
 [9,]  -9 1.11083317
[10,]  -8 1.07867942
[11,]  -7 0.98422000
[12,]  -6 0.73539660
[13,]  -5 0.36134577
[14,]  -4 0.18124645
[15,] -10 2.13212408
[16,]  -9 1.14529425
[17,]  -8 1.25102307
[18,]  -7 1.16045169
[19,]  -6 0.50321380
[20,]  -5 0.15422609
[21,]  -4 0.10198811

[1,] -10 1.16539392
[2,]  -9 1.15855333
[3,]  -8 1.11766975
[4,]  -7 0.97204379
[5,]  -6 0.53504417
[6,]  -5 0.17431435
[7,]  -4 0.29470416
[8,] -10 1.03683145
[9,]  -9 1.07524250
[10,]  -8 1.07761291
[11,]  -7 0.96401682
[12,]  -6 0.78346457
[13,]  -5 0.32783725
[14,]  -4 0.08103084
[15,] -10 0.81372339
[16,]  -9 0.85402909
[17,]  -8 0.86584396
[18,]  -7 0.80705470
[19,]  -6 0.53086151
[20,]  -5 0.15711034
[21,]  -4 0.11496499

【问题讨论】:

  • 一个minimal reproducible example 会很好。
  • 正如 Jaap 所设定的那样,拥有示例数据(“可重现”)会很好。但是,我担心这不是 ggplot 的工作方式。您正在考虑 R 中的标准图形线条,您可以在其中 plot 第一条线和轴,并使用 line 添加更多曲线。
  • 你必须找到一种方法将你的数据集(rbind)组合成更大的数据集,并创建一个像“plot id”这样的分组变量/列。然后使用带有 color=plot id 的 ggplot 将它们全部绘制在同一个图中。正如 Dieter 所说,ggplot 与经典图有不​​同的哲学。
  • 我刚刚用一些数据编辑了这个问题......希望这能有所帮助。谢谢
  • @Lycone 你读过 Jaap 发布的链接吗?请提供易于复制粘贴的数据,例如dput它。

标签: r plot ggplot2


【解决方案1】:

如 cmets 中所述,gpplot 通过向绘图添加不同的层来工作。为了实现你想要的,你应该像这样调整你的函数:

draw.data <- function(xy, add = FALSE) {
    x.lab <- "concentration [M]"
    y.lab <- "normalised luminescence [%]"
    my.data <- data.frame(xy)
    my_labels <- parse(text = paste("1E", seq(-10, -4, 1), sep = ""))
    y.max <- 1
    y.min <- 0
    diff <- y.max - y.min
    my.data$y <- apply(my.data, 1, function(z) ((z["y"] - y.min)/diff)*100)
    if (!add) {
        ggplot(my.data, aes(x,y)) +
          geom_line() +
          geom_hline(yintercept = c(50, 90), linetype = "dotted") +
          scale_x_continuous(x.lab, breaks = seq(-10, -4, 1), 
                                    labels = my_labels) + 
          labs(title = "Graph", x = x.lab, y = y.lab)
    } else {
       geom_line(aes(x, y), data = my.data )
  }
}

然后你可以像这样使用这个函数:

bp <- draw.data(dat)
bp + draw.data(dat.new1, TRUE) + draw.data(dat.new2, TRUE)

然而,这是由于缺乏未经测试的可重复数据。

【讨论】:

  • 我用一些数据测试了你的例子,效果很好......现在我想要不同颜色的图表。知道如何用 ads(...) 解决它吗?
【解决方案2】:

您可以更新代码以将每个新行添加到现有绘图中。

dat1 <- list(x = 1:10, y = c(100, 100, 90, 70, 45, 35, 30, 25, 20, 20))
dat2 <- list(x = 2:11, y = c(100, 98, 91, 73, 46, 37, 35, 30, 29, 28))

library(ggplot2)

draw.data <- function(xy, obj = ggplot()){
    # Bibliothek für ggplot-Funktion
    # Dependencies: > library("ggplot2") must be imported!

    x.lab <- "concentration [M]"
    y.lab <- "normalised luminescence [%]"

    my.data <- data.frame(xy)

    my_labels <- parse(text = paste("1E", seq(-10, -4, 1), sep = ""))

    # Find max, min and difference
    # y.max <- max(my.data$y)
    # y.min <- min(my.data$y)

    y.max <- 1
    y.min <- 0

    diff <- y.max - y.min

    # Find percentage and apply to new column 
    my.data$y <- apply(my.data, 1, function(z) ((z["y"] - y.min)/diff)*100)

    ggp.temp <- obj +
    geom_line(data = my.data, aes(x = x, y = y)) +
    # Draw 2 lines at 50% and 90% through the y-axis
    geom_hline(yintercept = c(50, 90), linetype = "dotted") + 
    scale_x_continuous(x.lab, breaks = seq(-10, -4, 1), 
        labels = my_labels) + 
    labs(title = "Graph", x = x.lab, y = y.lab)

    return (ggp.temp)
}

p1 <- draw.data(xy = dat1)
p2 <- draw.data(xy = dat2, obj = p1)
print(p2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多