I realised that matrices are a lot faster for storing numeric data than any other format in R. Interestingly they can also be used to speed up my plotly
code. Which is desirable because recalculating plotly
-plots e.g. in a shiny app can be slow.
However readability in plotly suffers a lot. If I want a line plot together with some text and maybe some dots, the code looks like this:
### Creating sample data
mysim=cbind(x=rep(1:4,4),y=round(runif(16)*10,1),id=rep(1:4,each=4))
plot_ly() %>%
add_trace(x=mysim[mysim[,'id']==1,'x'],y=mysim[mysim[,'id']==1,'y'],
type='scatter',mode='lines',opacity=0.9,hoverinfo='skip',
line=list(color='#AAAAAA',dash='dashed')) %>%
add_trace(x=mysim[mysim[,'id']==2,'x'],y=mysim[mysim[,'id']==2,'y'],
type='scatter',mode='text',
hoverinfo='y',text=mysim[mysim[,'id']==2,'y']) %>%
add_trace(x=mysim[mysim[,'id']==3,'x'],y=mysim[mysim[,'id']==3,'y'],
type='scatter',mode='lines',
line=list(color='#17becf',dash='solid'),hoverinfo='x') %>%
add_trace(x=mysim[mysim[,'id']==4,'x'],y=mysim[mysim[,'id']==4,'y'],
type='scatter',mode='marker')
That doesn't look very nice, right?
Is there any way to make this code more readable without making it slower? Since coding these kind of plotly
graphs seems cumbersome to me (even when using data.frames
), I am thinking about submitting that to Github as an open issue. Thus I want to be sure that I didn't miss something.
I tried improving readability it by
- using the
split
argument inside theplot_ly
-wrapper (just plots every trace four times) - using
data=mysim[mysim[,'id']==1,]
andx=~x
orx=~'x'
in theadd_trace
(returns an error)