如何用子集值和同一图上的所有值制作箱线图?

时间:2019-10-17 01:22:34

标签: r plot boxplot

我正在处理基准R图。

我想创建一个箱图,其中包括子集值和同一图上的所有值。关键是它必须在基本R图中。

Party <- rep(c("Rep", "Dem", "Ind"), 50)
Values <- sample(1:100, size = length(Party), replace = T)

hw <- data.frame(Party, Values)

#Plot 1
boxplot(hw$Values, 
        col=c("green"),
        xlab = "All Respondents")
#Plot 2
boxplot(hw$Values~hw$Party, 
        col=c("blue", "purple", "red"),
        xlab = "Partisan Respondents")


我实质上希望将图1和图2合并为一个图。

您能提供的任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

添加所有值组的简单方法。

library(dplyr)   # edited line

hw2 <- hw %>% 
  bind_rows(hw %>% mutate(Party = "ALL"))

boxplot(Values ~ Party, data = hw2,
        col = c("gray60", "blue", "purple", "red"),
        xlab = "Partisan Respondents")