Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change axis features in plotly?

Tags:

r

plotly

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

enter image description here

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 :)

like image 344
Behmah Avatar asked Sep 13 '17 05:09

Behmah


People also ask

How do you change the y-axis values in Plotly?

You can pass a dict in the keyword argument yaxis . It could be something like go. Layout(yaxis=dict(range=[0, 10])) I hope this helps you.

Is Plotly customizable?

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments.

How do you increase your Figsize in Plotly?

One of the best ways to modify the size of the Plotly figure is by adjusting the width and the height parameters. We will start by discussing how to change the height and width parameters using Plotly Express. For example, in the following code, we create a simple line chart using the default Plotly's dimensions.


Video Answer


2 Answers

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)))

enter image description here

like image 68
Marco Sandri Avatar answered Nov 06 '22 11:11

Marco Sandri


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))
)

enter image description here

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 enter image description here.

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)
)

enter image description here

Hope this helps.

like image 25
Ameya Avatar answered Nov 06 '22 12:11

Ameya