我有一个数据框,其中包含对每个test_subject执行的每个test_type的test_outcome(PASS / FAIL)。例如:
test_subject, test_type, test_outcome
person_a, height, PASS
person_b, height, PASS
person_c, height, FAIL
person_d, height, PASS
person_a, weight, FAIL
person_b, weight, FAIL
person_c, weight, PASS
person_d, weight, PASS
我想通过test_type和test_subject准备一个屈服图。
Y-axis = yield i.e. num pass/(num pass + num fail)
X-axis = test_subject
fill: = A line for each test_type.
我更愿意使用ggplot2,请您在此推荐最佳方法吗?例如如何在绘图之前重新整形数据?
答案 0 :(得分:1)
快速回答,您需要根据所需的颜色等来整理图表。
library(dplyr)
library(ggplot2)
dat <- dat %>% group_by(test_subject, test_type) %>%
summarise(passrate = sum(test_outcome=="PASS") / n())
ggplot(dat, aes(x = test_subject, y = passrate, fill = test_type)) +
geom_bar(stat = "identity", position = "dodge")
编辑:请求了折线图。通常情况下,分类组不应该通过折线图连接 - 因为没有理由以特定方式对它们进行排序。
ggplot(dat, aes(x = test_subject, y = passrate, col = test_type)) +
geom_line(aes(group = test_type)) +
geom_point()