【问题标题】:Highlight points on grouped box plot分组箱线图上的亮点
【发布时间】:2021-03-26 01:24:08
【问题描述】:

这是一个不同的问题,但从以下问题开始: R boxplot Subset column based on value in another column

更新

我的数据集如下所示:

Term Name True Result Gender
T1 Name1 True 4 F
T2 Name2 False 6 F
T3 Name3 True 5.5 M
T3 Name4 False 4.6 M

测试数据集:

dataset_test <- structure(list(Term = c("T1", "T1", "T1", "T1", "T1", "T1", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T3", "T3", "T3", "T3", "T3", 
"T3", "T3"), Name = c("Name1", "Name2", "Name3", "Name4", "Name5", 
"Name6", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10", 
"Name11", "Name12", "Name13", "Name14", "Name15", "Name16", "Name17", 
"Name18"), TRUE. = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, 
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
FALSE, TRUE, TRUE), Result = c(4, 5, 6, 4, 5, 6, 5.5, 4.6, 5.5, 
4.6, 5, 5.2, 6, 5.5, 4, 5.5, 4.8, 5, 5, 4.4), Gender = c("F", 
"F", "F", "M", "M", "M", "F", "F", "F", "F", "M", "M", "M", "F", 
"F", "F", "F", "M", "M", "M")), class = "data.frame", row.names = c(NA, 
-20L))

我在下面有一个按性别分组的箱线图。我希望能够突出显示正确性别箱线图中的点,即这些点需要与 True 记录的性别对齐。

解决方案归功于 chemdork123

dataset_test %>% 
  group_by(Term) %>% 
  filter(any(TRUE.)) %>%
  ggplot(aes(x = Term, y = Result, fill = Gender)) + 
  scale_fill_brewer(palette = "Blues") +
  geom_boxplot(position=position_dodge(0.8))+
  geom_point(                               # add the highlight points
    data=subset(dataset_test, TRUE. == TRUE), 
    aes(x=Term, y=Result), position=position_dodge(0.8),
    color="blue", size=4, show.legend = FALSE) +
  ggtitle("Distribution of results by term") +
  xlab("Term ") + ylab("Result)")

如果有两性的真实记录,位置闪避现在可以完美地工作。但是如果只有一个就会中断。然而,这是这种可视化的主要用例。

上面的代码产生了这个:

再次感谢任何帮助。

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    您可能很接近:您需要在geom_point() 呼叫中使用position_dodge。为了确保这些点与箱线图的位置正确对齐,您还应该为箱线图几何明确定义position_dodgewidth。我还在这里为geom_point() 包括show.legend=FALSE,因为您可能不希望像示例中那样在图例上使用蓝点:

    dataset %>% 
      group_by(Term) %>% 
      filter(any(TRUE.)) %>%
      ggplot(aes(x = Term, y = Result, fill = Gender)) + 
      scale_fill_brewer(palette = "Blues") +
      geom_boxplot(position=position_dodge(0.8))+
      geom_point(                               # add the highlight points
        data=subset(dataset, TRUE. == TRUE), 
        aes(x=Term, y=Result), position=position_dodge(0.8),
        color="blue", size=4, show.legend = FALSE) +
      ggtitle("Distribution of results by term") +
      xlab("Term ") + ylab("Result)")
    

    【讨论】:

    • 非常感谢 :) 在测试数据中效果很好,在真实数据中似乎有问题。我认为这是因为 True False 不是布尔值,但我不确定。
    • 我发现了问题所在 - 我只有一个性别且有真实记录。我需要更新问题。
    • @Keelin。给您带来问题的真实数据是什么?感谢您的编辑,但如果我们无法清楚地看到问题,则不清楚您希望解决什么问题。
    • 谢谢@chemdork123。我很抱歉我没有说清楚。我用更新的数据和更新的输出更新了问题。当我对数据集进行子集化以仅在 geom_point 行中显示 True 值时(data=subset(dataset, TRUE. == TRUE); 并且只有一个性别 = TRUE,这些点不会显示在右侧的箱形图中。
    • 再次感谢@chemdork123。您的解决方案帮助我找出了我在真实数据中遇到的问题,再次感谢。我接受了解决方案。而不是子集,我通过以下方式突出显示该点: geom_point(aes(color = HighlightPoint ==TRUE, alpha = .8), position = position_dodge(width=0.85), size = 4, show.legend = F)
    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 2020-05-22
    • 2021-08-28
    • 2016-09-14
    • 2013-05-11
    • 2021-03-08
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多