✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Categorical labels disappear when calling plotly::api_create()

Hi there,

I’m trying to create a plotly graphic from a ggplot2 object.

When I call plotly::ggplotly() on the ggplot2 object within my local R session, the categorical labels for the (flipped) x-axis values are displayed. However, if I call plotly::api_create() and display the plot in the browser, the categorical labels for the (flipped) x-axis values just disappear.

I’m new to plotly, so I couldn’t figure out what the problem is. Below you find the corresponding R code that creates the plot (automatically generated by the plotly webpage). I can add further information if necessary. For now, I hope that the code below provides enough detail about my problem.

Here’s the plot: https://plot.ly/~BiegT/1.embed

Thanks a lot, guys!

library(plotly)
trace1 <- list(
  x = c(76, 80, 97, 108, 147, 194, 196, 224, 260, 274, 750, 1851, 1887, 2560, 3493, 5174, 6558), 
  y = c(2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 17, 21, 22, 23, 25, 26, 27), 
  base = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
  basesrc = "BiegT:0:548546", 
  hoverinfo = "text", 
  legendgroup = "Extern", 
  marker = list(
autocolorscale = FALSE, 
color = "rgba(248,118,109,1)", 
line = list(
  color = "transparent", 
  width = 1.88976377953
)
  ), 
  name = "Extern", 
  orientation = "h", 
  showlegend = TRUE, 
  text = c("Kategorie: BILLA Onlineshop<br />Anzahl:   76<br />Bereich: Extern", "Kategorie: Apo-App<br />Anzahl:   80<br />Bereich: Extern", "Kategorie: Skype<br />Anzahl:   97<br />Bereich: Extern", "Kategorie: Amazon<br />Anzahl:  108<br />Bereich: Extern", "Kategorie: Kindle<br />Anzahl:  147<br />Bereich: Extern", "Kategorie: Willhaben<br />Anzahl:  194<br />Bereich: Extern", "Kategorie: ORF TVthek<br />Anzahl:  196<br />Bereich: Extern", "Kategorie: Qando<br />Anzahl:  224<br />Bereich: Extern", "Kategorie: ÖBB App<br />Anzahl:  260<br />Bereich: Extern", "Kategorie: Youtube<br />Anzahl:  274<br />Bereich: Extern", "Kategorie: Sudoku<br />Anzahl:  750<br />Bereich: Extern", "Kategorie: WetterOnline<br />Anzahl: 1851<br />Bereich: Extern", "Kategorie: Mahjong<br />Anzahl: 1887<br />Bereich: Extern", "Kategorie: WhatsApp<br />Anzahl: 2560<br />Bereich: Extern", "Kategorie: E-Mail<br />Anzahl: 3493<br />Bereich: Extern", "Kategorie: Internet<br />Anzahl: 5174<br />Bereich: Extern", "Kategorie: Solitaire<br />Anzahl: 6558<br />Bereich: Extern"), 
  textsrc = "BiegT:0:10c8c8", 
  type = "bar", 
  width = c(0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9), 
  widthsrc = "BiegT:0:04d739", 
  xaxis = "x", 
  xsrc = "BiegT:0:8ac989", 
  yaxis = "y", 
  ysrc = "BiegT:0:d1ef5d"
)
trace2 <- list(
  x = c(74, 218, 390, 674, 701, 711, 775, 846, 914, 2903), 
  y = c(1, 9, 13, 14, 15, 16, 18, 19, 20, 24), 
  base = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
  basesrc = "BiegT:0:1ba340", 
  hoverinfo = "text", 
  legendgroup = "Intern", 
  marker = list(
autocolorscale = FALSE, 
color = "rgba(0,191,196,1)", 
line = list(
  color = "transparent", 
  width = 1.88976377953
)
  ), 
  name = "Intern", 
  orientation = "h", 
  showlegend = TRUE, 
  text = c("Kategorie: Handbuch<br />Anzahl:   74<br />Bereich: Intern", "Kategorie: Gesundheitsdienste<br />Anzahl:  218<br />Bereich: Intern", "Kategorie: Einstellungen<br />Anzahl:  390<br />Bereich: Intern", "Kategorie: Photocloud<br />Anzahl:  674<br />Bereich: Intern", "Kategorie: Mobilität<br />Anzahl:  701<br />Bereich: Intern", "Kategorie: Messaging<br />Anzahl:  711<br />Bereich: Intern", "Kategorie: Veranstaltungen<br />Anzahl:  775<br />Bereich: Intern", "Kategorie: FragNebenan<br />Anzahl:  846<br />Bereich: Intern", "Kategorie: Kalender<br />Anzahl:  914<br />Bereich: Intern", "Kategorie: Zeitungen<br />Anzahl: 2903<br />Bereich: Intern"), 
  textsrc = "BiegT:0:d612ac", 
  type = "bar", 
  width = c(0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9), 
  widthsrc = "BiegT:0:271718", 
  xaxis = "x", 
  xsrc = "BiegT:0:8d148e", 
  yaxis = "y", 
  ysrc = "BiegT:0:46ca2b"
)
data <- list(trace1, trace2)
layout <- list(
  barmode = "relative", 
  font = list(
color = "rgba(0,0,0,1)", 
family = "", 
size = 14.6118721461
  ), 
  hovermode = "closest", 
  legend = list(
bgcolor = "rgba(255,255,255,1)", 
bordercolor = "transparent", 
borderwidth = 1.88976377953, 
font = list(
  color = "rgba(0,0,0,1)", 
  family = "", 
  size = 11.6894977169
)
  ), 
  margin = list(
r = 7.30593607306, 
t = 41.5092639431, 
b = 37.9293552673, 
l = 116.164383562
  ), 
  paper_bgcolor = "rgba(255,255,255,1)", 
  plot_bgcolor = "rgba(255,255,255,1)", 
  shapes = list(
list(
  fillcolor = "transparent", 
  line = list(
    color = "rgba(51,51,51,1)", 
    width = 0.664176006642
  ), 
  type = "rect", 
  x0 = 0, 
  x1 = 1, 
  xref = "paper", 
  y0 = 0, 
  y1 = 1, 
  yref = "paper"
)
  ), 
  showlegend = FALSE, 
  title = "Aufrufe pro Anwendung", 
  titlefont = list(
color = "rgba(0,0,0,1)", 
family = "", 
size = 17.5342465753
  ), 
  xaxis = list(
anchor = "y", 
automargin = TRUE, 
autorange = FALSE, 
categoryarraysrc = "BiegT:0:ccaf82", 
categoryorder = "array", 
domain = c(0, 1), 
gridcolor = "rgba(235,235,235,1)", 
gridwidth = 0.664176006642, 
hoverformat = ".2f", 
linewidth = 0, 
range = c(-52.8, 6652.8), 
showgrid = TRUE, 
showline = FALSE, 
showticklabels = TRUE, 
tickangle = 0, 
tickcolor = "rgba(51,51,51,1)", 
tickfont = list(
  color = "rgba(77,77,77,1)", 
  family = "", 
  size = 11.6894977169
), 
ticklen = 3.65296803653, 
tickmode = "array", 
ticks = "outside", 
ticktextsrc = "BiegT:0:a9bd24", 
tickvalssrc = "BiegT:0:6ea3e5", 
tickwidth = 0.664176006642, 
title = "Anzahl Aufrufe", 
titlefont = list(
  color = "rgba(0,0,0,1)", 
  family = "", 
  size = 14.6118721461
), 
type = "linear", 
zeroline = FALSE
  ), 
  yaxis = list(
anchor = "x", 
automargin = TRUE, 
autorange = FALSE, 
categoryarraysrc = "BiegT:0:36101b", 
categoryorder = "array", 
domain = c(0, 1), 
gridcolor = "rgba(235,235,235,1)", 
gridwidth = 0.664176006642, 
hoverformat = ".2f", 
linewidth = 0, 
range = c(0.4, 27.6), 
showgrid = TRUE, 
showline = FALSE, 
showticklabels = TRUE, 
tickangle = 0, 
tickcolor = "rgba(51,51,51,1)", 
tickfont = list(
  color = "rgba(77,77,77,1)", 
  family = "", 
  size = 11.6894977169
), 
ticklen = 3.65296803653, 
tickmode = "array", 
ticks = "outside", 
ticktextsrc = "BiegT:0:4f9417", 
tickvalssrc = "BiegT:0:a77cf1", 
tickwidth = 0.664176006642, 
title = "", 
titlefont = list(
  color = "rgba(0,0,0,1)", 
  family = "", 
  size = 14.6118721461
), 
type = "linear", 
zeroline = FALSE
  )
)
p <- plot_ly()
p <- add_trace(p, x=trace1$x, y=trace1$y, base=trace1$base, basesrc=trace1$basesrc, hoverinfo=trace1$hoverinfo, legendgroup=trace1$legendgroup, marker=trace1$marker, name=trace1$name, orientation=trace1$orientation, showlegend=trace1$showlegend, text=trace1$text, textsrc=trace1$textsrc, type=trace1$type, width=trace1$width, widthsrc=trace1$widthsrc, xaxis=trace1$xaxis, xsrc=trace1$xsrc, yaxis=trace1$yaxis, ysrc=trace1$ysrc)
p <- add_trace(p, x=trace2$x, y=trace2$y, base=trace2$base, basesrc=trace2$basesrc, hoverinfo=trace2$hoverinfo, legendgroup=trace2$legendgroup, marker=trace2$marker, name=trace2$name, orientation=trace2$orientation, showlegend=trace2$showlegend, text=trace2$text, textsrc=trace2$textsrc, type=trace2$type, width=trace2$width, widthsrc=trace2$widthsrc, xaxis=trace2$xaxis, xsrc=trace2$xsrc, yaxis=trace2$yaxis, ysrc=trace2$ysrc)
p <- layout(p, barmode=layout$barmode, font=layout$font, hovermode=layout$hovermode, legend=layout$legend, margin=layout$margin, paper_bgcolor=layout$paper_bgcolor, plot_bgcolor=layout$plot_bgcolor, shapes=layout$shapes, showlegend=layout$showlegend, title=layout$title, titlefont=layout$titlefont, xaxis=layout$xaxis, yaxis=layout$yaxis)

Hi @pablo, it’s hard to say exactly what’s going on here without seeing the ggplot2 code you used to generate this chart. Would you be able to post that here, or a small example that reproduces the issue?

Hi @michaelbabyn,

thank you very much for your answer. I’ll post an example that repoduces the issue - hopefully within the next days!