给定 X 轴的累积图

Posted

技术标签:

【中文标题】给定 X 轴的累积图【英文标题】:Cumulative Plot with Given X-Axis 【发布时间】:2010-10-14 00:49:12 【问题描述】:

我有这样的数据。 我想在其中绘制 dat1 的累积值 到 x 轴。也将它与 dat2 一起绘制。

#x-axis dat1              dat2
-10     0.0140149       0.0140146
-9      0.00890835      0.00891768
-8      0.00672276      0.00672488
-7      0.00876399      0.00879401
-6      0.00806879      0.00808141
-5      0.0088366       0.00885121
-4      0.00856872      0.00857769
-3      0.0195384       0.0196094
-2      0.0160239       0.0161829
-1      0.0254455       0.0257845
0       0.0397413       0.0400913
1       0.0743316       0.0755453
2       0.0247501       0.0253324
3       0.0214285       0.021778
4       0.0241462       0.0244967
5       0.0150943       0.015241
6       0.0141398       0.0142373
7       0.0101515       0.0102948
8       0.0308843       0.031294
9       0.0095504       0.00960626
10      0.00729676      0.0073713

在 R 中的常用方法是什么?

我从 Hmisc 查看了 ECDF,它似乎没有达到我想要的效果。 特别是它不允许我们给出 x 轴值。

【问题讨论】:

【参考方案1】:

我认为您正在寻找的函数是 cumsum(),它将对向量进行累积和。

#put your data into 3 vectors
x<-c(-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10)
dat1<-c(0.0140149,0.00890835,0.00672276,0.00876399,0.00806879,0.0088366,0.00856872,0.0195384,0.0160239,0.0254455,0.0397413,0.0743316,0.0247501,0.0214285,0.0241462,0.0150943,0.0141398,0.0101515,0.0308843,0.0095504,0.00729676)
dat2<-c(0.014015,0.008918,0.006725,0.008794,0.008081,0.008851,0.008578,0.019609,0.016183,0.025785,0.040091,0.075545,0.025332,0.021778,0.024497,0.015241,0.014237,0.010295,0.031294,0.009606,0.007371)

#create a new vector called cdat1 to hold the cumulative sum
cdat1<-cumsum(dat1)
plot(x,cdat1)
points(x,dat2,col="red")

我使用上面的功能点将 dat2 添加到现有绘图中。在 R 中运行它,看看它是否能满足您的需求。

【讨论】:

【参考方案2】:

如果您在文本文件(例如 data.txt)中有该数据,您还可以执行以下操作:

A <- read.table("data.txt",header=TRUE)
attach(A)
plot(x.axis, cumsum(dat1))
points(x.axis, cumsum(dat2), col='red')

正如 JD Long 所说,cumsum 函数正是您要找的。​​p>

【讨论】:

以上是关于给定 X 轴的累积图的主要内容,如果未能解决你的问题,请参考以下文章

text 带有yearqtr x轴的条形图

以日期为 X 轴的 Seaborn 条形图

R语言ggplot2可视化强制指定X轴的日期范围实战:组合条形图和lm模型构建的回归模型的结果并强制指定X轴的日期范围

小技巧获取matlab中cdfplot函数的x轴,y轴的数值

如何使用Figure(matplotlib FigureCanvasQTAgg)绘制具有多个y轴的共享x轴子图?

Android MPChart柱状图的X轴的文字有部分被挡住了,怎么解决