如何解决同样,我在R上有4个图,x轴不同,但是趋势曲线相似我试图覆盖它们,但它们不对齐
我被协助在此链接I have 2 graphs on R. They have different x axis,but similar trend profile. how do I overlay them on r?上重叠了两个x轴不同的图形。 但是,我现在正尝试叠加4张图。我试图覆盖它们,但它们不对齐。 我需要协助来覆盖这四个图形。
我的初始试用代码如下:
-
我的原始数据在下面的链接https://drive.google.com/drive/folders/1ZZQAATkbeV-Nvq1YYZMYdneZwMvKVUq1?usp=sharing中。
-
用于执行的代码:
first <- ggplot(data = first,aes(x,y)) + geom_line(pch = 1) second <- ggplot(data = second,y)) + geom_line(pch = 1) third <- ggplot(data = third,y)) + geom_line(pch = 1) fourth <- ggplot(data = fourth,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个具有x
和y
列的数据集,并希望对齐每个数据集中的峰,以便它们相互堆叠。这是我们将所有数据集一起绘制时的样子:
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"))
方法如下:
- 找到每个数据集的x峰值
- 调整每个x峰值以匹配第一个x峰值
- 合并数据集并一起绘制which respects Tidy Data principles
发现峰并调整x值
要查找峰,我喜欢使用findpeaks()
库中的pracma
函数。您将数据集的y值(按递增的x值排列)送入函数,该函数将返回一个矩阵,每行代表一个“峰值”,各列为您提供y值的峰值高度, index 或峰的数据集行,峰的起点和峰的终点。举例来说,以下是我们如何应用此原理以及在其中一个数据集上的结果:
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
的最大值实际上不是感兴趣的峰值;但是,如果我们运行该函数并进行测试,请使用findpeaks()
方法,在该方法中我们返回最高峰值可以正常工作:显然,该函数未在右侧找到“峰值”,因为它具有“向上” ”,而不是“下降”。
下面的功能处理了我们需要做的所有步骤:排列,查找峰和调整峰。
# 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,npeaks=1)
peaks_second <- findpeaks(second$y,npeaks=1)
peaks_third <- findpeaks(third$y,npeaks=1)
peaks_fourth <- findpeaks(fourth$y,npeaks=1)
# minimum peak x value
peak_x <- min(c(first[peaks_first[2],]$x,second[peaks_second[2],third[peaks_third[2],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,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'))
一起绘制
现在fixed
包含所有数据,我们可以将它们全部绘制在一起:
ggplot(fixed,aes(x=x,y=y,color=measure)) + theme_bw() +
geom_line()
替代绘图方法
如果您想将线彼此“堆叠”,这就是所谓的山脊线图。对于创建脊线图的方法,我可以显示两种方法:刻面或使用ggridges
和geom_ridgeline()
。我可以同时展示。
# Using facets
ggplot(fixed,color=measure)) + theme_bw() +
geom_line(show.legend = FALSE) +
facet_grid(measure~.)
请注意,我选择不显示图例,因为带状文本表示相同的信息。
# Using ggridges and geom_ridgeline
ggplot(fixed,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)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。