1

I have been using a code to summarize some Linear Regression Results in a graph (Both code and graph are attached). I adopted the code referring to various sources from here.

Now i want to make the exact same graph for Mann-Kendall test Results. And in place of "slope" section of my graph i want to insert the result from "Sen slope estimation". So i want result from two different test to show in this graph.

I thing the problem is in "Paste" section of "geom(label)". Because i have to identify the particular numeric result that is to be pasted in my graph which i dont know how to do.

If any body have a solution to this problem. I will greatly appreciate it.

ggplotRegressionxpl <- function (fit) {
 require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) + 
geom_point() + geom_line() + geom_label(aes(2000, 33, hjust = 0, vjust = 0, label = paste("R^2 = ",signif(summary(fit)$adj.r.squared, 3),"\n",
                                                                                          "Slope =",signif(fit$coef[[2]], 3),"\n",
                                                                                          "p-value =",signif(summary(fit)$coef[2,4], 3)))) +
stat_smooth(method = "lm", col = "red") + xlab("Year") + ylab("Total Precipitation") +
 labs(title = "Pullman (1941–2018)") + scale_y_continuous(limits = c(0, 40)) +
theme(plot.title = element_text(hjust = 0.5))



}

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
Samrat
  • 79
  • 1
  • 10
  • So your question is how to put the output of a test in a graph as text, or is there some peculiarity about this test that I may have missed? I have no clue what the results of your test look like in terms of data structure, but there is a good chance that you can use `str(your_test_results)` to find out where the particular information you need is stored. – teunbrand May 19 '19 at 20:32
  • @Samrat: Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 20 '19 at 03:02
  • Check out ggstatsplot. It might have what you're looking for https://indrajeetpatil.github.io/ggstatsplot/ – Tung May 20 '19 at 03:08
  • And this Daily Streamflow Trend Analysis https://owi.usgs.gov/blog/Quantile-Kendall/ – Tung May 20 '19 at 03:10
  • This might be useful too "Environmental Flows for the Huron River System" https://www.hrwc.org/wp-content/uploads/2014/06/Full-Report-Huron-Env-Flows.pdf – Tung May 20 '19 at 03:54
  • @Tung Hello sir, Thank you so much for all the resources that you have pointed out to. As I am new to this site i have little knowledge about the rules and regulations of posting the questions> I realize that it would be more helpful with the dataset but i am not able to edit the question anymore otherwise i would have given the dataset. – Samrat May 20 '19 at 17:57
  • @teunbrand THANK YOU! that is exactly what i needed. Now that i know what name those values were stored under. I was just able to replace those names and was able to make the graphs like i wanted. – Samrat May 20 '19 at 18:33

1 Answers1

1

I've used Mann-Kendall / Sens a number of years ago. The package I used was wql.

You can run the mannKen function over your data result <- mannKen(data$Precipitation). Once you have done that, you can access the Sens Slope and p-value using result $sen.slope and result$p.value.

To graph the trend line, I usually pick a point to fix the line - e.g. the median concentration in my timeseries coupled with the middle point of the timeseries. This gives me a reference point of (middleYear, median) - (1978, medianPrecipitation) in your graph above.

I then use the sens slope, the start point of the timeseries (1941 in your example above), the end point of the data (2019 in your example), and some linear algebra to calculate the start point of the line (Precipitation in 1941 if you extended the sens slope back from the reference point) and the end point of the line (Precipitation in 2019 if you extended the sens slope forward from the reference point).

I'm interested to hear if there are any other packages that would simplify this process.

olorcain
  • 1,230
  • 1
  • 9
  • 12
  • Thank you for your answer. Your idea will still be helpful to me to make the graph of the test. But i am more interested in directly showing my test statistics of Result of mann kendall test in the graph inside the box. Like i have done it in the graph above for Regression. – Samrat May 20 '19 at 16:16
  • You're welcome. As I say, I used the wql package. It's easy enough to pull the statistics from the results using e.g. result$sen.slope to access the slope statistic. It's hard to be more specific without data as @tung points out in his comment above. – olorcain May 20 '19 at 16:19
  • I realize now that it would be more easy to visualize my question with my data. But i am not sure if i can edit the question to include the data anymore. I am currently using a package called "Trend" and "Kendall" to carry out the two test i want. I am wondering, when i use result$sen.slope in the package you mentioned, will i be able to directly output the result inside the graph or will it just show what the result is as a output? – Samrat May 20 '19 at 18:02
  • Yes, you shoud be able to add it by updating your geom_label code. Something like: geom_label(aes(2000, 33, hjust = 0, vjust = 0, label = paste("R^2 = ",signif(summary(fit)$adj.r.squared, 3),"\n", "Slope =",signif(fit$coef[[2]], 3),"\n", "p-value =",signif(summary(fit)$coef[2,4], 3), "\n", "Sens Slope =",result$sen.slope))) – olorcain May 21 '19 at 09:19
  • thank you so much for your help. this is very helpful – Samrat May 22 '19 at 18:19