Need help to plot precipitation anomalies

Hello,

I need some help, I want to plot precipitation anomalies. I already have a (vector) of monthly values of anomalies (positives and negatives), but I don't know how to plot them as a histogram (x-axis=dates and y-axis=values).
Month Values
1 -36.45351166
2 7.38889555
3 -7.50222414
4 6.67311712
5 62.35480802
6 22.16203958
7 0.79312881
8 11.92063581
9 38.90699358
10 -25.27711429
11 -22.19690044
12 -2.36755113
13 -11.25805478
14 3.58435243
15 -23.30676725
16 -1.13142600
17 -20.44973509
18 -14.64250353
19 -0.01141430
20 2.11609269
21 16.10245047
22 -5.08165741
23 55.99855644
24 -17.17209425

I used this command:

library(timetk)
plot_anomaly_diagnostics(resi_data, .date_var = "time", .value = "value")

But got this result:

frequency = 12 observations per 1 year
trend = 60 observations per 5 years
Error in stats::stl(., s.window = "periodic", t.window = trnd, robust = TRUE) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In storage.mode(x) <- "double" : NAs introduced by coercion

Is there a simple way to do it?

What you're describing is not a histogram. A histogram would be x-axis = range of values, y-axis = counts within bins of values.

Try these

with(df, plot(Month, Values))
with(df, hist(Values))

Yes, you are right, it is not actually a histogram, but I want to plot it as this picture:

anomalies

But the method you gave me is more like a histogram with positive values to the right and negative to the left.

library(tidyverse)

df <- expand_grid(Year = 2010:2020, Month = 1:12) %>%
  mutate(Value = runif(nrow(.))) 
df
#> # A tibble: 132 x 3
#>     Year Month  Value
#>    <int> <int>  <dbl>
#>  1  2010     1 0.330 
#>  2  2010     2 0.657 
#>  3  2010     3 0.0239
#>  4  2010     4 0.537 
#>  5  2010     5 0.644 
#>  6  2010     6 0.227 
#>  7  2010     7 0.991 
#>  8  2010     8 0.230 
#>  9  2010     9 0.982 
#> 10  2010    10 0.0535
#> # ... with 122 more rows

dfs <- df %>%
  group_by(Year) %>%
  summarize_at(vars(Value), c("minval" = min, "meanval" = mean, "maxval" = max)) 
dfs
#> # A tibble: 11 x 4
#>     Year  minval meanval maxval
#>  * <int>   <dbl>   <dbl>  <dbl>
#>  1  2010 0.0239    0.478  0.991
#>  2  2011 0.113     0.480  0.896
#>  3  2012 0.00223   0.401  0.872
#>  4  2013 0.0650    0.476  0.962
#>  5  2014 0.118     0.576  0.920
#>  6  2015 0.0463    0.457  0.913
#>  7  2016 0.0561    0.389  0.884
#>  8  2017 0.0361    0.482  0.939
#>  9  2018 0.00881   0.444  0.845
#> 10  2019 0.0433    0.466  0.968
#> 11  2020 0.0658    0.435  0.976

dfs %>%
  ggplot() + 
  geom_crossbar(aes(x = Year, y = meanval, ymin = minval, ymax = maxval), 
                fill = "darkgrey",
                color = "darkgrey") +
  scale_x_continuous(breaks = 2010:2020)

Created on 2021-06-23 by the reprex package (v1.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.