同样,我在R上有4个图,x轴不同,但是趋势曲线相似我试图覆盖它们,但它们不对齐

如何解决同样,我在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张图。我试图覆盖它们,但它们不对齐。 我需要协助来覆盖这四个图形。

enter image description here

我的初始试用代码如下:

  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,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个具有xy列的数据集,并希望对齐每个数据集中的峰,以便它们相互堆叠。这是我们将所有数据集一起绘制时的样子:

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"))

enter image description here

方法如下:

  1. 找到每个数据集的x峰值
  2. 调整每个x峰值以匹配第一个x峰值
  3. 合并数据集并一起绘制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()

enter image description here

替代绘图方法

如果您想将线彼此“堆叠”,这就是所谓的山脊线图。对于创建脊线图的方法,我可以显示两种方法:刻面或使用ggridgesgeom_ridgeline()。我可以同时展示。

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

enter image description here

请注意,我选择不显示图例,因为带状文本表示相同的信息。

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

enter image description here

使用geom_ridgeline()时,您会注意到y=美学成为用于堆叠的列,而原始y值则映射到height=美学。我还不得不使用scale=,因为对于离散值,每个measure将被视为整数(1、2、3、4)。您的height=值要比该值高,因此我们必须将其缩小以使其在该范围内(缩小约1000)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-