【问题标题】:Set text size within marker in r plotly bubble chart在r plotly气泡图中设置标记内的文本大小
【发布时间】:2023-03-18 23:44:01
【问题描述】:

气泡内的标签显示的大小与大小参数成正比。但是我想保持标签的大小不变。

我应该使用哪个参数来保持它们的大小不变?

下面提供了我正在使用的代码。

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 )

预期图像:

获得的图像:

请忽略图中的颜色。

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    您可以使用add_text 来获得想要的结果:

    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 )
    

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多