Plotly and ggplot with facet_grid in R: How to to get yaxis labels to use ticktext value instead of range value?

前端 未结 2 1971
予麋鹿
予麋鹿 2021-01-20 08:32

I would like to use ggplot2 facets with plotly but am running into issues with the y-axis labels showing the yaxis range values instead of the ticktext. Is there something I

2条回答
  •  悲&欢浪女
    2021-01-20 08:53

    I have similiar issues with the ggplot to plotly conversion but came up with a different solution. Still not perfect, but with some tuning I am sure you can achieve very good results:

    #Using your original dt dataframe
    
    dt<-data.table(mtcars)
    dt[,car.id:=rownames(mtcars)]
    dt[,model:=substr(car.id,1,regexpr(" ",car.id)-1)][model=="",model:=car.id]
    
    
    
    #define plot xaxis limits (+/- 10%)
    
    limits <- dt %>%
      summarise(max = ceiling(max(mpg*1.1)),
                min = floor(min(mpg*0.9)))
    
    
    #define height of subplots by finding the number of cars in each "facet"
    
    plot_height<- dt %>%
      group_by(model) %>%
      count() %>%
      ungroup() %>%
      mutate(height_pct = n/sum(n))
    
    
    #define a list of ggplots and feed it in the subplot function with the calculated limits
    
    dt %>%
      split(.$model) %>%
      map(function(x) {
        ggplot(data=x,aes(mpg,car.id)) + geom_point()+
          facet_grid(model~.) + xlim(c(limits$min,limits$max))
      }) %>%
      subplot(margin = 0.005, shareX = T,heights = plot_height$height_pct,nrows = nrow(plot_height))
    

提交回复
热议问题