Skip to content

Not able to show Regression Line Equation using ggplotly #1687

Closed

Description

@romabhu

I need to display Regression Line Equation with the lineplot. I am using "stat_poly_eq" function present in "ggpmisc" package. Able to achieve it with ggplot (Find attached screenshot). But when when I am trying to show plot using ggplotly regression equition is getting disppeared (Find attached screenshot)

Below is the sample code, I am using:

library(ggplot2)
library(ggpmisc)
library(plotly)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
print(p)
ggplotly(p)

Please suggest probable solution with which i can achieve this. It is really urgent for me to show quation using ggplotly.
ggplotly_screenshot
ggplot_screenshot

Activity

  1. cpsievert commented on Feb 4, 2020

    @cpsievert
    Collaborator

    Added to #566

  2. aphalo commented on Jun 24, 2023

    @aphalo

    Layer function stat_poly_equation() is flexible enough to work with 'ggplotly' if some defaults are overriden. One needs to be careful about the positioning of the text labels as some manual tweaking is required.

    library(ggplot2)
    library(ggpmisc)
    library(plotly)
    df <- data.frame(x = c(1:100))
    df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)

    The geom used by default is geom_text_npc() from 'ggpp', which is unknown to 'ggplotly', so we need to use geom_text() instead. 'ggplotly' does not seem to understand character strings that are meant to be parsed into expressions or even respect justification in geom_text(). However, there are workarounds available for all these limitations. The pure-text labels lack italics and super- and subscripts, as stat_poly_eq() currently uses the ASCII character set for equations formatted with output.type = "text".

    my.formula <- y ~ x
    p <-
      ggplot(data = df, aes(x = x, y = y)) +
      geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
      stat_poly_eq(geom = "text", # override default "text_npc"
                           output.type = "text",  # override default "expression"
                           formula = my.formula,
                           aes(label = paste(..eq.label.., ..rr.label.., sep = ",  ")), # use for sep plain text
                           label.x = 20) + # override default "left"
      geom_point()
    
    ggplotly(p)

    This PNG is downloaded from the plotly plot.

    newplot

  3. aphalo commented on Jun 24, 2023

    @aphalo

    @cpsievert I think the best way to enhance 'ggplotly' would be to implement support for geom_richtext() from package 'ggtext'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions