Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

line break and subscript in axis title using plotly in R

Tags:

r

plotly

I just started to use plotly for some interactive scatter plots in R and having a hard time on axis labels. Normally I designed my plots with ggplot2 and then using the ggplotly function to convert them, but this is sometimes very slow for any reason. So I want to create my plots directly in plotly...

I am trying now to change the axis title and want to add line breaks and later I also want to add subscript labels. But I am already failing at the newline in the title. Is there any trick?

library(plotly)
library(dplyr)
plot_ly(mtcars, x = wt, y = mpg, text = rownames(mtcars), mode = "text") %>%
  layout(xaxis=list(title='text with\nlinebreak'))
like image 255
drmariod Avatar asked Jul 08 '16 06:07

drmariod


2 Answers

In plotly, you can get linebreaks (and other text formatting) using html tags.

So piping

layout(xaxis=list(title='text with <br> linebreak'))

should work.

Hence, to get subscript labels use the <sub> tag. For example

CO<sub>2</sub>

will give you

CO2.

like image 143
Martin C. Arnold Avatar answered Sep 25 '22 21:09

Martin C. Arnold


Note you have to use <br> or <br /> not <br/> (without a space)

like image 35
JobJob Avatar answered Sep 22 '22 21:09

JobJob