R Plotly:Y轴在右侧的水平条形图
R Plotly : Horizontal Bar chart with Y axis on right
我正在尝试绘制 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 (" ", 20 ), y_name, rep( " ", 20 ),
rep( "\n ", 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 轴向右移动,但条形图的起点仍然是左侧。这些需要从右开始。
您必须在 x 轴上设置 autorange="reversed"
才能使其按照您喜欢的方式运行。
我正在尝试绘制 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 (" ", 20 ), y_name, rep( " ", 20 ),
rep( "\n ", 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 轴向右移动,但条形图的起点仍然是左侧。这些需要从右开始。
您必须在 x 轴上设置 autorange="reversed"
才能使其按照您喜欢的方式运行。