【问题标题】:Creating a facet theme/design plot in ggplot2 without using facet_在不使用 facet_ 的情况下在 ggplot2 中创建 facet 主题/设计图
【发布时间】:2020-05-05 10:28:50
【问题描述】:

是否有可能在不使用facet_wrap() 的情况下在 ggplot2 中创建一个 facet_wrap 外观图我想实现这一点的原因是为了匹配其他一些设计。在下面的情节without_facet 中,我能否以某种方式在顶部添加“Setosa”,所以它看起来像with_facet 情节,而不使用facet_wrap

library(ggplot2)

df <- iris[iris$Species == 'setosa', ]

with_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +facet_wrap(~Species)
with_facet

without_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()

【问题讨论】:

  • 你想去掉情节上的分面标签还是将不同的情节组合在一起?
  • 所以您提供了一个错误的答案,将其删除并决定否决我的问题?
  • 我没有投反对票,我认为@desval 也没有。可能是您的问题有点不清楚,没有必要再猜测了。您可能需要使用拼凑或 gridExtra 加入绘图,这是您要找的吗?
  • 您希望它在多大程度上看起来像切面包装?你想要一个带有背景颜色的标题周围的框架吗?还是可以将标题放在绘图区域的顶部或上方?

标签: r ggplot2


【解决方案1】:

你可以试试

ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
   geom_point() + 
   ggtitle("setosa")  + 
   theme(plot.title = element_text(hjust = 0.5))

一种更“骇人听闻”的方法可能是这种硬编码方法:

ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point() + 
  ggtitle("setosa")  + 
  geom_rect(xmin = 4.225, xmax = 5.875 , ymin=4.5, ymax=4.6, fill ="lightgrey") + 
  coord_cartesian(clip = 'off', expand = 0.05) +
  theme(plot.title = element_text(hjust = 0.5, size = 12),
        plot.margin = margin(t = 30, r = 20, b = 20, l = 20, unit = "pt"))

【讨论】:

    【解决方案2】:

    这可能是一种选择:

    
    library(ggplot2)
    
    df <- iris[iris$Species == 'setosa', ]
    
    # with annotate:
    
    
    with_annotate <-
      ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
      geom_point() +
      annotate('text', x = 5, y = 4.7, label = "setosa", size = 12)
    
    with_annotate
    
    #or if you do not want the heading to print over the plot area
    
    with_coord_cart <-
      ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
      geom_point() +
      annotate('text', x = 5, y = 4.7, label = "setosa", size = 8)+
      coord_cartesian(ylim = c(2, 4.5), clip = 'off') +
      theme(plot.margin = margin(2, 1, 1, 1, "lines"))
    
    
    with_coord_cart
    
    
    

    这给了你:

    【讨论】:

    • 我认为问题的标题明确表示:不使用 facet_
    【解决方案3】:

    注意:我已将其删除,因为在更新原始问题后它似乎不再相关。

    我不确定我是否理解正确。如果您想将不同的地块安排在一起:

    library(gridExtra)
    
    grid.arrange(without_facet,
                 without_facet,
                 without_facet,
                 without_facet, nrow = 2)
    

    【讨论】:

    • 不抱歉不是我想要的。我已经编辑了我的问题,希望它更清楚。
    【解决方案4】:

    来自 Paleolimbot 在 Gitbub 上的回答 (https://github.com/tidyverse/ggplot2/issues/2344)

    element_textbox <- function(...) {
      el <- element_text(...)
      class(el) <- c("element_textbox", class(el))
      el
    }
    
    element_grob.element_textbox <- function(element, ...) {
      text_grob <- NextMethod()
      rect_grob <- element_grob(calc_element("strip.background", theme_bw()))
    
      ggplot2:::absoluteGrob(
        grid::gList(
          element_grob(calc_element("strip.background", theme_bw())),
          text_grob
        ),
        height = grid::grobHeight(text_grob), 
        width = grid::unit(1, "npc")
      )
    }
    

    从我最初的问题中,我添加了theme_bw()

    library(ggplot2)
    library(gridExtra)
    
    df <- iris[iris$Species == 'setosa', ]
    
    with_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() +
      facet_wrap(~Species) + 
      theme(plot.background = element_rect(color = 'black')) + theme_bw()
    
    without_facet <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width)) + 
      geom_point() + 
      ggtitle("setosa")  + 
      theme_bw() +
      theme(
        plot.title = element_textbox(
          hjust = 0.5, margin = margin(t = 5, b = 5), size = 10
        ),
      )
    
    grid.arrange(with_facet, without_facet)
    

    不完全相同,但适用于我的目的。

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 2011-09-12
      • 2020-02-09
      • 1970-01-01
      相关资源
      最近更新 更多