【问题标题】:How to set all xaxis dynamically to empty for all levels?如何将所有 xaxis 动态设置为所有级别的空?
【发布时间】:2021-07-02 07:15:10
【问题描述】:

MWE:

# Packages; ####
library(tidyverse)
library(plotly)

# Plot; #####
mtcars %>% 
        group_by(gear,am) %>% 
        summarise(
                mpg = mean(mpg)
        ) %>% do(
                plot = plot_ly(
                        data = .,
                        x = ~gear,
                        y = ~mpg
                ) %>% add_bars(
                        name = ~am
                )
        ) %>% subplot(
                nrows = 1,
                shareY = TRUE,
                shareX = TRUE
        ) %>% layout(
                title = "Facetted Plot",
                xaxis = list(title = "")
        )

这个plot 有三个图例,每个gear 一个。如何动态地将所有 Xaxis 动态设置为"",而无需显式指定xaxis = list(title = "")xaxis1 = list(title = "")

我自己的半心半意的尝试/想法是像这样在function 中使用parseeval

plot_function <- function(grouping_vars) {
        
        
        # Baseplot; ####
        base_plot = mtcars %>% 
                group_by(!!sym(grouping_vars[1]),!!sym(grouping_vars[2])) %>% 
                summarise(
                        mpg = mean(mpg)
                ) %>% do(
                        plot = plot_ly(
                                data = .,
                                x = ~!!sym(grouping_vars[1]),
                                y = ~mpg
                        ) %>% add_bars(
                                name = ~am
                        )
                ) %>% subplot(
                        nrows = 1,
                        shareY = TRUE,
                        shareX = TRUE
                ) 
        
        # Remove all Xaxis Labels; ####
        
        # Count number of unique values in grouping var;
        no_unique <- mtcars %>%
                select(grouping_vars[1]) %>% 
                unique() %>%
                nrow()
        
        
        gen_layout <- 1:no_unique %>% map(
                .f = function(i) {
                        
                        "some expression here"
                        
                }
        )
        
        

        base_plot %>% 
                gen_layout %>% 
                parse(text = .) %>%
                eval()

        
        
        
}

但我认为必须有一个更优雅的解决方案。如果可能,最好使用plotlytidyverse 解决方案!

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    不确定plotly 是否提供了一个简单的选项来实现这一点。但一种方法是为自己编写一个方便的函数来删除(或设置相同)所有 x 轴的标题:

    # Packages; ####
    library(tidyverse)
    library(plotly)
    
    set_x_axis_title <- function(p, title = "") {
      layout <- p$x$layout
      xaxes <- names(layout)[grepl("^xaxis", names(layout))]
      for (i in xaxes) {
        layout[[i]]["title"] <- title 
      }
      p$x$layout <- layout
      p
    }
    
    # Plot; #####
    mtcars %>% 
      group_by(gear,am) %>% 
      summarise(
        mpg = mean(mpg)
      ) %>% do(
        plot = plot_ly(
          data = .,
          x = ~gear,
          y = ~mpg
        ) %>% add_bars(
          name = ~am
        )
      ) %>% subplot(
        nrows = 1,
        shareY = TRUE,
        shareX = TRUE
      ) %>% layout(
        title = "Facetted Plot"
      ) %>% 
      set_x_axis_title()
    #> `summarise()` has grouped output by 'gear'. You can override using the `.groups` argument.
    

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 2013-03-26
      • 2019-05-30
      • 2011-12-20
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      相关资源
      最近更新 更多