1

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.

mohitji
  • 129
  • 1
  • 10

1 Answers1

1

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

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Thank you Marc! How to label selective values on the plot? Also I am using lwd=5, the width of the line remain same – mohitji Jun 09 '22 at 04:25
  • @mohitji - Glad it helped. What do you mean by the "selective values on the plot"? Labeling the dashed lines? see `text()` perhaps. – Marc in the box Jun 09 '22 at 06:03
  • I want to add label 37, 964,4377,3534, on the plotted line. I am using text as like this `selected <- c(3,4,5,7,12) text(x[selected], y[selected], labels = y[selected], cex = 0.7,pch=1, pos = 3, col = "black")` – mohitji Jun 09 '22 at 07:17