【问题标题】:How to make a boxplot with subset values and all values on the same plot?如何制作具有子集值和同一图上所有值的箱线图?
【发布时间】:2020-02-13 20:14:40
【问题描述】:

我正在使用基本 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 合并为一个情节。

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

【问题讨论】:

    标签: r plot boxplot


    【解决方案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")
    

    【讨论】:

    • 图书馆(dplyr),对吧?太感谢了!感谢您的帮助!
    • 是的,很抱歉错过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2017-11-25
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多