plotly / plotly/plotly.R

Ggplotly does not always make a legend correctly for where there are points and lines

Open
#1,816 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Ggplotly does not always make a legend correctly for where there are points and lines. Sometimes it only makes the legend key based on either geom_point or geom_line, whichever is the first layer. Below is an example where it doesn't work. It seems to fix it if the data is arranged by the x variable. It'd be good if it worked for however the data was arranged.

library(dplyr)
library(ggplot2)
library(plotly)

#This doesn't work: the legend is based only on the first geom shape
plot_data <- iris %>% 
  as_tibble() %>%  
  janitor::clean_names() 

plot <- ggplot(data = plot_data, aes(petal_width, petal_length, col = species)) +
  geom_point() +
  geom_line()

plotly::ggplotly(plot)

image

plot_data <- iris %>% 
  as_tibble() %>%  
  janitor::clean_names() %>% 
  arrange(petal_width)

plot <- ggplot(data = plot_data, aes(petal_width, petal_length, col = species)) +
  geom_point() +
  geom_line()

plotly::ggplotly(plot)

image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied R example with plotly::ggplotly, comparing the unsorted iris data with the version arranged by petal_width. The issue is done when the legend correctly represents both point and line layers regardless of the data row order.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.