R Plotly has X-axis hidden

2019-08-30 04:26发布

问题:

The r-plotly chart has its x-axis hidden when I plotted it. Below is the chart with this problem:

Below is my code:

p <- plot_ly(b1image, x = b1image$CNT, y = b1image$Label, type = 'bar', orientation = 'h', 
             marker = list(color = viridis::viridis_pal(option = "C", direction =1)(max(b1image$Label) - min(b1image$Label) + 5))) %>% 
  config(displayModeBar = F)
p

I am wondering if there is a way to avoid hidden x-axis?

Also, I would like to know how to change the scale of y-axis.

For example, I do not want to see 5.5, 4.5 ..., but only 5, 4, 3 ... etc. on y-axis.

Thanks in advance.

回答1:

The line can be altered by the argument showline = T or in some cases by using zeroline = T depending whether your plot actually runs from 0-x range.

almost all attributes can be looked up here

p <- plot_ly(mtcars, x = ~mpg, y = ~cyl, type = 'bar', orientation = 'h', 
             marker = list(color = viridis::viridis_pal(option = "C", direction =1)(max(mtcars$mpg) - min(mtcars$mpg) + 5))) %>% 
  config(displayModeBar = F) 
p <- layout(p, title = 'A plot',
       autosize = TRUE,
       margin = list(l = 50, r = 0, b = 20, t = 60, pad = 2), 
       xaxis = list(title = 'horizontal', ticks = "outside", ticklen = 5, tickwidth = 2, tickcolor = toRGB("black"),
                  showgrid = T, autorange = T, showticklabels = TRUE, zeroline = F,  showline = T),
       yaxis = list(title = 'vertical', ticks = "outside", ticklen = 5, tickwidth = 2, tickcolor = toRGB("black"),
                    showgrid = T, autorange = T, showticklabels = TRUE, zeroline = F,  showline = T),
       showlegend = T,  #show it or not
       legend = list(x = 100, y = 0.5), # can be used to position the legend
       scene = list(aspectratio = list(x = 1, y = 1, z = 1))) # can be used to keep the plot square, or not
p