请注意,您的图表目前不能完全用给定的代码重现,所以如果我做了不必要的假设,请告诉我。
TLDR:如果您更喜欢问题中描述的所有更改,请查看底部。
我建议不要使用tickangle,因为它有点搞砸了。试试下面的。注意insidetextfont 和tickfont 用于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))
)
如果您想让标签位于条形之外,请使用textposition = 'outside' 而不是textposition = 'auto',并去掉默认黑色的insidetextfont。这可能最终会弄乱 y 轴的范围,您需要手动定义范围,这可能很麻烦。
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)
)
)
这给了我们。
我不推荐tickangle,但如果必须,请在layout 中与margin 一起使用。
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)
)
希望这会有所帮助。