如何在时间序列数据的X轴上绘制不平等间隔?

发布于 2025-02-05 21:15:51 字数 641 浏览 2 评论 0原文

我正在使用plotRix软件包使用颜色可视化数据中的更改。数据可用

library(plotrix)
my_colors1=c("red", "green","blue")
a<-read.csv("DataSt.csv")
x<-a$Year
y<-a$TP
clplot(x, y, main="",lwd=5,labels=y,levels=c(37,964,4377),col=my_colors1, showcuts=T, bty="n",xlab="Year", ylab = "numbers", axes=F)
axis(1, at = a$Year, las=2) 
axis(2, at = seq(0, 4400, by = 100), las=2)

我正在获取上图 “输出”

我想减少1975年至1989年之间的轴空间。请帮助我在X轴上获得不平等的间隔。

I am using plotrix package to visualize changes in the data using colors. The data is available here.
I am using below code for plotting the data.

library(plotrix)
my_colors1=c("red", "green","blue")
a<-read.csv("DataSt.csv")
x<-a$Year
y<-a$TP
clplot(x, y, main="",lwd=5,labels=y,levels=c(37,964,4377),col=my_colors1, showcuts=T, bty="n",xlab="Year", ylab = "numbers", axes=F)
axis(1, at = a$Year, las=2) 
axis(2, at = seq(0, 4400, by = 100), las=2)

I am getting the above chart Output

I want to reduce the axis space between the year 1975 and 1989. Please help me to get unequal interval at the x axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2025-02-12 21:15:51

这样做有点危险,即观众可能不会意识到X轴值之间的间距不一致。然而,以下示例通过将X值视为因子水平来显示可能的解决方案。问题在于,绘图函数仅允许数值值。因此,我绘制了因子的绘制,但是然后需要使用数值值在使用sepgments之间绘制某种插值值:

a <- structure(list(Year = c(2021L, 2020L, 2019L, 2018L, 2017L, 2016L, 
  2015L, 2014L, 2013L, 2012L, 2011L, 2010L, 2009L, 2008L, 2007L, 
  2006L, 2005L, 2004L, 2003L, 2002L, 2001L, 2000L, 1999L, 1998L, 
  1997L, 1996L, 1995L, 1994L, 1993L, 1992L, 1991L, 1990L, 1989L, 
  1975L), TP = c(785L, 848L, 1067L, 1079L, 1263L, 678L, 1204L, 
  542L, 661L, 387L, 3534L, 4377L, 964L, 244L, 237L, 145L, 86L, 
  37L, 39L, 23L, 14L, 11L, 7L, 9L, 6L, 3L, 7L, 7L, 6L, 1L, 1L, 
  1L, 2L, 1L)), class = "data.frame", row.names = c(NA, -34L))
a$Year <- factor(a$Year)
a <- a[order(a$Year),]
head(a)

my_colors1=c("red", "green","blue")

plot(TP ~ Year, a, col = NA, border = NA, las = 2)
for(i in 2:nrow(a)){
  b <- as.data.frame(approx(x = as.numeric(a$Year[(i-1):i]), y = a$TP[(i-1):i], n = 100))
  b$col <- my_colors1[as.numeric(cut(b$y, breaks = c(-Inf,37,964,4377,Inf)))]
  segments(x0 = b$x[-nrow(b)], x1 = b$x[-1], y0 = b$y[-nrow(b)], y1 = b$y[-1], col = b$col[-1])
}
abline(h = c(37,964), lty = 2)

“

It's a bit dangerous to do this give that the viewer might not realize the inconsistent spacing among the x-axis values. Nevertheless, the following example shows a possible solution by treating the x-values as factor levels. The problem is that that plotting function only allows numeric values. I thus plot with factors, but then need to use numeric values to plot some sort of interpolated values in between using segments:

a <- structure(list(Year = c(2021L, 2020L, 2019L, 2018L, 2017L, 2016L, 
  2015L, 2014L, 2013L, 2012L, 2011L, 2010L, 2009L, 2008L, 2007L, 
  2006L, 2005L, 2004L, 2003L, 2002L, 2001L, 2000L, 1999L, 1998L, 
  1997L, 1996L, 1995L, 1994L, 1993L, 1992L, 1991L, 1990L, 1989L, 
  1975L), TP = c(785L, 848L, 1067L, 1079L, 1263L, 678L, 1204L, 
  542L, 661L, 387L, 3534L, 4377L, 964L, 244L, 237L, 145L, 86L, 
  37L, 39L, 23L, 14L, 11L, 7L, 9L, 6L, 3L, 7L, 7L, 6L, 1L, 1L, 
  1L, 2L, 1L)), class = "data.frame", row.names = c(NA, -34L))
a$Year <- factor(a$Year)
a <- a[order(a$Year),]
head(a)

my_colors1=c("red", "green","blue")

plot(TP ~ Year, a, col = NA, border = NA, las = 2)
for(i in 2:nrow(a)){
  b <- as.data.frame(approx(x = as.numeric(a$Year[(i-1):i]), y = a$TP[(i-1):i], n = 100))
  b$col <- my_colors1[as.numeric(cut(b$y, breaks = c(-Inf,37,964,4377,Inf)))]
  segments(x0 = b$x[-nrow(b)], x1 = b$x[-1], y0 = b$y[-nrow(b)], y1 = b$y[-1], col = b$col[-1])
}
abline(h = c(37,964), lty = 2)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文