我有一组名为“剧院”的数据
我用这段代码准备了所有数据的箱线图:
数据中有一列名为“部门”,该列中的数据设置为“住院病人”或“日间病例”。我想创建两个箱线图,一个只使用住院患者行,一个只使用日间病例行,我想使用过滤器...
如果你能帮助一个菜鸟解决他的第一个问题,非常感谢。
我试图将其标记到上面代码的末尾,但出现错误,我还在每行代码之前尝试了过滤器,认为代码的层次结构可能是一个因素 (???)
ggplot(data = theatre) +
(mapping = aes( x = speciality_groups, y = process_time, fill =
speciality_groups)) +
geom_boxplot() + labs(x = "Sector", fill = "sector") +
theme_minimal() +
theme(axis.text.x=element_text (angle =45, hjust =1))'
尝试使用:
filter(theatre, sector == "Inpatient")
我收到以下错误:
ggplot(data = theatre) + (mapping = aes(x = speciality_groups, : 二元运算符的非数字参数 另外: 警告信息: “+”的不兼容方法(“+.gg”、“Ops.data.frame”)
最佳答案
在使用 ggplot2 之前创建一个变量
library(tidyverse)
theatre_inpatient <- theatre %>%
filter(sector == "Inpatient")
theatre_inpatient_boxplot <- theatre_inpatient %>%
ggplot(., aes(x = speciality_groups, y = process_time, fill =
speciality_groups)) +
geom_boxplot() + labs(x = "Sector", fill = "sector") +
theme_minimal() +
theme(axis.text.x=element_text (angle =45, hjust =1))
然后你可以用“Day case”做同样的事情
另一种方式是使用facet_grid
library(tidyverse)
ggplot(theatre, aes(x = speciality_groups, y = process_time, fill =
speciality_groups)) +
geom_boxplot() + labs(x = "Sector", fill = "sector") +
theme_minimal() +
theme(axis.text.x=element_text (angle =45, hjust =1)) +
facet_grid(. ~ sector)
关于r - 在 ggplot 中的何处以及如何应用过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58346022/