我具有以下数据框,并希望使用ggplot绘制2周的数据。
df<-data.frame(
Date=sample(seq(as.Date('2018-10-25'), as.Date('2018-11-20'), by = "day"), 100,replace = T),
category1=sample(letters[1:6],100,replace = T),
count=sample(1:1000,100,replace = T)
)
数据框架1(过去7天)
df1<-df%>%select(everything())%>%filter(Date < Sys.Date()-1 & Date>=Sys.Date()-8)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
数据框2(过去8天到15天)
df2<-df%>%select(everything())%>%filter(Date < Sys.Date()-8 & Date>=Sys.Date()-15)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
图:
ggplot(df1,aes(Date,Total,fill=category1))+geom_bar(stat = "identity",position = "stack",width = 0.8,alpha=0.8)
ggplot(df2,aes(Date,Total,fill=category1))+geom_bar(stat = "identity",position = "stack",width = 0.8,alpha=0.8)
现在如何比较两个图来绘制相同的内容? 如果需要任何其他信息,请让我知道。
答案 0 :(得分:2)
这是一种使用构面的方法,在该方法中,我将上周的每个日期显示为构面,并在每个构面的x轴上显示整周的前#个值。
library(tidyverse)
df1 <- df %>%
mutate(days_old = (as.Date("2018-11-20") - Date) / lubridate::ddays(1),
weeks_ago = days_old %/% 7,
adj_to_this_week = as.Date("2018-11-20") - days_old %% 7) %>%
group_by(adj_to_this_week, weeks_ago, category1) %>%
summarise(Total=sum(count))
ggplot(df1 %>%
filter(weeks_ago <= 1) %>%
mutate(nice_dates = format(adj_to_this_week, "%b %d") %>%
fct_reorder(adj_to_this_wk)),
aes(-weeks_ago, Total,fill=category1)) +
geom_bar(stat = "identity",position = "stack",width = 0.8,alpha=0.8) +
scale_x_continuous(breaks = -1:0, labels = c("LW", "TW")) +
facet_wrap(~nice_dates, nrow = 1) +
labs(title = "Last week (LW) vs. This week (TW)", x ="") +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
样本数据:
set.seed(42)
df<-data.frame(
Date=sample(seq(as.Date('2018-10-25'), as.Date('2018-11-20'), by = "day"), 100,replace = T),
category1=sample(letters[1:6],100,replace = T),
count=sample(1:1000,100,replace = T)
)