Compare 2 data frames with ggplot
up vote
0
down vote
favorite
I have the below dataframes and want to plot 2 weeks of data with ggplot.
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)
)
Data Frame-1 (Last 7 Days)
df1<-df%>%select(everything())%>%filter(Date < Sys.Date()-1 & Date>=Sys.Date()-8)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Data Frame-2 (Last 8 days to 15 days)
df2<-df%>%select(everything())%>%filter(Date < Sys.Date()-8 & Date>=Sys.Date()-15)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Plots :
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)

Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.
r ggplot2
add a comment |
up vote
0
down vote
favorite
I have the below dataframes and want to plot 2 weeks of data with ggplot.
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)
)
Data Frame-1 (Last 7 Days)
df1<-df%>%select(everything())%>%filter(Date < Sys.Date()-1 & Date>=Sys.Date()-8)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Data Frame-2 (Last 8 days to 15 days)
df2<-df%>%select(everything())%>%filter(Date < Sys.Date()-8 & Date>=Sys.Date()-15)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Plots :
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)

Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.
r ggplot2
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the below dataframes and want to plot 2 weeks of data with ggplot.
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)
)
Data Frame-1 (Last 7 Days)
df1<-df%>%select(everything())%>%filter(Date < Sys.Date()-1 & Date>=Sys.Date()-8)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Data Frame-2 (Last 8 days to 15 days)
df2<-df%>%select(everything())%>%filter(Date < Sys.Date()-8 & Date>=Sys.Date()-15)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Plots :
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)

Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.
r ggplot2
I have the below dataframes and want to plot 2 weeks of data with ggplot.
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)
)
Data Frame-1 (Last 7 Days)
df1<-df%>%select(everything())%>%filter(Date < Sys.Date()-1 & Date>=Sys.Date()-8)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Data Frame-2 (Last 8 days to 15 days)
df2<-df%>%select(everything())%>%filter(Date < Sys.Date()-8 & Date>=Sys.Date()-15)%>%
group_by(Date,category1)%>%summarise(Total=sum(count))
Plots :
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)

Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.
r ggplot2
r ggplot2
asked Nov 20 at 4:01
sai saran
342224
342224
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14
add a comment |
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Here's an approach using facets, where I show each date of the last week as a facet, with the # of whole weeks prior on the x axis of each facet.
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())

Sample data:
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)
)
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
Date is expressed here alternatively, as the number of whole weeks ago (weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet toadj_to_this_wkbut then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version callednice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.
– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here's an approach using facets, where I show each date of the last week as a facet, with the # of whole weeks prior on the x axis of each facet.
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())

Sample data:
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)
)
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
Date is expressed here alternatively, as the number of whole weeks ago (weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet toadj_to_this_wkbut then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version callednice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.
– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
add a comment |
up vote
2
down vote
accepted
Here's an approach using facets, where I show each date of the last week as a facet, with the # of whole weeks prior on the x axis of each facet.
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())

Sample data:
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)
)
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
Date is expressed here alternatively, as the number of whole weeks ago (weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet toadj_to_this_wkbut then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version callednice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.
– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here's an approach using facets, where I show each date of the last week as a facet, with the # of whole weeks prior on the x axis of each facet.
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())

Sample data:
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)
)
Here's an approach using facets, where I show each date of the last week as a facet, with the # of whole weeks prior on the x axis of each facet.
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())

Sample data:
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)
)
edited Nov 20 at 5:34
answered Nov 20 at 4:58
Jon Spring
4,5561521
4,5561521
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
Date is expressed here alternatively, as the number of whole weeks ago (weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet toadj_to_this_wkbut then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version callednice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.
– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
add a comment |
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
Date is expressed here alternatively, as the number of whole weeks ago (weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet toadj_to_this_wkbut then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version callednice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.
– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
looks good and i have one doubt...we are comparing the 2 different date ranges but facet was used with another dates...can u please clarify how can we use dates in facet?
– sai saran
Nov 20 at 5:14
2
2
Date is expressed here alternatively, as the number of whole weeks ago (
weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet to adj_to_this_wk but then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version called nice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.– Jon Spring
Nov 20 at 5:36
Date is expressed here alternatively, as the number of whole weeks ago (
weeks_ago), and the day of this week it would align with (adj_to_this_wk). You can facet to adj_to_this_wk but then it would be formatted like "2018-11-14." To make it prettier, I made a formatted version called nice_dates, and made that a factor ordered by dates. Then it will show up as a string, but sorted chronologically instead of alphabetically.– Jon Spring
Nov 20 at 5:36
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
superb...thanks for the response and trying with real time usage data
– sai saran
Nov 20 at 5:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386038%2fcompare-2-data-frames-with-ggplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i want to compare first data frame result with second data frame result like TOTAL count needs to be compare with one week to another week
– sai saran
Nov 20 at 4:14
to know how the progress of total from one week to last week data
– sai saran
Nov 20 at 4:14