簡體   English   中英

同樣,我在 R 上有 4 個圖表,x 軸不同,但趨勢概況相似。 我試圖覆蓋它們,但它們沒有對齊

[英]Again, I have 4 graphs on R, different x axis, but similar trend profile. I tried to overlay them but they are not aligned

有人協助我在此鏈接上疊加兩個具有不同 x 軸的圖表我在 R 上有 2 個圖表。它們具有不同的 x 軸,但趨勢概況相似。 我如何將它們覆蓋在 r 上? . 但是,我現在正在嘗試疊加 4 個圖形 我試圖覆蓋它們,但它們沒有對齊。 我需要幫助來疊加這四張圖。 在此處輸入圖像描述

我最初的試用代碼如下:

  1. 我的原始數據位於以下鏈接https://drive.google.com/drive/folders/1ZZQAATkbeV-Nvq1YYZMYdneZwMvKVUq1?usp=sharing中。

  2. 用於執行的代碼:

     first <- ggplot(data = first, aes(x, y)) + geom_line(pch = 1) second <- ggplot(data = second, aes(x, y)) + geom_line(pch = 1) third <- ggplot(data = third, aes(x, y)) + geom_line(pch = 1) fourth <- ggplot(data = fourth, aes(x, y)) + geom_line(pch = 1) first$match <- first$x second$match <- second$x - second$x[second$y == max(second$y)] + first$x[first$y == max(first$y)] third$match <- third$x fourth$match <- fourth$x first$series = "first" second$series = "second" third$series = "third" fourth$series = "fourth" all_data <- rbind(first, second, third, fourth) ggplot(all_data) + geom_line(aes(x = match, y, color = series)) + scale_x_continuous(name = "X, arbitrary units") + theme(axis.text.x = element_blank())

非常感謝您的幫助。

OP,我想我會為你的問題提出一個解決方案。 OP 有 4 個包含xy列的數據集,並希望對齊每個數據集中的峰值,以便它們相互堆疊。 這是我們將所有數據集 plot 放在一起時的樣子:

p <- ggplot(mapping=aes(x=x, y=y)) + theme_bw() +
  geom_line(data=first, aes(color="first")) +
  geom_line(data=second, aes(color="second")) +
  geom_line(data=third, aes(color="third")) +
  geom_line(data=fourth, aes(color="fourth"))

在此處輸入圖像描述

該方法如下:

  1. 找到每個數據集的峰值 x 值
  2. 調整每個峰值 x 值以匹配第一個峰值 x 值
  3. 將數據集和 plot 結合在一起,尊重 Tidy Data 原則

查找峰值並調整 x 值

為了找到峰值,我喜歡使用pracma庫中的findpeaks() function。 您向 function 提供數據集的 y 值(按遞增的 x 值排列),function 將返回一個矩陣,其中每一行代表一個“峰值”,列為您提供 y 值、索引或數據集行的峰值高度峰值、峰值開始的位置和峰值結束的位置。 例如,以下是我們如何應用這一原則以及在其中一個數據集上的結果:

library(pracma)

first <- arrange(first, x)  # arrange first by increasing x
findpeaks(first$y, sortstr = TRUE, npeaks=1)

        [,1] [,2] [,3] [,4]
[1,] 1047.54  402  286  515

參數sortstr=表示我們希望首先按“最高”排序的峰列表,我們只對選擇第一個峰感興趣。 在這種情況下,我們可以看到 402 是峰的first x,y 值的索引。 所以我們可以通過first[index,]$x訪問那個 x 值。

我們在這里可能擔心的一個問題是這可能不適用於fourth ,因為y的最大值實際上不是感興趣的峰值; 但是,如果我們運行 function 並對其進行測試,使用返回最高峰的findpeaks()方法可以正常工作:顯然 function 沒有在右側找到“峰值”,因為它有一個“向上”,但不是“下降”。

下面的 function 處理了我們需要做的所有步驟:排列、查找峰值和調整峰值。

# find the minimum peak.  We know it's from third, but here's
# how you do it if you don't "know" that

peaks_first <- findpeaks(first$y, sortstr = TRUE, npeaks=1)
peaks_second <- findpeaks(second$y, sortstr = TRUE, npeaks=1)
peaks_third <- findpeaks(third$y, sortstr = TRUE, npeaks=1)
peaks_fourth <- findpeaks(fourth$y, sortstr = TRUE, npeaks=1)

# minimum peak x value
peak_x <- min(c(first[peaks_first[2],]$x, second[peaks_second[2],]$x, third[peaks_third[2],]$x, fourth[peaks_fourth[2],]$x))

# function to use to fix each dataset
fix_x <- function(peak_x, dataset) {
  dataset <- arrange(dataset, x)
  d_peak <- findpeaks(dataset$y, sortstr = TRUE, npeaks=1)
  d_peak_x <- dataset[d_peak[2],]$x
  x_adj <- peak_x - d_peak_x
  dataset$x <- dataset$x + x_adj
  return(dataset)
}

# apply and fix each dataset
fix_first <- fix_x(peak_x, first)
fix_second <- fix_x(peak_x, second)
fix_third <- fix_x(peak_x, third)
fix_fourth <- fix_x(peak_x, fourth)

# combine datasets
fix_first$measure <- 'First'
fix_second$measure <- 'Second'
fix_third$measure <- 'Third'
fix_fourth$measure <- 'Fourth'

fixed <- rbind(fix_first, fix_second, fix_third, fix_fourth)
fixed$measure <- factor(fixed$measure, levels=c('First','Second','Third','Fourth'))

Plot 一起

現在fixed包含所有數據,我們可以將它們全部放在一起 plot :

ggplot(fixed, aes(x=x, y=y, color=measure)) + theme_bw() +
  geom_line()

在此處輸入圖像描述

替代繪圖方法

如果您想將線條“堆疊”在彼此之上,這就是所謂的脊線 plot。我可以展示兩種創建脊線 plot 的方法:刻面或使用ggridgesgeom_ridgeline() 我可以證明兩者。

# Using facets
ggplot(fixed, aes(x=x, y=y, color=measure)) + theme_bw() +
  geom_line(show.legend = FALSE) +
  facet_grid(measure~.)

在此處輸入圖像描述

請注意,我選擇不顯示圖例,因為條帶文本指示相同的信息。

# Using ggridges and geom_ridgeline
ggplot(fixed, aes(x=x, y=measure, color=measure)) + theme_bw() +
  geom_ridgeline(aes(height=y), fill=NA, scale=0.001)

在此處輸入圖像描述

使用geom_ridgeline()時,您會注意到y=美學成為用於堆疊的列,而您的原始 y 值被映射到height=美學。 我還不得不嘗試使用scale= ,因為對於離散值,每個measure都將被視為整數(1、2、3、4)。 你的height=值比那個高很多,所以我們必須縮小它們,使它們在這個范圍內(縮小大約 1000)。

暫無
暫無

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

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