0

I am working with the R programming language. I have the following data:

myFun <- function(n = 5000) {
  a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
  paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}

var1 <- c("a", "b", "c", "d", "e")

var1 <- sample(var1, 100, replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2))

var2 = rnorm(100, 100,100)

var3 = rnorm(100,100,100)

var4 = rnorm(100, 100,100)

label1 = myFun(100)

label2 = myFun(100)

label3 = myFun(100)

label4 = myFun(100)


my_data  =data.frame(var1, var2, var3, var4, label1, label2, label3, label4)

I am trying to make a Bubble Plot using the Plotly library. Here is the code I am using:

library(plotly)

p = plot_ly(my_data, x = ~ var2, y = ~ var3, color = ~ var1, type = "scatter", mode = "markers", size = ~var4, marker = list(symbol = 'circle', sizemode = 'diameter', line = list(width = 2, color = '#FFFFFF'), opacity =0.4, autosize = T))

p = p %>% layout ( axis = list(title = 'Title 1'), yaxis = list(title = 'title 2'), legend = list(title = list(text = '<b> var1 </b>')))

p = p %>% layout(title = paste0('Main Title', '<br>', 'sup', 'Subtitle' )) 

annotation_row = my_data[1,]

annotation = list( x = my_data$var2, y = my_data$var3, text = my_data$var1, xref = "x", yref = "y", showarrow = TRUE, arrowhead = 7, ax = 20, ay = -40)

p = p %>% add_trace (text = paste("Var 1:", my_data$var1, "<br> Var 2: ", my_data$var2, "<br> Var 3: ", my_data$var3 ), hoverinfo = "text" )

p = p %>% layout(annotations = annotation)

htmltools::save_html(html = p, file = "file.html")

The plot runs fine, but I am noticing two problems:

  • The values in the legend are appearing twice (e.g. a,b,c,d,e,a,b,c,d,e)

  • (Even though the plot in this example appears fine) When I run this code on my real data, the plot is appearing "horizontally stretched" (e.g.https://stackoverflow.com/questions/73552382/r-forcing-plotly-to-save-full-sized-plots). I tried different options to fix this "horizontal stretching"

For example (instead of "autosize", I tried to manually specify the height and width) :

p = plot_ly(my_data, x = ~ var2, y = ~ var3, color = ~ var1, type = "scatter", mode = "markers", size = ~var4, marker = list(symbol = 'circle', sizemode = 'diameter', line = list(width = 2, color = '#FFFFFF'), opacity =0.4, height = 1000, width = 1000))

Can someone please show me how to fix this problem?

Is it possible to undo this "legend duplication" and re-write the plotly code in such a way that the "horizontal stretching" problem is fixed?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    You may use `showlegend = FALSE` in `add_trace` for your first issue – akrun Aug 31 '22 at 17:28
  • Do you have any ideas about the second issue? Is there some default that can remove the "horizontal stretching"? Thank you so much! – stats_noob Aug 31 '22 at 18:08
  • It may be related to the window size of plot – akrun Aug 31 '22 at 18:13
  • 1
    @ akrun: this post over here solved my problem; https://stackoverflow.com/questions/47921757/how-to-resize-html-widget-using-savewidget-in-htmlwidgets-r Thank you so much! – stats_noob Aug 31 '22 at 21:48

0 Answers0