Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

R Plotly : Horizontal Bar chart with Y axis on right

$
0
0

I am trying to plot bar chart with Y axis on right.

Expectation :

enter image description here

However I am unable to shift the y axis to right. I have tried with side = 'right' but it seems to not work.

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 )

Getting output as below. Here the y axis shifts to right but the starting of the bars are still from left side. Those need to be start from right.

enter image description here


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>