Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Plotly in R Shiny

I am trying to develop shiny app which renders plot using plotly. I am able to get the plot using an example for plotly in shiny. But my graph does not look good. I am using ggplot2 for ploting the graph and then converting it to plotly using gg2list() and stored in a variable gg.
` output$trendPlot <- renderGraph({

xaxislimits=as.character(sort(unique(as.numeric(subdata4()$price_point)),decreasing = FALSE))

p <-  ggplot(data=subdata4(), aes(x=price_point, y=Price, fill=Seller))    +
  geom_bar(stat="identity",position=position_dodge() ) +
  xlab("Number of Units Sold") + ylab("Selling Price ")
 

gg <- gg2list(p)

# make edits with plotly syntax
gg$layout <- list(legend = list(
  x = 1,
  y = 1,
  bgcolor = "transparent"))

# Send this message up to the browser client, which will get fed through to
# Plotly's javascript graphing library embedded inside the graph
return(list(
  list(
    id = "trendPlot",
    task = "newPlot",
    data = gg$data,
    layout= gg$layout
  )
))

})

})`

But the plot is not good. And i dont understand whether to plot using ggplot2 then convert to plotly or us plot_ly() directly?

Kindly help me!

Any help will be appreciated.

Hey, can you be more specific about the issue with your plot? Perhaps you can attach an image of what you’re referring to.

Hey Kevin! Regret for the delay reply. I have attached the plotly image . As you can see i need to fix the scale of the x axis,labe of both the axis , the width of the bars and i also need to increase the width of the plot on the page.

Kindly help!

Does your plot render correctly if you’re just using ggplot ?

Yes the plot is correct! But the bars are really terrifying! Have to look into the the ranges of x-axis too!

Have you tried creating a plot using either ggplotly() or plot_ly() and then rendering the output with renderPlotly rather than renderGraph?
Here are some examples: https://plot.ly/r/shiny-tutorial/

I am able to plot the graph using plot_ly() , but can you tell me what can be the attribute for position_dodge in ggplot2 bar graph in plot_ly??