比较2个数据框和ggplot

时间:2018-11-20 04:01:57

标签: r ggplot2

我具有以下数据框,并希望使用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)

data frame 1

ggplot(df2,aes(Date,Total,fill=category1))+geom_bar(stat = "identity",position = "stack",width = 0.8,alpha=0.8)

data frame 2

现在如何比较两个图来绘制相同的内容? 如果需要任何其他信息,请让我知道。

1 个答案:

答案 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())

enter image description here

样本数据:

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)
)