I have the following bar in plotly and I want to :
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 :)
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.
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.
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.
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)))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With