1

I use ggplot2 to create a point plot and then I add the correlation coefficient to that chart. Next, I use plotly to see info about each data point. However, there is a mistake in font style in the plot as shown here.

I need R = 0.87 and P = 2.2e-16, not "italic(R)" or "italic(P)", while keeping mapping part in stat_cor. I guess, plotly cannot understand italic(p) part as a code. The solution should not fix that adding a text manually, I need the calculation of "R" and "P".

Here is the code:

p1 <- ggplot(iris) +
      geom_point(aes(Sepal.Length, Petal.Length)) +
      stat_cor(mapping = aes(Sepal.Length, Petal.Length))
p2 <- ggplotly(p1)
p2
Fernando Barbosa
  • 853
  • 1
  • 8
  • 24
Arda Askin
  • 51
  • 5
  • Would using ['ggiraph'](https://davidgohel.github.io/ggiraph/) instead of 'plotly' be an option for you? It works far better with 'ggplot' and has the same interactive features. – TimTeaFan Dec 10 '22 at 21:07

1 Answers1

0

You can add anotations to the plotly chart - any sort of R functions and html code are going to work as part of the text.

Plotly only solution

A possible solution is using plotly directly, not using ggplot then converting.

The code would be:

p2 <- plot_ly(data = iris, x=~Sepal.Length, y = ~Petal.Length) |>   #base R pipe operator
  add_annotations(
  xref = "paper", yref = "paper",
  x = 0.1, y = 0.9, 
  text = paste0("<i>R</i> = ", round(cor(iris$Sepal.Length, iris$Petal.Length),2), "<br>",
            "<i>P</i> = ", formatC(cor.test(iris$Sepal.Length, iris$Petal.Length)$p.value,
                                       format="e", digits=2)),
  showarrow = F,   # Does not show an arrow indicating the text position
  align = "left")  #align the text left
p2
  • "paper" defines how x and y applies (relative to the axis (paper) or on specific value)
  • x = 0.1, y = 0.9 says that the text will be placed at the 10% of the x-axis and 90% of the y-axis.
  • text is the text itself. I am using basic functions to calculate R and p-value, and html symbols to edit the text.

Alternative using ggplotly

As you prefer to use ggplotly, the exact same annotation can be used on it. In this case, the code is:

p1 <- ggplot(iris) + geom_point(aes(Sepal.Length, Petal.Length))
p2 <- ggplotly(p1) |>
      add_annotations(
        xref = "paper", yref = "paper",
        x = 0.01, y = 0.95, 
        text = paste0("<i>R</i> = ", round(cor(iris$Sepal.Length, iris$Petal.Length),2),
                      "<br>",
                      "<i>P</i> = ", formatC(cor.test(iris$Sepal.Length, iris$Petal.Length)$p.value,
                                            format="e", digits=2)),
      showarrow = F,   # Does not show an arrow indicating the text position
      align = "left")  #align the text left
p2
Fernando Barbosa
  • 853
  • 1
  • 8
  • 24
  • I can't thank you enough. It's working, but it is difficult to add facet in plotly for shiny app. – Arda Askin Dec 11 '22 at 10:09
  • Got it, I added a solution using ggplotly, then. Hope it works for you. If you think it is a good solution, please accept it: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Fernando Barbosa Dec 11 '22 at 14:05