Convert R code into Python code using rpy2

from rpy2.robjects import r

ggplotRegression = r("""
    function (fit) {

        require(ggplot2)

        ggplot(fit$model,
               aes_string(x = names(fit$model)[2],
                          y = names(fit$model)[1])) + 
            geom_point() +
            stat_smooth(method = "lm", col = "red") +
            labs(title = paste("Adj R2 = ",
                               signif(summary(fit)$adj.r.squared, 5),
                               "Intercept =",
                               signif(fit$coef[[1]],5 ),
                               " Slope =",signif(fit$coef[[2]], 5),
                               " P =",signif(summary(fit)$coef[2,4],         
                               5)))
    }""")

With this you can call the function ggplotRegression in your Python code.

Leave a Comment