按时间段绘制R中的唯一组
如我们所见,按时间段绘制R中的唯一组,r,ggplot2,dplyr,tidyr,R,Ggplot2,Dplyr,Tidyr,如我们所见,x和y是组变量(我们只有组类别q-q、w-w、e-e) 1月1日 mydat=structure(list(date = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("01.01.2018", "02.01.2018"), class = "factor"), x = structure(c(2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L), .Label = c("e",
x
和y
是组变量(我们只有组类别q-q、w-w、e-e)
1月1日
mydat=structure(list(date = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L), .Label = c("01.01.2018", "02.01.2018"), class = "factor"),
x = structure(c(2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L), .Label = c("e",
"q", "w"), class = "factor"), y = structure(c(2L, 2L, 2L,
3L, 1L, 1L, 1L, 1L, 1L), .Label = c("e", "q", "w"), class = "factor")), .Names = c("date",
"x", "y"), class = "data.frame", row.names = c(NA, -9L))
然后是1月2日
q q = count 3
w w =count 1
类别数在图形中的显示方式如下:数据集很大,所以需要1月份的图形,所以绘图显示每天售出的类别数
我发现你的问题不太清楚,但这可能会有帮助:
e e =count 5
你的问题并不像我希望的那么清楚,但我想你想知道我们每个小组每天有多少人,对吗 您可以使用
dplyr
软件包中的group\u
我创建了一个名为group的新变量,它包含x
和y
mydata%
突变('group'=粘贴(x,y,sep='-'))%>%
分组依据(日期,分组)%>%
总结('qtd'=长度(组))
结果:
日期组qtd
2018年1月1日q-q 3
2018年1月1日w-w 1
2018年1月2日e-e 5
您可以使用ggplot2
打包并创建如下内容,在其中可以使用facet\u wrap
按日期分隔绘图:
ggplot(数据=mydata,aes(x=group,y=qtd))+
几何图形栏(stat='identity')+
面_包装(~日期)
否则,您可以使用另一种语法ggplot2
并使用fill
。如果你有很多约会,有时候会更好
代码:
ggplot(数据=mydata,aes(x=group,y=qtd,fill=date))+
几何图形栏(stat='identity')
祝你好运 我可以再问你一个问题吗?我编辑了这篇文章。这里是加总数据集(q),其中度量变量是价格,使用求和函数按日期、x和y进行聚合。如何获取x
轴上的日期和y
轴上的x+y组以及每个组x+y
行的价格图。或者我应该打开一个新主题?我想你可以问一个新问题,但我的建议是试着自己做,学习如何绘图:一些小提示(这是上面代码的一个未经测试的调整),q%>%mutate(group=paste(x,y,sep='-'),day=day(dmy(date))%%>%ggplot(aes)(x=as.factor(day),y=price,group=group))+geom_line()
。
library(lubridate) # manipulate date
library(tidyverse) # manipulate data and plot
# your data
mydat %>%
# add columns (here my doubts)
mutate(group = paste (x,y, sep ='-'), # here the category pasted
cnt = ifelse(paste (x,y, sep ='-') == 'q-q',3,
ifelse(paste (x,y, sep ='-') == 'w-w',1,5)), # ifelse with value
day = day(dmy(date))) %>% # day
group_by(group,day) %>% # grouping
summarise(cnt = sum(cnt)) %>% # add the count as sum
# now the plot, here other doubts on your request
ggplot(aes(x = as.factor(day), y = cnt, group = group, fill = group, label = group)) +
geom_bar(stat = 'identity', position = 'dodge') +
geom_label(position = position_dodge(width = 1)) +
theme(legend.position="none")