Compare 2 data frames with ggplot












0















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)


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



Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.










share|improve this question























  • 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 '18 at 4:14











  • to know how the progress of total from one week to last week data

    – sai saran
    Nov 20 '18 at 4:14
















0















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)


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



Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.










share|improve this question























  • 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 '18 at 4:14











  • to know how the progress of total from one week to last week data

    – sai saran
    Nov 20 '18 at 4:14














0












0








0








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)


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



Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.










share|improve this question














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)


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



Now how to plot the same content comparing the 2 plots ?
Any extra information if required please let me know.







r ggplot2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 4:01









sai saransai saran

348224




348224













  • 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 '18 at 4:14











  • to know how the progress of total from one week to last week data

    – sai saran
    Nov 20 '18 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 '18 at 4:14











  • to know how the progress of total from one week to last week data

    – sai saran
    Nov 20 '18 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 '18 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 '18 at 4:14













to know how the progress of total from one week to last week data

– sai saran
Nov 20 '18 at 4:14





to know how the progress of total from one week to last week data

– sai saran
Nov 20 '18 at 4:14












1 Answer
1






active

oldest

votes


















2














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


enter image description here



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





share|improve this answer


























  • 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 '18 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 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 '18 at 5:36











  • superb...thanks for the response and trying with real time usage data

    – sai saran
    Nov 20 '18 at 5:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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


enter image description here



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





share|improve this answer


























  • 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 '18 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 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 '18 at 5:36











  • superb...thanks for the response and trying with real time usage data

    – sai saran
    Nov 20 '18 at 5:41
















2














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


enter image description here



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





share|improve this answer


























  • 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 '18 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 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 '18 at 5:36











  • superb...thanks for the response and trying with real time usage data

    – sai saran
    Nov 20 '18 at 5:41














2












2








2







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


enter image description here



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





share|improve this answer















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


enter image description here



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 5:34

























answered Nov 20 '18 at 4:58









Jon SpringJon Spring

5,4131625




5,4131625













  • 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 '18 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 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 '18 at 5:36











  • superb...thanks for the response and trying with real time usage data

    – sai saran
    Nov 20 '18 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 '18 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 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 '18 at 5:36











  • superb...thanks for the response and trying with real time usage data

    – sai saran
    Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 5:36













superb...thanks for the response and trying with real time usage data

– sai saran
Nov 20 '18 at 5:41





superb...thanks for the response and trying with real time usage data

– sai saran
Nov 20 '18 at 5:41


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory