Set text size within marker in r plotly bubble chart

Soumya Boral

The labels within bubbles are appearing with size proportional to size argument. However I want to keep the labels in constant sizes.

Which argument should I use to keep them at constant size ?

Code that I am using is provided below.

df = data.frame( x = c( 3, 2, 2, 4, 6, 8 ), y = c( 3, 2, 3, 4, 6, 7 ), size = c( 20, 20, 20, 30, 40, 40 ), labels = letters[1:6] )

evo_bubble <- function(plot_data ,x_var, y_var, z_var, t_var) {

  # Trasform data into dataframe and quos
  df <- data.frame(plot_data)
  xenc <- enquo(x_var)
  yenc <- enquo(y_var)
  zenc <- enquo(z_var)
  tenc <- enquo(t_var)
  df <- df %>% mutate( bubble_size = !!zenc*50 ) # Modify the denominator if you want to change the dimension of the bubble


  # Set parameters for the plot 
  bubble_pal <- c("white", "#AECEE8" )
  gray_axis <- '#dadada'
  font_size <- list(size = 12, family = 'Lato')
  width <- 0.5
  legend_name <- Hmisc::capitalize( quo_name(zenc) ) # WATCH OUT! it works only with the package with Hmisc
  decimal <- ',.2f'
  sep <- ','
  #x_name <- capitalize(quo_name(xenc))
  y_name <- Hmisc::capitalize(quo_name(yenc))

  p <- plot_ly(df, x = xenc, y = yenc, name = '', text = tenc, type = "scatter",  mode = 'markers+text', 
               hoverlabel = list(font = font_size), size = zenc, color = zenc, hoverinfo = "text+y", colors= bubble_pal,
               marker = list(size = df$bubble_size, line = list(color = gray_axis)) ) %>% hide_colorbar()

  p <- p %>%  layout(xaxis = list(zeroline = F,
                                  title = '',
                                  linecolor = gray_axis,
                                  titlefont = font_size,
                                  tickfont  = font_size,
                                  rangemode='tozero',
                                  gridcolor = gray_axis,
                                  gridwidth = width,
                                  hoverformat = decimal,
                                  mirror = "ticks",
                                  tickmode = 'array',
                                  tickcolor = gray_axis,
                                  linewidth = width,
                                  showgrid = F ),
                     yaxis = list(title = y_name,
                                  zerolinecolor = gray_axis,
                                  linecolor = gray_axis,
                                  mirror = "ticks",
                                  hoverformat = '.2f',
                                  linewidth = width,
                                  tickcolor = gray_axis,
                                  tickformat = '.2f',
                                  titlefont = font_size,
                                  tickfont  = font_size,
                                  showgrid = FALSE) ) %>%
    config(displayModeBar = F)

  return(p)
}


evo_bubble( df, x, y, size, labels )

Expected image :

enter image description here

Obtained image :

enter image description here

Please ignore the colors in plot.

ismirsehregal

You can use add_text to get the desired result:

library(plotly)
library(dplyr)

DF = data.frame( x = c( 3, 2, 2, 4, 6, 8 ), y = c( 3, 2, 3, 4, 6, 7 ), size = c( 20, 20, 20, 30, 40, 40 ), labels = letters[1:6] )

evo_bubble <- function(plot_data, x_var, y_var, z_var, t_var) {
  # browser()
  # Trasform data into dataframe and quos
  DF <- data.frame(plot_data)
  xenc <- enquo(x_var)
  yenc <- enquo(y_var)
  zenc <- enquo(z_var)
  tenc <- enquo(t_var)
  DF <- DF %>% mutate( bubble_size = !!zenc*50 ) # Modify the denominator if you want to change the dimension of the bubble


  # Set parameters for the plot 
  bubble_pal <- c("white", "#AECEE8" )
  gray_axis <- '#dadada'
  font_size <- list(size = 12, family = 'Lato')
  width <- 0.5
  legend_name <- Hmisc::capitalize( quo_name(zenc) ) # WATCH OUT! it works only with the package with Hmisc
  decimal <- ',.2f'
  sep <- ','
  #x_name <- capitalize(quo_name(xenc))
  y_name <- Hmisc::capitalize(quo_name(yenc))

  p <- plot_ly(DF, x = xenc, y = yenc, name = '', type = "scatter",  mode = 'markers', 
               hoverlabel = list(font = font_size), size = zenc, color = zenc, hoverinfo = "text+y", colors= bubble_pal,
               marker = list(size = DF$bubble_size, line = list(color = gray_axis))) %>% 
    add_text(text = tenc, textfont = font_size, textposition = "middle center") %>% hide_colorbar()

  p <- p %>%  layout(xaxis = list(zeroline = F,
                                  title = '',
                                  linecolor = gray_axis,
                                  titlefont = font_size,
                                  tickfont  = font_size,
                                  rangemode='tozero',
                                  gridcolor = gray_axis,
                                  gridwidth = width,
                                  hoverformat = decimal,
                                  mirror = "ticks",
                                  tickmode = 'array',
                                  tickcolor = gray_axis,
                                  linewidth = width,
                                  showgrid = F ),
                     yaxis = list(title = y_name,
                                  zerolinecolor = gray_axis,
                                  linecolor = gray_axis,
                                  mirror = "ticks",
                                  hoverformat = '.2f',
                                  linewidth = width,
                                  tickcolor = gray_axis,
                                  tickformat = '.2f',
                                  titlefont = font_size,
                                  tickfont  = font_size,
                                  showgrid = FALSE) ) %>%
    config(displayModeBar = F)

  return(p)
}


evo_bubble( DF, x, y, size, labels )

Result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change legend marker size for Plotly scatter plot (bubble chart) in Python

size legend for plotly bubble map/chart

Adding color and bubble size legend in R plotly

R Plotly - Assign the color of a marker by its size

Plotly marker size

Manually changing the size of the Bubbles for Plotly Bubble Map in R

R plotly version 4.5.2 scatterplot legend bubble size settings

Plot a bubble chart using plotly

R plotly: Adjust absolute marker size on 3D scatterplot

spurious warning when mapping marker size in plotly R

R Plotly set custom colors for bar chart

Hover text for plotly r candlestick chart

R Scatterplot/bubble chart with dot size based on observation number

How to sort an R Plotly grouped bar chart within groups?

Unproportional Bubble Chart with the size paramater

Plotly Bubble chart from pandas crosstab

Label specific bubbles in Plotly bubble chart

Custom hoverinfo text on plotly R chart that is different to the 'text' parameter?

Is it possible to override font size limit on labels in R plotly bar chart?

Change output width of plotly chart size in R Markdown PDF output

How do I create weighted scatter plot / bubble chart with color gradient using plotly R

Add marker over scatter chart plotly

How can I set marker size based on column value in python using plotly scatterplot?

Creating a Bubble Chart of text columns

set font size of sankey chart in HIghcharter for R

Is there a way to add text to the `y2` axis of a plotly chart in R?

How to add text label to R plotly aggregate chart?

Specifying marker size in data unit for plotly

plotly-r: specifying size of marker in add_trace() reduces opacity of markers