plotly / plotly/plotly.py

R ggplotly to Python plotly conversion issues (with beginnings of a possible fix)

Ouverte
#4,410 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

community feature P2
Langage dominant
Python
Étoiles
18.8k
Forks
2.8k
Merge moyen
16 h 26 min
PR mergées (30 j)
21

Description

Hello Plotly team! I want to make graphs in R’s ggplotly and then run existing Plotly Python code to reformat them. However, I’m getting errors when importing a ggplotly JSON created in R into Plotly in Python.

To fix these errors, I have to resort to either running code to fix them in the JSON (2a below) or setting pio.read_json()’s skip_invalid parameter to true (2b below). , I believe the JSON is valid because 1) The JSON renders correctly in Plotly.js 2) The JSON comes from simple graphs made in ggplotly.

R code:

library(ggplot2)
library(plotly)

# Data
a = data.frame(x = (0:4), y = c(2, 4, 1, 6, 8), group="A")
b = data.frame(x = (0:4), y = c(3, 2, 5, 4, 7), group="B")
data <- rbind(a, b)

# Graph 
fig <- ggplot(data, aes(x, y, color = group)) + geom_line()

#Convert to ggplotly, save as JSON, and export
fig_ggplotly <- ggplotly(fig)
fig_ggplotly_json<-plotly_json(fig_ggplotly, FALSE)
write(fig_ggplotly_json,'fig_ggplotly.json')

2a: Python code - Errors when using json.load( )

import json
import sys

with open(“fig_ggplotly.json", "r") as f:
    fig_ggplotly_json = json.load(f)
print(fig_ggplotly_json)

go.Figure(fig_ggplotly_json) # throws errors; solved with ggplotly_to_plotly.py 

 # Example of invalid parameter:  
fig_ggplotly_json['layout']['font']['family'] # shows value as ‘’. Does not throw error.

2b: Python code - Using pio.read_json() with skip_invalid=True

import json
import sys
import plotly.io as pio

with open("fig_ggplotly.json", "r") as f:
    fig_ggplotly_pio = pio.read_json(f, skip_invalid=True)

# Example of invalid parameter that gets completely eliminated using skip_invalid, causing later issues:   
fig_ggplotly_pio.to_dict()['layout']['font']['family'] #throws KeyError

Skipping elements of the JSON seems to be inviting a loss of fidelity. Indeed, the reformatting code I apply has issues handling some elements due to the skipped invalid parameters. I’ve identified a number of specific elements that are not compatible with Plotly.py and would appreciate it if Plotly.py were to handle this valid input correctly. The ggplotly_to_plotly conversion code, which highlights many of the problem areas for basic graphs, can be found here.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire l’export R ggplotly fourni et l’importer avec pio.read_json() et go.Figure() de Python. Consultez le gist lié ggplotly_to_plotly.py pour connaître les incompatibilités identifiées. Le travail est terminé lorsque les JSON ggplotly compatibles sont importés sans skip_invalid=True tout en préservant des champs tels que layout.font.family.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python, r
Domaine
data-visualization
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.