Hello,
I have some plots that created by ggplot library in R, and I use the ggplotly function to convert it into ggplot objects.
Is there is a way to save these objects as a json file, so I could open then later using plotly module in python (I want to build a dash app in python and use that plots).
So far I tried this code in order to generate the json in R:
library(ggplot2)
library(plotly)
library(jsonlite)
df <- read.csv("table.csv")
plot <- ggplot(df) + geom_point(mapping = aes(x = X, y = Y, colour = Co, text = X))
plot_ly <- ggplotly(plot)
plotly_json <- toJSON(plot_ly, pretty = TRUE)
json_file <- "test_plotly.json"
writeLines(as.character(plotly_json), json_file)
And this code in python order to open it
import plotly.graph_objects as go
import json
# Load the JSON file
json_file = "test_plotly.json"
with open(json_file, "r") as file:
plotly_json = json.load(file)
# Create a plotly figure from the loaded JSON
loaded_plot = go.Figure(plotly_json)
loaded_plot.show()
But it didn’t manage to load the json file. Thank you so much!