【问题标题】:Long facet_wrap labels in ggplotly / plotly overlap facet's strip.backgroundggplotly / plotly 中的长 facet_wrap 标签重叠 facet 的 strip.background
【发布时间】:2020-04-09 13:58:43
【问题描述】:

我有一个像下面这样的情节,我需要在其中显示情节标题和一些长的刻面标签。在ggplot2,看起来还不错。

代表:

library(ggplot2)
library(stringr)
library(plotly)

iris$Species2 <- paste(iris$Species, "... some text to make the label really long and hard to put on a facet label")
iris$Species2 <- str_wrap(iris$Species2, 20)

g <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  labs(title = "This title isn't helping anyone") + 
  facet_wrap(~Species2)

g

但是,转换为动态图并没有按预期工作...构面标签被切断并进入标题:

gp <- ggplotly(g)
gp

有一个previous SO question about this,但看起来 OP 没有尝试答案 - 没有人发现建议的答案没有按预期工作。

我对@9​​87654330@ 在涉及方面时的奇怪行为并不陌生 - 请参阅对话here on github,但我不知道plotly 足以修改对象以强制它具有更长的strip.background。

希望有人可以帮我修改gp的对象以寻求解决方案。

【问题讨论】:

    标签: r ggplot2 r-plotly facet-wrap ggplotly


    【解决方案1】:
    gp <- ggplotly(g)
    # move facet labels down
    gp[["x"]][["layout"]][["annotations"]][[3]][["y"]] <- 0.85 
    gp[["x"]][["layout"]][["annotations"]][[4]][["y"]] <- 0.85
    gp[["x"]][["layout"]][["annotations"]][[5]][["y"]] <- 0.85
    
    # extend y axis to make room to move facet box down
    gp[["x"]][["layout"]][["yaxis"]][["range"]] <- c(1.88,5.5) 
    # extend facet boxes down
    gp[["x"]][["layout"]][["shapes"]][[2]][["y0"]] <- - 100 
    gp[["x"]][["layout"]][["shapes"]][[4]][["y0"]] <- - 100 
    gp[["x"]][["layout"]][["shapes"]][[6]][["y0"]] <- - 100
    
    gp
    

    【讨论】:

    • 感谢 e.matt 但这只会改变字体大小,而不是带状背景的大小。在我的“真实”图中,文本已经尽可能小......我也可以在转换为 plotly 之前修改 ggplot 对象中的文本大小。
    • 我已经更新了,它应该向下移动构面框以覆盖整个构面标签。这就是你所追求的吗?
    • 谢谢!是的,它有帮助 - 文本仍然在我目前想要显示我的图表的纵横比的框外,但我想我可以调整你的数字以使其工作。我会试试的。
    • 只是一个警告 - 在我的“挤压”纵横比(一个短而不是高的情节)下,一些数据被新的条形背景覆盖。
    • 如果某些数据仍然被覆盖,则将 ylim 5.5 例如 7 增加到更大的值并将构面框向上移动,即负数较小,例如 -80
    【解决方案2】:

    根据e.matt的回答,我写了一个简化流程的函数:

    原创

    facet_strip_bigger <- function(gp){
    
      # n_facets should be the number of facets x2
      n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]]))
      
      for(i in n_facets){
        if(n_facets[i] %% 2 == 0){
          gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + 80 # increase as needed
          gp[["x"]][["layout"]][["shapes"]][[i]][["y1"]] <- 0
        }
      }
      
      return(gp)
    }
    

    所以在这种特殊情况下:

    iris$Species2 <- paste(iris$Species, "... some text to make the label really long and 
        hard to put on a facet label")
    iris$Species2 <- str_wrap(iris$Species2, 20)
    
    g <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
      geom_point() +
      labs(title = "This title isn't helping anyone") + 
      theme(axis.title.y = element_blank(),
            axis.title.x = element_blank())+
      facet_wrap(~Species2) 
    
    g %>% 
      ggplotly() %>% 
      layout(title = list(y = 0.96,
                          yanchor = "top",
                          yef = "container"),
             margin = list(t = 110),
             yaxis = list(title = list(text = "Sepal width",
                                       standoff = 10L)),
             xaxis = list(title = list(text = "Sepal length"))
             ) %>%
      facet_strip_bigger()
    

    编辑

    我改进了函数,使size成为一个参数,所以每次需要更改大小时都不需要编辑函数。

    facet_strip_bigger <- function(gp, size){
      if(missing(gp)){
        print("this function needs a facet_wrap ggplotly object")
      }
      if(missing(size)){
        print("this function needs 'size' argument to be specified as integer. 80 will be introduced as default")
        size <- 80
      }
      
      n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]]))
      
      for(i in n_facets){
        if(n_facets[i] %% 2 == 0){
          gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + as.numeric(size)
          gp[["x"]][["layout"]][["shapes"]][[i]][["y1"]] <- 0
        }
      }
      
      return(gp)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2020-08-17
      • 1970-01-01
      • 2016-04-08
      • 2018-09-04
      • 2023-03-28
      • 2014-04-30
      • 2021-12-07
      • 2018-09-24
      相关资源
      最近更新 更多