How to change axis features in plotly?

痞子三分冷 提交于 2019-12-06 02:26:15

问题


I have the following bar in plotly and I want to :

  1. get the x-axis title away from the axis label so they dont overlap
  2. make the Y-axis labels bigger
  3. bring the bar values to the top of bars

My code is :

library(plotly)
plot_ly(x = c('100-200','200-400', '400-600','600-800','800- 1000'),
y = c(12261,29637,17469,11233,17043),          
name = "dd",
type = "bar", 
 xaxis = list(title ="tr"),
yaxis = list(title = "cc") ,                                                
 text = c(12261,29637,17469,11233,17043),textposition = 'auto') %>%
layout(
xaxis = list(tickangle=15, title = " "),
 yaxis = list(title = " "))

Thanks for your comments :)


回答1:


Question 1: get the x-axis title away from the axis label so they dont overlap
This problem can be solved setting proper margins with margin = list(b=100, l=100) in layout.

Question 2: make the Y-axis labels bigger.
Use xaxis = list(titlefont=list(size=30)) in layout

Question 3: bring the bar values to the top of bars.
Use add_text with textposition = 'top'

library(plotly)

x <- c('100-200','200-400', '400-600','600-800','800-1000')
y <- c(12261,29637,17469,11233,17043)
labs <- c(12261,29637,17469,11233,17043)

plot_ly(x = x, y = y,          
 name = "dd",
 type = "bar", 
 xaxis = list(title ="tr"),
 yaxis = list(title = "cc")) %>%
add_text(x=x, y=y, text=labs, hoverinfo='none', textposition = 'top', showlegend = FALSE, 
        textfont=list(size=20, color="black")) %>%
layout(margin = list(b=100, l=100),
 xaxis = list(tickangle=15, title = "Lenght of exon", titlefont=list(size=30)),
 yaxis = list(title = "Number of genes", titlefont=list(size=30)))




回答2:


Note that your graph is not exactly reproducible with the given code at the moment, so let me know if I have made unnecessary assumptions.

TLDR: Look at the bottom if you prefer all the changes described in the question.

I would recommend not using tickangle as it screws things up a bit. Try the following. Note the use of insidetextfont and tickfont for the yaxis.

library(plotly)
x = c('100-200','200-400', '400-600','600-800','800- 1000')
y = c(12261,29637,17469,11233,17043)
plot_ly(
  x = x, y = y, type = "bar", text = y, textposition = 'auto',
  insidetextfont = list(color = 'white')
) %>% layout(
  xaxis = list(title = "Length of exon"),
  yaxis = list(title = "Number of genes", tickfont = list(size = 15))
)

If you want to make the labels lie outside the bars, then use textposition = 'outside' instead of textposition = 'auto', and get rid of the insidetextfont for the default black colour. That may end up messing with the range of the y axis and you would need to manually define the range which may be cumbersome.

plot_ly(
  x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
  xaxis = list(title = "Length of exon"),
  yaxis = list(
    title = "Number of genes", tickfont = list(size = 15), 
    range = c(0, max(y) + 2000)
  )
)

This gives us .

I do not recommend tickangle, but if you must, use it with margin in layout.

plot_ly(
  x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
  xaxis = list(title = "Length of exon", tickangle = 15),
  yaxis = list(
    title = "Number of genes", tickfont = list(size = 15), 
    range = c(0, max(y) + 2000)
  ), margin = list(b=100)
)

Hope this helps.



来源:https://stackoverflow.com/questions/46189779/how-to-change-axis-features-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!