【问题标题】:'Labels on top' with facet_grid, or 'space option' with facet_wrap带有 facet_grid 的“顶部标签”或带有 facet_wrap 的“空格选项”
【发布时间】:2015-01-22 16:08:27
【问题描述】:

facet_grid 允许我根据 y 轴上的项目数调整每个方面的宽度(space 参数):

df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

但我想在顶部添加刻面标签,所以我切换到 facet_wrap,并丢失了 space 参数(刻面的宽度都相同):

ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

有没有可能两全其美?

提前感谢您的帮助。

【问题讨论】:

    标签: r ggplot2 facet


    【解决方案1】:

    我认为此功能不存在或计划在ggplot2 (see here for the closed issue discussing this feature) 中实施

    但是ggforce 包支持您正在寻找的内容

    library(ggplot2)
    library(ggforce)
    df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
      
    ggplot(df, aes(x = value, y = item)) + 
      geom_point() + 
      ggforce::facet_col(facets = vars(label), 
                         scales = "free_y", 
                         space = "free") +
      ylab("") + 
      theme(strip.text.y = element_text(angle=0))
    

    reprex package (v0.3.0) 于 2020 年 6 月 28 日创建

    【讨论】:

    • 感谢分享!不错的功能。我知道示例图是这样的,但只是一个小提示:使用labs(y = NULL) 删除实验室标题,这样实际上什么都不会绘制。否则,那里仍然会有“东西”。 +1
    • 有什么办法可以在所有面板上保留 x 轴标签?我正在制作一个日历,我希望每个方面都标有工作日。
    • 使用这个例子类似于:ggforce::facet_col(facets = vars(label), scales = "free", space = "free") + scale_x_continuous(breaks = seq(-1, 1, 1), limits = c(-1, 1))
    【解决方案2】:

    可以手动完成。所需地块中三个面板的高度比大致为 1:3:2。三个面板的高度可以通过改变grobs来调整:

    library(ggplot2)
    library(grid)
    df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
    
    p1 = ggplot(df, aes(x = value, y = item)) + 
    geom_point() + 
    facet_wrap(~ label, scales = "free_y", ncol = 1) + 
    ylab("")
    
    g1 = ggplotGrob(p1) 
    
    g1$heights[[7]] = unit(1, "null")
    g1$heights[[12]] = unit(3, "null")
    g1$heights[[17]] = unit(2, "null")
    
    grid.newpage()
    grid.draw(g1)
    

    或者,可以将高度设置为与原始图中的高度相同:

    p2 = ggplot(df, aes(x = value, y = item)) + 
    geom_point() + 
    facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
    ylab("") + 
    theme(strip.text.y = element_text(angle=0))
    
    g2 = ggplotGrob(p2) 
    
    g1$heights[[7]] = g2$heights[[6]]
    g1$heights[[12]] = g2$heights[[8]]
    g1$heights[[17]] = g2$heights[[10]]
    
    grid.newpage()
    grid.draw(g1)
    

    或者,可以在不参考原始图的情况下设置高度。可以根据df中每个labelitems的数量来设置。并借用@baptiste's answer here的一些代码,从面板对应的布局中选择项目:

    # From 'df', get the number of 'items' for each 'label'.
    # That is, the number y-breaks in each panel.
    library(plyr)
    N = dlply(df, .(label), function(x) length(row.names(x)))
    
    # Get the items in the g1 layout corresponding to the panels.
    panels1 <- g1$layout$t[grepl("panel", g1$layout$name)]
    
    # Replace the default panel heights with relative heights
    g1$heights[panels1] <- unit(N, "null")
    
    ## Draw g1
    grid.newpage()
    grid.draw(g1)
    

    【讨论】:

      【解决方案3】:

      我不知道如何在ggplot 中做到这一点,但是您可以使用latticeExtraggplotoid 样式实现类似的布局:

      library(latticeExtra)
      df$label <- factor(df$label, levels = unique(df$label))
      
      dotplot(item ~ value | label, data = df,
              layout = c(1, 3),
              as.table = TRUE,
              scales = list(y = list(relation = "free")),
              par.settings = ggplot2like())
      resizePanels()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-18
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        相关资源
        最近更新 更多