Priyanka Banerjee
Priyanka Banerjee

Reputation: 21

How to Plot a R Plotly Graph when having only one observation

I have a application that I have created, within which I have a few plotly bar graphs that change with different filters. They work well when I have 2+ bars to display (and I have made it such that if there are zero observations, then there won't be a graph displayed). However, when there is only one bar to plot in the bar graph, then the plotly function errors with essentially: Error in plot_ly(df, labels = ~(df[a]), : argument is missing, with no default. Is there a way to go around this error?

EDIT: As @PavoDive suggested that I should provide a reproducible example for my problem (thank you for your suggestion!), I tried to create a simple example of my plot code that still demonstrated the problem.

Simple version of my code:

df <- data.frame(
  ID = 1234:1235,
  ID_Name = c('ID_Name_1', 'ID_Name_2'), 
  Site = c('Site_1', 'Site_2'), 
  Time = c('Jan 2021', 'Jan 2021'), 
  Value = c(1000,2000), 
  Fraction = c(0.25, 0.75), 
  Percentage = c(25, 75)    
)

site_var = 'Site'
value_var = 'Value'

fig <- plot_ly(
               df, 
               x = df[[site_var]], 
               y = df[[value_var]], 
               type = 'bar', 
               insidetextfont = list(color = '#FFFFFF'), 
               hoverinfo = 'hovertext', 
               hovertemplate = paste('<i>Site Type</i>: %{x}','\n<i>Value</i>: ', format(df[[value_var]], scientific = TRUE, digits = 3) ,'\nRelative Contribution: ',df$Percentage,'%', sep = ''),
               color = ~(df[[site_var]]),
               marker = list(colors = colors,line = list(color = '#FFFFFF', width = 1)),
               showlegend = FALSE
) %>%
layout(
       title = list(text = "Sample Bar Graph"),
       margin = list(b = 210),
       xaxis = list( title = "Independent",showgrid = FALSE, zeroline = TRUE, showticklabels = TRUE),
       yaxis = list(title = "Dependent", showgrid = FALSE, zeroline = TRUE, showticklabels = TRUE, hoverformat = '.2f')
  )

However, this doesn't work with a dataframe like this:

df <- data.frame(
                 ID = 1234,
                 ID_Name = c('ID_Name_1'), 
                 Site = c('Site_1'), 
                 Time = c('Jan 2021'), 
                 Value = c(1000), 
                 Fraction = c(1), 
                 Percentage = c(100)    
                )

Upvotes: 1

Views: 90

Answers (1)

Priyanka Banerjee
Priyanka Banerjee

Reputation: 21

While creating the simpler version of my code due to @PavoDive's suggestion, I stumbled upon the answer (which is something I have tried to find for way too long, but better late than never!).

It turns out that the line marker = list(colors = colors,line = list(color = '#FFFFFF', width = 1)), was the culprit. I had included this line in my function a long time back, maybe due to looking at old documentation because I cannot find this keyword in the plot_ly() documentation anymore. Removing this line makes the function work!

Upvotes: 1

Related Questions