I took the example of geom_ribbon
from this page . Result looked good with ggplot
but was messed up when converting to plotly
using ggplotly
:
2 legend titles were pushed to the top.
legend keys had unwanted trailing text ",1)"
at the end.
I was wondering if it is possible to fix these issues? Thank you
library(ggplot2)
library(plotly)
set.seed(1)
x <- seq(1, 10, length = 100)
data <- data.frame(x, dnorm(x, mean = 6.5, sd = 1))
names(data) <- c("x", "new.data")
x.ribbon <- seq(1, 10, length = 20)
ribbon <- data.frame(
x.ribbon,
dnorm(x.ribbon, mean = 5, sd = 1) + .01,
dnorm(x.ribbon, mean = 5, sd = 1) - .01,
dnorm(x.ribbon, mean = 5, sd = 1)
)
names(ribbon) <- c("x.ribbon", "max", "min", "avg")
ggplot
:
p <- ggplot() +
geom_ribbon(data = ribbon, aes(ymin = min, ymax = max, x = x.ribbon, fill = "lightgreen")) +
geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = "black")) +
geom_line(data = data, aes(x = x, y = new.data, color = "red")) +
xlab("x") +
ylab("density") +
scale_colour_manual(
name = "Colour",
values = c("black" = "black", "red" = "red")
) +
scale_fill_manual(
name = "Ribbon",
values = c("lightgreen" = "lightgreen")
)
p
plotly
:
fig <- ggplotly(p)
fig
The rendering seems to differ in the RStudio IDE by viewport. See this post . If the problem persists elsewhere (shiny
?), come back.
Thank you. I am seeing the same problem with a shiny app displaying on browser though.
library(ggplot2)
library(plotly)
library(shiny)
# Global variables
n <- 1
# Define the UI
ui <- bootstrapPage(
numericInput('n', 'Set seed', n),
plotlyOutput('plot')
)
# Define the server code
server <- function(input, output) {
output$plot <- renderPlotly({
set.seed(input$n)
x <- seq(1, 10, length = 100)
data <- data.frame(x, dnorm(x, mean = 6.5, sd = 1))
names(data) <- c("x", "new.data")
x.ribbon <- seq(1, 10, length = 20)
ribbon <- data.frame(
x.ribbon,
dnorm(x.ribbon, mean = 5, sd = 1) + .01,
dnorm(x.ribbon, mean = 5, sd = 1) - .01,
dnorm(x.ribbon, mean = 5, sd = 1)
)
names(ribbon) <- c("x.ribbon", "max", "min", "avg")
p <- ggplot() +
geom_ribbon(data = ribbon, aes(ymin = min, ymax = max, x = x.ribbon, fill = "lightgreen")) +
geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = "black")) +
geom_line(data = data, aes(x = x, y = new.data, color = "red")) +
xlab("x") +
ylab("density") +
scale_colour_manual(
name = "Colour",
values = c("black" = "black", "red" = "red")
) +
scale_fill_manual(
name = "Ribbon",
values = c("lightgreen" = "lightgreen")
)
fig <- ggplotly(p)
fig
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
Thanks for the reprex
, which shows this clearly. The next step, which I'll try shortly, is rendering to an appropriate HTML file. If it works there that points to shiny
and RStudio
's view engine.
The object returned by ggplotly
, fit
is a deeply nested list. str(fig)
will show you where the string literals causing the problem come from, but I was unable to find a way to override this. This question might attract more answers on the repo issues page .
Same issue with Rmd
file
---
title: "Plotly Ribbon"
author: "ggplotly"
date: "May 2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## ggplot2
```{r ggplt, echo=FALSE}
library(ggplot2)
library(plotly)
set.seed(1)
x <- seq(1, 10, length = 100)
data <- data.frame(x, dnorm(x, mean = 6.5, sd = 1))
names(data) <- c("x", "new.data")
x.ribbon <- seq(1, 10, length = 20)
ribbon <- data.frame(
x.ribbon,
dnorm(x.ribbon, mean = 5, sd = 1) + .01,
dnorm(x.ribbon, mean = 5, sd = 1) - .01,
dnorm(x.ribbon, mean = 5, sd = 1)
)
names(ribbon) <- c("x.ribbon", "max", "min", "avg")
p <- ggplot() +
geom_ribbon(data = ribbon, aes(ymin = min, ymax = max, x = x.ribbon, fill = "lightgreen")) +
geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = "black")) +
geom_line(data = data, aes(x = x, y = new.data, color = "red")) +
xlab("x") +
ylab("density") +
scale_colour_manual(
name = "Colour",
values = c("black" = "black", "red" = "red")
) +
scale_fill_manual(
name = "Ribbon",
values = c("lightgreen" = "lightgreen")
)
p
```
## Plotly
```{r plotly, echo=FALSE}
fig <- ggplotly(p)
fig
```
Try code that constructs plotly the plotly way.
plot_ly(type = "scatter",
mode = "lines") %>%
add_ribbons(
data = ribbon,
x = ~x.ribbon,
ymin = ~min,
ymax = ~max,
color = I("green"),
name = "Ribbon"
) %>%
add_trace(
x = ~x.ribbon,
y = ~avg,
color = I("black"),
name = "Black"
) %>%
add_trace(
data = data,
x = ~x,
y = ~new.data,
name = "Red",
color = I("red")
) %>%
layout(
xaxis = list(title = "x"),
yaxis = list(title = "density"),
legend = list(x = 1,
y = .5,
title=list(text="<b>Ribbon Color</b>"))
)
1 Like
system
Closed
May 27, 2021, 5:47am
8
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.