R 长刻面(在ggplotly/plotly重叠刻面中包裹标签#x27;s.背景

R 长刻面(在ggplotly/plotly重叠刻面中包裹标签#x27;s.背景,r,ggplot2,r-plotly,facet-wrap,ggplotly,R,Ggplot2,R Plotly,Facet Wrap,Ggplotly,我有一个像下面这样的图,我需要显示一个图标题和一些长的刻面标签。在ggplot2中,它看起来很好 Reprex: 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 <- st

我有一个像下面这样的图,我需要显示一个图标题和一些长的刻面标签。在
ggplot2
中,它看起来很好

Reprex:

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
库(ggplot2)
图书馆(stringr)
图书馆(绘本)
iris$Species2
gp根据答案,我编写了一个函数,简化了过程:

起初的
谢谢e.matt,但这只会改变字体大小,而不会改变条纹背景的大小。在我的“真实”图表中,文本已经尽可能小了。。。另外,我可以在转换为plotly之前修改ggplot对象中的文本大小。我已经更新了,它应该向下移动刻面框以覆盖整个刻面标签。这是你想要的吗?谢谢!是的,它有帮助-文本仍然在框外的纵横比,我目前想显示我的图形,但我想我可以调整你的数字,使其工作。我会试试。只是提醒一下——在我的“压扁”纵横比(一个短的,而不是高的绘图)中,一些数据被新的条形背景覆盖。如果一些数据仍然被覆盖,将ylim 5.5 eg 7增加到一个更大的值,并将刻面框向上移动,即更少的负eg-80
gp <- ggplotly(g)
gp
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
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()
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)
}