简体   繁体   English

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

[英]How to plot Unequal Interval on x axis of a time series data?

I am using plotrix package to visualize changes in the data using colors.我正在使用plotrix包来使用颜色可视化数据的变化。 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我得到了上面的图表输出

I want to reduce the axis space between the year 1975 and 1989. Please help me to get unequal interval at the x axis.我想减少 1975 年和 1989 年之间的轴空间。请帮我在 x 轴上获得不等的间隔。

It's a bit dangerous to do this give that the viewer might not realize the inconsistent spacing among the x-axis values.这样做有点危险,因为观看者可能没有意识到 x 轴值之间的间距不一致。 Nevertheless, the following example shows a possible solution by treating the x-values as factor levels.然而,以下示例显示了将 x 值视为因子水平的可能解决方案。 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 :因此,我使用因子进行绘图,但随后需要使用数值在使用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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅在没有日期的x轴上绘制时间间隔? - How to plot Time Interval only on x axis without date? 如何在 r 中的 plot 每周时间序列数据在 r 或 ggplot 中的 x 轴上显示日期? - how to plot weekly time series data in r show the date in the x axis in r or ggplot? R-为时间序列绘制法线X轴 - R - plot normal X axis for time series 多时间序列图中的标签X轴 - Label X Axis in Multiple Time Series Plot 在1个面板中绘制多个时间序列数据时如何自定义轴? - How to customize axis when plot multiple time series data in 1 panel? 如何在 ggplot2 中用 x 轴上的时间间隔和 y 轴上的值绘制折线图? - How to plot a line graph in ggplot2 with With time interval on x axis and values on y axis? ggplot:如何使时间序列图的x /时间轴只是时间分量,而不是日期? - ggplot: How to make the x/time-axis of a time-series plot only the time-component, not the date? 时间序列中多元回归的残差图,时间在 R 中的 X 轴上 - Residual Plot for multivariate regression in Time Series, with time on X axis in R 如何在 R 中的 ggplot2 中自定义并将离散时间步添加到时间序列中的 x 轴 plot? - How to customize and add discrete time steps to x-axis in time series plot in ggplot2 in R? ts.plot()未针对自定义x轴绘制时间序列数据 - ts.plot() not plotting Time Series data against custom x-axis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM