r - 在 ggplot 中的何处以及如何应用过滤器

标签 r ggplot2

我有一组名为“剧院”的数据

我用这段代码准备了所有数据的箱线图:

数据中有一列名为“部门”,该列中的数据设置为“住院病人”或“日间病例”。我想创建两个箱线图,一个只使用住院患者行,一个只使用日间病例行,我想使用过滤器...

如果你能帮助一个菜鸟解决他的第一个问题,非常感谢。

我试图将其标记到上面代码的末尾,但出现错误,我还在每行代码之前尝试了过滤器,认为代码的层次结构可能是一个因素 (???)

   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/

相关文章:

r - 将矩阵拆分为列表,同时保留行名

r - 在 knitr/markdown 中更改代码块颜色

R:创建时间日期 y 变量的 ggplot2 条形图

r - geom_smooth() : One line, 不同的颜色

r - ggplot 与来自数据表 (DT) 的过滤数据和 react 性小部件来选择 x 和 y 轴

r - write_xlsx(all_trips, "trips.xlsx") 错误 : Error in libxlsxwriter: 'Worksheet row or column index out of range.'

r - cordDiagram函数,R包circlize

r - 有没有办法合并 R 中的回归摘要列表?

r - R ggplot中 map 多边形的标签中心

r - 计数表中的 ggplot2 箱线图