【问题标题】:Align data points with missing values in ggplot2 without faceting将数据点与 ggplot2 中的缺失值对齐,无需刻面
【发布时间】:2020-06-20 21:53:19
【问题描述】:

我有一个(非常抽象地)看起来像这样的数据框。请注意,并非每个品尝者都会对每种水果进行评分。

df <- data.frame(
  fruit = c("apple", "apple", "apple", "banana", "banana", "banana"),
  taster = c("Ann", "Bob", "Don", "Bob", "Cat", "Don"),
  rating = c(8, 7, 4, 6, 3, 7)
)

我想使用ggplot2 创建两个图,一个用于苹果,一个用于香蕉。我想要表示评分的点,这些点需要用线连接。至关重要的是,即使 Ann 没有对香蕉进行评分,Cat 也没有对苹果进行评分,我仍然希望这两个图的 x 轴上都有所有四个评分者。在苹果图中,Bob 的评分点应该直接连接到 Don 的评分点,跳过 Cat 列。在香蕉图中,Bob、Cat 和 Don 将只有三个连接点; Ann 列中不会出现任何内容。

可以通过分面来实现这一点,如下所示:

.

plot_facet <- ggplot(df, aes(x = taster, y = rating, col = fruit, group = fruit)) +
  geom_point() + geom_line() +
  scale_color_manual(values=c("#123456", "#abcdef")) +
  facet_grid(fruit ~ .)

不过,出于一个复杂的独立原因,我不想使用分面。相反,我想制作两个单独的图表并将它们与(例如)ggarrange() 缝合在一起。但是当我这样做时,结果是这样的:

香蕉图中省略了 Ann 列,苹果图中省略了 Cat 列。我不喜欢的是,很难直观地比较一个人对这两种水果的评分。

plot_apple <- ggplot(subset(df, fruit == "apple"), 
                     aes(x = taster, y = rating, col = fruit, group = fruit)) +
  geom_point() + geom_line() +
  scale_color_manual(values=c("#123456"))

plot_banana <- ggplot(subset(df, fruit == "banana"), 
                     aes(x = taster, y = rating, col = fruit, group = fruit)) +
  geom_point() + geom_line() +
  scale_color_manual(values=c("#abcdef"))

ggarrange(plot_apple, plot_banana, ncol = 1, nrow = 2)

如何防止这种情况发生?我尝试将具有空值的行添加到我的数据框中,但这没有帮助。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以将名称转换为因子,然后在 scale_x_discrete 调用中使用 drop = FALSE。这是一个代表:

    library(ggplot2)
    library(ggpubr)
    
    df <- data.frame(
      fruit = c("apple", "apple", "apple", "banana", "banana", "banana"),
      taster = c("Ann", "Bob", "Don", "Bob", "Cat", "Don"),
      rating = c(8, 7, 4, 6, 3, 7)
    )
    
    df$taster <- factor(df$taster)
    
    plot_apple <- ggplot(subset(df, fruit == "apple"), 
                         aes(x = taster, y = rating, col = fruit, group = fruit)) +
      geom_point() + geom_line() +
      scale_color_manual(values=c("#123456"))+
      scale_x_discrete(drop = FALSE)
    
    plot_banana <- ggplot(subset(df, fruit == "banana"), 
                          aes(x = taster, y = rating, col = fruit, group = fruit)) +
      geom_point() + geom_line() +
      scale_color_manual(values=c("#abcdef")) +
      scale_x_discrete(drop = FALSE)
    
    ggarrange(plot_apple, plot_banana, ncol = 1, nrow = 2)
    

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

    【讨论】:

    • 我想用另一个包,比如拼凑而成,x 会更好地对齐......(?)
    • 啊,很简单!非常感谢。
    【解决方案2】:

    另一个更老套的选择。 (不过,我更喜欢艾伦的解决方案!)。该解决方案还需要先对变量进行因式分解!

    诀窍是按照Z.Lin's great hack! 中的建议更改cowplot::plot_grid 函数

    df <- data.frame(
      fruit = c("apple", "apple", "apple", "banana", "banana", "banana"),
      taster = c("Ann", "Bob", "Don", "Bob", "Cat", "Don"),
      rating = c(8, 7, 4, 6, 3, 7), stringsAsFactors = TRUE ## factorised
    )
    
    # trace(cowplot::plot_grid, edit = TRUE)
    ## following Z.Lin's suggesting to modify it only in the session. 
    ## Then I used the function `align_plots_modified()` from her post which I will not post again here
    
    ## The labels need some tweaking
    plot_apple <- ggplot(subset(df, fruit == "apple"), 
                         aes(x = as.integer(taster), y = rating, col = fruit, group = fruit)) +
      scale_x_continuous(breaks = 1:4, labels = levels(df$taster)) + #added labels and breaks
      geom_point() + 
      geom_line() +
      scale_color_manual(values=c("#123456"))
    
    plot_banana <- ggplot(subset(df, fruit == "banana"), 
                          aes(x = as.integer(taster), y = rating, col = fruit, group = fruit)) +
      geom_point() + 
      geom_line() +
      scale_x_continuous(breaks = 1:4, labels = levels(df$taster)) +
      scale_color_manual(values=c("#abcdef"))
    
    cowplot::plot_grid(plot_apple, plot_banana, nrow = 2, align = "v")
    
    # untrace(cowplot::plot_grid)
    

    【讨论】:

    • 感谢您的建议,并为我指点 Z.Lin 的帖子!
    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多