在for
摘要功能上应用dplyr
循环时,我得到奇怪的结果-不知道为什么或如何解决它。
test <- data.frame(title = c("a", "b", "c","a","b","c", "a", "b", "c","a","b","c"),
category = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B"),
sex = c("m", "m", "m", "f", "f", "f", "m", "m", "m", "f", "f", "f"),
salary = c(50,70,90,40,60,85, 220,270,350,180,200,330))
category_list <- unique(test$category)
tmp = list()
for (category in category_list) {
# Create an average salary line for the category
tmp[category] <- test %>%
filter(category == category) %>%
summarise(mean(salary))
print(tmp)
}
我得到这个作为输出
$A
[1] 162.0833
$A
[1] 162.0833
$B
[1] 162.0833
其中group_by()
函数返回适当的结果:
test %>% group_by(category) %>% summarise(mean(salary))
# A tibble: 2 x 2
category `mean(salary)`
<fct> <dbl>
1 A 65.8
2 B 258.
替换特定类别确实会返回适当的结果,
test %>%
filter(category == "A") %>%
summarise(mean(salary))
mean(salary)
1 65.83333
所以category_list
对象也许有问题吗?
令人惊讶的是,当我调用category_list
对象的第一个元素时,我也得到了适当的答案:
test %>%
+ filter(category == category_list[1]) %>%
+ summarise(mean(salary))
mean(salary)
1 65.83333
我想弄清楚(而不使用group_by
的原因是因为我试图制作一个脚本,该脚本将创建多个ggplot对象,然后将这些对象与{{1 }}库。
也许我错了,可以使用gridExtra
,但是我想到的唯一方法是使用以下伪代码:
group_by
创建一个在category
参数中使用的均值列表geom_hline()
子集一个数据帧对象,每个子集将以其category
在ggplot中使用geom_hline()
创建一个打印对象列表category
循环之外使用grid.arrange()
库中的gridExtra
将每个图组合在一起到目前为止,这是我的代码(无法正常工作):
for
我想要的输出是这样:
答案 0 :(得分:1)
for循环中的问题是语句filter(category == category)
。总是如此,因为这两次都从您的数据中提取category
。如果您确实需要for循环,只需在for循环中重命名迭代器即可。
但是,您根本不需要grid.arrange
。 facet_wrap
可以为您提供所需的确切信息(您可能需要对构面标签进行一些重新格式化,可以使用以strip
开头的主题元素进行控制):
category_means <- test %>%
group_by(category) %>%
summarize_at(vars(salary), mean)
p <- test %>%
# group_by(category) %>%
ggplot(aes(x = title, y = salary, color = sex)) +
facet_wrap(~ category, nrow = 1, scales = "free_y") +
geom_line(color = 'white') +
geom_point() +
scale_color_manual(values = c("#F49171", "#81C19C")) +
geom_hline(data = category_means, aes(yintercept = salary), color = 'white', alpha = 0.6, size = 1) +
theme(legend.position = "none",
panel.background = element_rect(color = "#242B47", fill = "#242B47"),
plot.background = element_rect(color = "#242B47", fill = "#242B47"),
axis.line = element_line(color = "grey48", size = 0.05, linetype = "dotted"),
axis.text = element_text(family = "Georgia", color = "white"),
axis.text.x = element_text(angle = 90),
# Get rid of the y- and x-axis titles
axis.title.y=element_blank(),
axis.title.x=element_blank(),
panel.grid.major.y = element_line(color = "grey48", size = 0.05),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank())
p