我有一个包含3个键名称,周,小时的数据框。有6个名字,我想geom_smooth()散乱的情节,但不想在平滑中包含与1个名字相关的值。这是一个图表,可以看出一个人的小时数如何影响团队的工作时间。
my data looks something like this
a<-
Week<date> Names<chr> Hours<dbl>
2017-02-27 Miki 4.50
2017-02-27 Mouse 1.20
2017-02-27 Rocks 3.70
2017-03-06 Miki 5.80
2017-03-06 Mouse 2.20
2017-03-06 Rocks 11.00
library(ggplot2)
library(scales)
library(dplyr)
ggplot(a, aes(x=Week, y=Hours))+
geom_point(aes(color=Names))+
geom_smooth(aes(group_by(subset(Hours, !("Rocks" %in% Names)))))+
geom_smooth(aes(group_by(subset(Hours, ("Rocks" %in% Names)))))+
scale_x_date(date_labels = "%Y/%m/%d", date_breaks="1 month", date_minor_breaks = "1 week")
运行上面的代码时出现此错误代码:
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('double', 'numeric')"
答案 0 :(得分:0)
您应修改数据框以排除/过滤第2层和第3层的“Rocks”。
a %>% ggplot(aes(x = Week, y = Hours)) +
geom_point(aes(color = Names)) +
geom_smooth(data = a %>% filter(!Names %in% 'Rocks')) +
geom_smooth(data = a %>% filter(Names %in% 'Rocks'))
答案 1 :(得分:0)
Week <- c("2017-02-27","2017-02-27","2017-02-27","2017-03-06","2017-03-06",
"2017-03-06")
Names <- rep(c("Miki","Mouse","Rocks"),2)
Hours <- c(4.5,1.2,3.7,5.8,2.2,11)
dt <- data.frame(Week = Week,
Names = Names,
Hours = Hours)
dt$Week <- as.Date(dt$Week)
library(ggplot2)
ggplot(dt,aes(x = Week, y = Hours)) +
geom_point(aes(group = Names,color = Names)) +
geom_smooth(data = subset(dt, dt$Names != "Rocks"),method = "loess", se = FALSE)