簡體   English   中英

ggplot R中的for循環中的圖形編號

[英]ggplot figure number in for loop in R

我有以下數據:

    Detector=c("A", "A", "A", "A","B", "B", "B", "B","C", "C", "C", "C")
    hours=rep(c(0,4,24,48),3)
    LoD=c(rep(25,4),rep(40,4),rep(34,4))
    Ct=c(24,24.2,24.3,24,43,42,43,41,35,37,35,35.6)
    Sample.Type=c(rep("T",2),rep("P",4),rep("T",5), rep("P",1))
    d4.1=data.frame(Detector,hours, Ct, LoD, Sample.Type)

我想將Ct與小時數作圖作為一個簡單的散點圖,作為每個檢測器的循環,並將每個檢測器的lod值顯示為繪圖上的水平線。 我使用以下代碼,效果很好:

    for(i in unique(d4.1$Detector)) {
    print(ggplot(d4.1[d4.1$Detector==i], aes(d4.1$hours[d4.1$Detector==i], 
    d4.1$Ct[d4.1$Detector==i], colour = d4.1$Sample.Type[d4.1$Detector==i]))+
    geom_point()+
    geom_hline(yintercept=mean(d4.1$LoD[d4.1$Detector==i]))+
    ggtitle(paste("Figure", j,i, sep=" "))+
    theme(plot.title = element_text(hjust = 
    0.5),plot.caption=element_text(hjust = 0))+
    labs(x="Hours/Time Points", y="Ct",caption=paste("The solid black line 
    is the LoD(=",mean(d4.1$LoD[d4.1$Detector==i]),") for",i,sep=" "), 
    colour="Sample Types"))}

我需要的是ggplot的標題。 生成的每個圖都用A,B和C標記,但我想在其中添加以下文本:

    Figure 1: A
    Figure 2: B
    Figure 3: C

我嘗試添加1-3的“ j”序列,並將其粘貼到標題(如下)中,但此方法無效。 它給了我9個圖(圖1A,圖1B,圖1C,圖2A ...等)

     for(i in unique(d4.1$Detector)){for (j in seq(1:3)){print(ggplot(d4.1[which(d4.1$Detector==i),], aes(hours,Ct, colour = Sample.Type))+geom_point()+geom_hline(yintercept=mean(LoD))+ggtitle(paste("Figure",j,i,sep=" "))+theme(plot.title = element_text(hjust = 0.5),plot.caption=element_text(hjust = 0))+labs(x="Hours/Time Points", y="Ct",caption=paste("The solid black line is the LoD(=",mean(LoD),") for",i,sep=" "), colour="Sample Types"))}}

如何在循環中向ggplot標題添加數字序列(1,2,3 ...)? 非常感謝你。

如果您存儲唯一的Detector在一個變量( detectors ),並使用seq_along你會得到索引為1到3( x的功能)。 然后,您可以創建三個圖,如下所示:

detectors <- unique(d4.1$Detector)
lapply(seq_along(detectors), function (x) {
  selected_detector <- detectors[[x]]
  df_plot <- d4.1[d4.1$Detector == selected_detector, ]
  p <- ggplot(df_plot, aes(x = hours, y = Ct, colour = Sample.Type)) +
      geom_point() + geom_hline(yintercept = mean(df_plot$LoD)) +
      ggtitle(paste("Figure", x, selected_detector, sep = " ")) +
      theme(plot.title = element_text(hjust = 0.5),
        plot.caption = element_text(hjust = 0))+
        labs(x = "Hours/Time Points", y = "Ct",
          caption=paste("The solid black line is the LoD(=",
            mean(df_plot$LoD),") for", selected_detector, sep=" "),
          colour="Sample Types")
  print(p)
})

在此處輸入圖片說明

由於您的代碼不可復制,因此我決定使用光圈數據給出一個替代示例。

(i)根據種類將虹膜分為3個子集的列表。 類似於提供的代碼中的d4.1[d4.1$Detector==i,]

df <- split(iris, iris$Species)

(ii)編寫一個包含不同子集的通用繪圖函數

plotFn <- function(df){
              ggplot(df, aes(Sepal.Length, Petal.Length)) + 
              geom_point() + 
              geom_hline(yintercept = mean(df[,"Petal.Length"]))
          }

(iii)獲取ggplot對象的列表,然后進行lapply來單獨添加Figure x: y格式。

p <- lapply(df, plotFn)

lapply(1:length(p), function(x){
          p[[x]] + 
          ggtitle(paste0("Figure ", x, ": ", names(p)[x]))
       })

這將提供三個情節:

在此處輸入圖片說明

您的代碼不可復制,因為您編寫循環的方式。 您首先對數據框進行子集設置,然后在aes()中請求不再存在的子集。

盡管我發現Kristoffer和Adam的答案比我的答案更為優雅,但這里還是一個粗略的版本,它使用了您的原始循環。 我通過將因子轉換為數字來使用因子級別A對應於第一級別的事實。

for(i in unique(d4.1$Detector)) {


 print(ggplot(d4.1[d4.1$Detector==i,], aes(hours, 
                                           Ct, colour = Sample.Type))+
          geom_point()+
          geom_hline(yintercept=mean(d4.1$LoD[d4.1$Detector==i]))+
          ggtitle(paste("Figure", as.numeric(d4.1$Detector[d4.1$Detector==i]), ":" , i, sep=" "))+
            theme(plot.title = element_text(hjust = 
                                            0.5),plot.caption=element_text(hjust = 0))+
          labs(x="Hours/Time Points", y="Ct",caption=paste("The solid black line 
                                                           is the LoD(=",mean(d4.1$LoD[d4.1$Detector==i]),") for",i,sep=" "), 
               colour="Sample Types"))}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM