1
我有一个数据帧,我想绘制所有从“值” 矢量的3行。前两行是由“group”组成的值向量,第三行是UNGROUPED值向量。我目前的做法是对DPLYR执行2次调用并创建2个数据帧,然后合并它们,然后绘制合并的数据帧。有没有一种更简单的方法可以避免2次调用DPLYR?是这里所需的数据帧的合并
d = data.frame(ym = rep(c(20011,20012,20023),3), group = c(0,0,1,0,1,0,1,0,1), value = c(1,2,3,4,2,1,3,3,2))
############### 1st call to dplyr to create plot with 2 lines grouped by "group"
d2 = d %>%
group_by(ym,group) %>%
summarise(
Value = mean(value)
)
d2= as.data.frame(d2)
d2
ggplot(data=d2 , aes(x=ym, y=Value, group=as.factor(group), colour = as.factor(group))) +
geom_line() + geom_point()
###second call to dplyr to create a second data frame just for the UNGROUPED data
d3 = d %>%
group_by(ym) %>%
summarise(
Value = mean(value)
)
#### merge the data TWO frames
d3 =as.data.frame(d3)
d3$group=2
d4 = rbind(d2,d3)
### plot all 3 lines
ggplot(data=d4 , aes(x=ym, y=Value, group=as.factor(group), colour = as.factor(group))) +
geom_line() + geom_point()
在这种情况下的操作是“堆积”,而不是“合并”。 – eipi10