Suppose I have this lovely ggplot
library(plotly)
library(tidyverse)
mpg2=mpg%>%
filter(manufacturer%>%as.factor%>%as.numeric<6)%>%
group_by(manufacturer,model)%>%
summarize(hwy=mean(hwy))%>%
mutate(model=reorder(model,hwy))
myggplot=mpg2%>%
ggplot(aes(x=model,y=hwy))+
geom_col()+
coord_flip()+
facet_grid(manufacturer~.,scales="free",space="free")+
theme(legend.position="none",
axis.text.y=element_text(),
axis.title.y=element_blank(),
axis.text.x=element_text(size=12),
strip.text.y = element_text(angle = 0))
myggplot
looks good, then I try to plotlify it,
ggplotly(myggplot)
three problems.
- the facet with 1 bar only has a numeric yaxis label.
- the bar width is not identical across facets (my space="free" argument is lost).
- the labels (both y axis labels and facet labels go off-screen).
So, how do I fix it?
I also tried doing it natively, and this is what I have reached so far,
myplotly=mpg2%>%
nest(-manufacturer)%>%
mutate(plty=map(data,function(d){
plot_ly(d,x=~hwy,y=~model,type="bar") %>%
layout(showlegend = FALSE)
}))%>%
select(plty)%>%
subplot(nrows=5,shareX=T,heights = table(mpg2$manufacturer)%>%prop.table%>%unname())
myplotly
How would one finish it?