Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display X Axis at Top of Plotly Plot in R Shiny Instead of Bottom?

Tags:

r

shiny

plotly

How do I display the x axis at the top of a Plotly horizontal bar plot instead of at the bottom? Showing the x axis at both the top and bottom would also be ok. The plot will be displayed in R Shiny and must be Plotly not ggplotly. Thanks!

Example from https://plot.ly/r/horizontal-bar-charts/:

library(plotly)
plot_ly(x = c(20, 14, 23), 
y = c('giraffes', 'orangutans', 'monkeys'), 
type = 'bar', 
orientation = 'h')

EDIT: HubertL provided a solution (thanks!) that puts the x axis at the top but it then overlaps with the chart title. My fault for not specifying that I am using a title. Is there a way to get the x axis title and x axis ticks to not overlap the plot title?

plot_ly(x = c(20, 14, 23), 
        y = c('giraffes', 'orangutans', 'monkeys'), 
        type = 'bar', 
        orientation = 'h') %>%
   layout(xaxis = list(side ="top", 
          title = "EXAMPLE X AXIS TITLE"), 
          title = "EXAMPLE PLOT TITLE")

enter image description here

like image 969
Justin Meyer Avatar asked Sep 15 '25 19:09

Justin Meyer


1 Answers

You can use side ="top" in the layout.xaxis

plot_ly(x = c(20, 14, 23), 
        y = c('giraffes', 'orangutans', 'monkeys'), 
        type = 'bar', 
        orientation = 'h') %>%
  layout(xaxis = list(side ="top" ) ) 

enter image description here

like image 156
HubertL Avatar answered Sep 18 '25 08:09

HubertL