0

enter image description hereI have a plot which shows three time series lines (temperature, snowdays, raindays). Unfortunately the snowdays- and raindays-line is sort of interrupted by a datagap (~2000-2018). What I want to do now is to unshow or cut out the parts of the time series where the datagap is.

For example I got a dataframe including the number of snowdays for each hydrological year from 1980-2020 and I want to plot only the space of time from 1980 to 2000 and from 2018 to 2020. How can I do that?

Thats my code for the plot in the picture:

par(mar = c(5, 5, 3, 5))
plot(x=df$hydroyear, y=df$raindays, type ="l", lty= 1, ylab = "days 
[d]", ylim = c(15,70), lwd= 2,
xlab = "[Nov-Apr] per hydrological year",
col = "#04a2ca")
lines(x=df$hydroyear, y=df$snowdays, col= "#bcfaff", lty=1, lwd= 2)
par(new = TRUE)
plot(x=df_Temp$hydroyear, y=df_Temp$temperature, type = "l", xaxt = 
"n", yaxt = "n",
ylab = "", xlab = "", col = "black", lty = 2, lwd=1)
axis(side = 4)
mtext("temperature [°C]", side = 4, line = 3)
legend("topleft",
c("snowdays", "raindays","temperature"),
col = c("#bcfaff", "#04a2ca", "black"), lty = c(1,1,2), cex = .7, 
pt.cex = 2) code here

This is what my dataframe looks like: (the columns sli.avg.rain and sli.avg.snow are irrelavant for my plot)

structure(list(hydroyear = c(1975, 1976, 1977, 1978, 1979, 1980, 
1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 
1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2018, 
2019, 2020, 2021), number.of.raindays = c(37L, 33L, 45L, 33L, 40L, 
32L, 27L, 28L, 40L, 28L, 31L, 24L, 35L, 47L, 43L, 36L, 39L, 30L, 
40L, 64L, 61L, 29L, 38L, 64L, 44L, 39L, 52L, 39L, 41L, 58L, 38L
), number.of.snowdays = c(33L, 35L, 43L, 58L, 47L, 48L, 50L, 57L, 
38L, 44L, 44L, 49L, 47L, 30L, 24L, 14L, 45L, 27L, 27L, 34L, 38L, 
52L, 39L, 28L, 49L, 37L, 21L, 21L, 11L, 7L, 23L), number.of.snowraindays 
= c(31L, 
27L, 26L, 27L, 29L, 37L, 31L, 33L, 25L, 29L, 24L, 30L, 22L, 30L, 
30L, 31L, 16L, 33L, 20L, 33L, 31L, 23L, 28L, 21L, 25L, 40L, 31L, 
66L, 55L, 42L, 46L), sli.avg.snow = c(NA, NA, NA, NA, 43.2, 46.2, 
49.2, 52, 48, 47.4, 46.6, 46.4, 44.4, 42.8, 38.8, 32.8, 32, 28, 
27.4, 29.4, 34.2, 35.6, 38, 38.2, 41.2, 41, 34.8, 31.2, 27.8, 
19.4, 16.6), sli.avg.rain = c(NA, NA, NA, NA, 37.6, 36.6, 35.4, 32, 
33.4, 31, 30.8, 30.2, 31.6, 33, 36, 37, 40, 39, 37.6, 41.8, 46.8, 
44.8, 46.4, 51.2, 47.2, 42.8, 47.4, 47.6, 43, 45.8, 45.6)), row.names = 
c(NA, 
-31L), class = "data.frame")

Thanks!

a_zrhch
  • 17
  • 4
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 26 '21 at 18:09
  • We would need an idea of your dataframe. So write `dput(df)` in your console and post the result here. Thanks. – TarJae Jul 26 '21 at 18:33
  • There should not be a `...` in your data. That makes invalid code that we cannot copy/paste into R for testing. Edit your data into the question like with your code. No not put it in the comments. – MrFlick Jul 26 '21 at 18:53
  • Sorry, this is all quite new for me! Thanks for your patience! – a_zrhch Jul 26 '21 at 19:17

1 Answers1

1

The simplest way is to break your data frame into two and plot each separately. You did not provide the temperature data so this just plots rain and snow days:

df1 <- df[1:27, ]
df2 <- df[28:31, ]
dev.new(width=8, height=5)
plot(NA, xlab="Year", ylab="Days", xlim=c(1975, 2021), ylim=c(7, 64))
lines(number.of.raindays~hydroyear, df1, col="darkblue", lwd=2)
lines(number.of.raindays~hydroyear, df2, col="darkblue", lwd=2)
lines(number.of.snowdays~hydroyear, df1, col="lightblue", lwd=2)
lines(number.of.snowdays~hydroyear, df2, col="lightblue", lwd=2)

Plot with break

dcarlson
  • 10,936
  • 2
  • 15
  • 18