简体   繁体   中英

Using ggplot to Summarize Mann Kendall test results in a graph

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))



}

在此处输入图片说明

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 - eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM