Hi,
I’m trying to combine a list of R
plotly
plots - vertically, but since the y-axis title of each of the individual plots is horizontal the combined plot comes out messy.
Here’s my example:
data:
df <- data.frame(cluster=unlist(lapply(letters[1:10],function(i) rep(paste0("cluster:",i),200))),
group=rep(c(rep("A",100),rep("B",100)),10),val=rnorm(2000))
df$group <- factor(df$group,levels=c("A","B"))
plotting a list
of density
plots, one per df$cluster
:
library(plotly)
plot.list <- lapply(unique(df$cluster),function(i){
density.df <- do.call(rbind,lapply(c("A","B"),function(b){
dens <- density(dplyr::filter(df,cluster == i,group == b)$val,adjust=1)
return(data.frame(x=dens$x,y=dens$y,group=b,stringsAsFactors=F))
}))
density.df$group <- factor(density.df$group,levels=c("A","B"))
if(i == unique(df$cluster)[1]){
cluster.plot <- plot_ly(x=~density.df$x,y=~density.df$y,type='scatter',mode='lines',color=~density.df$group,line=list(width=3),showlegend=T) %>%
layout(xaxis=list(title="Val",zeroline=F),yaxis=list(title=i,zeroline=F,showticklabels=F),legend=list(orientation="h",xanchor="center",x=0,y=1))
} else{
cluster.plot <- plot_ly(x=~density.df$x,y=~density.df$y,type='scatter',mode='lines',color=~density.df$group,line=list(width=3),showlegend=F) %>%
layout(xaxis=list(title="Val",zeroline=F),yaxis=list(title=i,zeroline=F,showticklabels=F))
}
return(cluster.plot)
})
Then, combining them using:
subplot(plot.list,nrows=length(plot.list),shareX=T,shareY=T,titleX=T,titleY=T)
Gives:
How can I rotate the y-axis title of each plot in plot.list
to be horizontal rather than vertical?