R ggplotly to Python plotly conversion issues (with beginnings of a possible fix)
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 18.8k
- 派生
- 2.8k
- 平均合并
- 16 小时 26 分钟
- 30 天内合并 PR
- 21
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先复现所提供的 R ggplotly 导出结果,并使用 Python 的 pio.read_json() 和 go.Figure() 导入它。阅读链接的 ggplotly_to_plotly.py gist,了解已识别的不兼容项。当兼容的 ggplotly JSON 能够在不使用 skip_invalid=True 的情况下导入,同时保留 layout.font.family 等字段时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python, r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 35/100