Home > database >  How do I get rid of these legend numbers in ggplotly?
How do I get rid of these legend numbers in ggplotly?

Time:03-01

Problem

I am having issues with ggplotly generating random numbers with the legend of the plot. Details are below.

Libraries

I'm only using two libraries to demonstrate the issue:

# Libraries:
library(plotly)
library(ggpubr)

Simulated Data

To take out the random issues of some complex data, I've created the following dataframe:

# Simulated data:
slack_frame <- data.frame(x = rnorm(100, 5, 2),
                          y = rnorm(100, 10, 4),
                          z = rep(x=c("Boy", "Girl"), 100))

Bare Bones Plot:

Now if I just make a normal scatterplot, it gives me basically what I want: an interactive version of the base ggpubr plot I made:

# Scatter (Basic):
slack_scatter <- ggscatter(slack_frame,
          x="x",
          y="y",
          color = "z",
          title = "Simulated Scatterplot")
ggplotly(slack_scatter)

Seen here:

enter image description here

Adding palette:

However, if I add a color palette, I always run into an issue. Here is the code for the palette of colors used, "jco":

# Scatter:
slack_scatter_jco <- ggscatter(slack_frame,
                           x="x",
                           y="y",
                           color = "z",
                           palette = "jco",
                           title = "Simulated Scatterplot")

# Plotly scatter:
ggplotly(slack_scatter_jco)

It now gives me a legend that has these annoying numbers on the right:

enter image description here

I never run into the same issue with regular ggplot used with ggplotly:

# GGPLOT2 scatter:
base_gg <- ggplot(slack_frame,
                  aes(x=x,
                      y=y,
                      color=z)) 
  geom_point() 
  labs(title = "Simulated Scatterplot") 
  scale_color_grey()
ggplotly(base_gg)

enter image description here

Why do these numbers keep showing up and how can I get rid of them?

CodePudding user response:

It seems like its a bug in the intersection between the palette-argument (and ggpubr::set_palette("jco")) and ggplotly. A fix is to go the direct way:

slack_scatter_jco <- ggpubr::ggscatter(slack_frame,
                               x="x",
                               y="y",
                               color = "z",
                               title = "Simulated Scatterplot")  
                     ggpubr:::.ggcolor("jco")

# Plotly scatter:
plotly::ggplotly(slack_scatter_jco)
  • Related