R Plotly:右侧 Y 轴的水平条形图

Posted

技术标签:

【中文标题】R Plotly:右侧 Y 轴的水平条形图【英文标题】:R Plotly : Horizontal Bar chart with Y axis on right 【发布时间】:2020-03-20 01:35:21 【问题描述】:

我正在尝试用右侧的 Y 轴绘制条形图。

期待:

但是我无法将 y 轴向右移动。我试过使用 side = 'right' 但它似乎不起作用。

bar_plot_vertical_left <- function( plot_data , var_y, x_name = '', y_name = 'Y axis',  ... )

  df = data.frame( plot_data ) ; df = na.omit( df )

  var = quos(...) ; names_vars = names( var ) ; x_vars = names_vars[ startsWith( names_vars, 'var_x' ) ]

  x_var_names = sapply( 1:length( x_vars ), function( j ) quo_name( var[[ x_vars[j] ]] )  )

  row_sum = df %>% select( x_var_names ) %>% rowSums()

  yenc = enquo( var_y ) ; cols = colorRampPalette(c("white", "#4C68A2"))( length( x_vars ) )

  #... Plot parameters .....

  font_size = list( size = 12, family = 'Lato' ) ; gray_axis = '#dadada'

  p = plot_ly( data = df, x = var[[ x_vars[1] ]], y = yenc, name = quo_name( var[[ x_vars[1] ]] ), 

               type = 'bar', marker = list( color = cols[1], line = list( color = '#E1E1E1', width = 0.8 ) ), 

               textposition = 'auto', text = var[[ x_vars[1] ]], hoverlabel = list( font = font_size ),

               orientation = 'h'  ) %>%

    layout( title = list( text = 'Vertical left', x = 0, font = list( family = 'Lato black' ) ), barmode = 'stack',

            xaxis = list( title = x_name, showgrid = F, showticklabels = F,  zerolinecolor = gray_axis,

                          tickfont = font_size, titlefont = font_size ),

            yaxis = list( title = paste0( c( rep ("&nbsp;", 20 ), y_name, rep( "&nbsp;", 20 ),

                                             rep( "\n&nbsp;", 3 ) ), collapse = '' ),

                          linecolor = gray_axis, zerolinecolor = gray_axis, side = 'right',

                          tickfont = font_size, titlefont = font_size ),

            legend = list( font = font_size, orientation= 'h', font = font_size,

                           x = 1 , y = 1.2, xanchor = "left", yanchor = 'top' ),

            margin = list( l = 20, r = 100, b = 10, t = 10 )

    )

  if( length( x_vars ) >= 2 )

    for( i in 2:length( x_vars ) )

      p = p %>% add_trace( x = var[[ x_vars[i] ]], name = quo_name( var[[ x_vars[i] ]] ),

                           marker = list( color = cols[i], line = list( color = '#E1E1E1', width = 0.8 ) ),

                           textposition = 'auto', text = var[[ x_vars[i] ]],

                           hoverlabel = list( font = font_size ) )

    

  

  p =  p %>% add_annotations( xref = 'x', yref = 'y', x = ( row_sum ) + 5, y = yenc,

                              text = paste( row_sum ), font = font_size, showarrow = F )

  p



bar_plot_vertical_left( df, var_y = Labels, var_x1 = Aria, var_x2 = Acqua, var_x3 = Fuoco, var_x4 = Terra,

                       var_x5 = Cielo )

得到如下输出。这里 y 轴向右移动,但条形图的起点仍然是从左侧。那些需要从正确的开始。

【问题讨论】:

【参考方案1】:

您必须在 x 轴上设置 autorange="reversed" 才能让它按照您喜欢的方式运行。

【讨论】:

以上是关于R Plotly:右侧 Y 轴的水平条形图的主要内容,如果未能解决你的问题,请参考以下文章

如何在条形图上更正 R 中 y 轴的比例和顺序

R Shiny中的Plotly分组条形图

如何为 R 中使用百分比而不是计数作为 y 轴的数据框创建条形图?

在 plotly 中为每个条形图添加两个文本

R语言 条形图

水平堆叠的独特 100% 条形图 plotly python