0

I have a bar chart that I want to make interactive in my R Shiny app using plotly. When it is rendered as a plot, everything is fine using ggplot:

#Bar plots
ggplot(data = df_continents) +
  geom_bar(aes(x=country, y=coal_co2), stat="identity", fill="#2596be") +
  scale_y_continuous(labels = number_format())`

Bar chart using

But, when I try to use plotly, the format gets messed up in an incredible way:

#Bar chart - Adding Labels
ticklabels <- seq(from=0, to=round(max(df$coal_co2*100000)), by=100000)
  ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))

  output$bar <- renderPlotly({
    df %>%
      plot_ly(x =~ country, y = ~ coal_co2, type = "bar", marker = list(color = "#2596be")) %>%
      layout(yaxis=list(tickvals = ticklabels,
                        ticktext = ticktexts
      ))
  })

Bar chart from  shiny Dashboard

So, I'm not sure what is the problem here, why do I have those white horizontal lines inside the bars?. How do I get my bar chart plot to look like the first screenshot?.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Try using the mean value of `y` grouped by the countries. It should make the lines go. – gurezende Jan 31 '23 at 21:02
  • In your ggplot chart you are using `df_continents` whereas in your plotly chart you use `df`. And from the images I would guess that the first contains aggregated values per continent while the latter still contains the values per country, i.e. the values get stacked which results in the lines. – stefan Jan 31 '23 at 21:10

0 Answers0