How to shift the starting point of the X axis in ggplot?












0















Let's say I have a dataframe containing 365 observations of a variable, and 365 dates, one for each day of the year.



I want to plot this sequence using ggplot, but I want the plot to begin at an arbitrary date mid-way through the year, and plot all 365 observations, with the dates before the starting point appearing at the end of the sequence.



What can I do, either to the dataframe itself, or to the plot aesthetics, to achieve this?



Updated with reproducible data and more context



Below is example code which should illustrate how I ended up with this problem.



#ten years of data ordered by hydro year

dates <- seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by="days")
values <- runif(3653)
df = data.frame(Date=as.Date(dates), Val=values)

> head(df)
Date Val
1 2000-10-01 0.9868603
2 2000-10-02 0.6461032
3 2000-10-03 0.7823848
4 2000-10-04 0.9914216
5 2000-10-05 0.8171412
6 2000-10-06 0.3213551

#created new df containing the average of all ten years of measurements for each day of the year

df2 <- df %>% mutate(Day=day(dates), Month =month(dates)) %>%
group_by(Month, Day) %>%
summarize(Multiyearmean=mean(Val))

> head(df2)
# A tibble: 6 x 3
# Groups: Month [1]
Month Day Multiyearmean
<dbl> <int> <dbl>
1 1 1 0.272
2 1 2 0.577
3 1 3 0.269
4 1 4 0.534
5 1 5 0.607
6 1 6 0.649


My values are still associated with the correct month and day dates, but they are now ordered Jan-Dec, instead Oct-Sep.



Is that interpretation correct?



How can I reorder them for plotting?



How can I achieve the creating the multiyearmean as described here without breaking my date sequence?










share|improve this question




















  • 1





    I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

    – neilfws
    Nov 25 '18 at 22:29











  • This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

    – Clayton Glasser
    Nov 25 '18 at 22:34






  • 1





    It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

    – neilfws
    Nov 25 '18 at 22:39











  • If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

    – hrbrmstr
    Nov 25 '18 at 22:46
















0















Let's say I have a dataframe containing 365 observations of a variable, and 365 dates, one for each day of the year.



I want to plot this sequence using ggplot, but I want the plot to begin at an arbitrary date mid-way through the year, and plot all 365 observations, with the dates before the starting point appearing at the end of the sequence.



What can I do, either to the dataframe itself, or to the plot aesthetics, to achieve this?



Updated with reproducible data and more context



Below is example code which should illustrate how I ended up with this problem.



#ten years of data ordered by hydro year

dates <- seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by="days")
values <- runif(3653)
df = data.frame(Date=as.Date(dates), Val=values)

> head(df)
Date Val
1 2000-10-01 0.9868603
2 2000-10-02 0.6461032
3 2000-10-03 0.7823848
4 2000-10-04 0.9914216
5 2000-10-05 0.8171412
6 2000-10-06 0.3213551

#created new df containing the average of all ten years of measurements for each day of the year

df2 <- df %>% mutate(Day=day(dates), Month =month(dates)) %>%
group_by(Month, Day) %>%
summarize(Multiyearmean=mean(Val))

> head(df2)
# A tibble: 6 x 3
# Groups: Month [1]
Month Day Multiyearmean
<dbl> <int> <dbl>
1 1 1 0.272
2 1 2 0.577
3 1 3 0.269
4 1 4 0.534
5 1 5 0.607
6 1 6 0.649


My values are still associated with the correct month and day dates, but they are now ordered Jan-Dec, instead Oct-Sep.



Is that interpretation correct?



How can I reorder them for plotting?



How can I achieve the creating the multiyearmean as described here without breaking my date sequence?










share|improve this question




















  • 1





    I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

    – neilfws
    Nov 25 '18 at 22:29











  • This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

    – Clayton Glasser
    Nov 25 '18 at 22:34






  • 1





    It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

    – neilfws
    Nov 25 '18 at 22:39











  • If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

    – hrbrmstr
    Nov 25 '18 at 22:46














0












0








0








Let's say I have a dataframe containing 365 observations of a variable, and 365 dates, one for each day of the year.



I want to plot this sequence using ggplot, but I want the plot to begin at an arbitrary date mid-way through the year, and plot all 365 observations, with the dates before the starting point appearing at the end of the sequence.



What can I do, either to the dataframe itself, or to the plot aesthetics, to achieve this?



Updated with reproducible data and more context



Below is example code which should illustrate how I ended up with this problem.



#ten years of data ordered by hydro year

dates <- seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by="days")
values <- runif(3653)
df = data.frame(Date=as.Date(dates), Val=values)

> head(df)
Date Val
1 2000-10-01 0.9868603
2 2000-10-02 0.6461032
3 2000-10-03 0.7823848
4 2000-10-04 0.9914216
5 2000-10-05 0.8171412
6 2000-10-06 0.3213551

#created new df containing the average of all ten years of measurements for each day of the year

df2 <- df %>% mutate(Day=day(dates), Month =month(dates)) %>%
group_by(Month, Day) %>%
summarize(Multiyearmean=mean(Val))

> head(df2)
# A tibble: 6 x 3
# Groups: Month [1]
Month Day Multiyearmean
<dbl> <int> <dbl>
1 1 1 0.272
2 1 2 0.577
3 1 3 0.269
4 1 4 0.534
5 1 5 0.607
6 1 6 0.649


My values are still associated with the correct month and day dates, but they are now ordered Jan-Dec, instead Oct-Sep.



Is that interpretation correct?



How can I reorder them for plotting?



How can I achieve the creating the multiyearmean as described here without breaking my date sequence?










share|improve this question
















Let's say I have a dataframe containing 365 observations of a variable, and 365 dates, one for each day of the year.



I want to plot this sequence using ggplot, but I want the plot to begin at an arbitrary date mid-way through the year, and plot all 365 observations, with the dates before the starting point appearing at the end of the sequence.



What can I do, either to the dataframe itself, or to the plot aesthetics, to achieve this?



Updated with reproducible data and more context



Below is example code which should illustrate how I ended up with this problem.



#ten years of data ordered by hydro year

dates <- seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by="days")
values <- runif(3653)
df = data.frame(Date=as.Date(dates), Val=values)

> head(df)
Date Val
1 2000-10-01 0.9868603
2 2000-10-02 0.6461032
3 2000-10-03 0.7823848
4 2000-10-04 0.9914216
5 2000-10-05 0.8171412
6 2000-10-06 0.3213551

#created new df containing the average of all ten years of measurements for each day of the year

df2 <- df %>% mutate(Day=day(dates), Month =month(dates)) %>%
group_by(Month, Day) %>%
summarize(Multiyearmean=mean(Val))

> head(df2)
# A tibble: 6 x 3
# Groups: Month [1]
Month Day Multiyearmean
<dbl> <int> <dbl>
1 1 1 0.272
2 1 2 0.577
3 1 3 0.269
4 1 4 0.534
5 1 5 0.607
6 1 6 0.649


My values are still associated with the correct month and day dates, but they are now ordered Jan-Dec, instead Oct-Sep.



Is that interpretation correct?



How can I reorder them for plotting?



How can I achieve the creating the multiyearmean as described here without breaking my date sequence?







r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 23:19







Clayton Glasser

















asked Nov 25 '18 at 22:19









Clayton GlasserClayton Glasser

518




518








  • 1





    I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

    – neilfws
    Nov 25 '18 at 22:29











  • This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

    – Clayton Glasser
    Nov 25 '18 at 22:34






  • 1





    It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

    – neilfws
    Nov 25 '18 at 22:39











  • If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

    – hrbrmstr
    Nov 25 '18 at 22:46














  • 1





    I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

    – neilfws
    Nov 25 '18 at 22:29











  • This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

    – Clayton Glasser
    Nov 25 '18 at 22:34






  • 1





    It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

    – neilfws
    Nov 25 '18 at 22:39











  • If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

    – hrbrmstr
    Nov 25 '18 at 22:46








1




1





I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

– neilfws
Nov 25 '18 at 22:29





I have to ask: why? If the dates are within one calendar year, this just sounds like a confusing and misleading visualisation.

– neilfws
Nov 25 '18 at 22:29













This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

– Clayton Glasser
Nov 25 '18 at 22:34





This is for the purposes of representing rainfall data, which is a context wherein the "hydrological year" is a preferred unit of time to the "calendar year". So the plot should begin Oct 1 and end Sep 30. But the dataframe runs Jan 1 to Dec 30, since the dates are ordered ascending or descending by default.

– Clayton Glasser
Nov 25 '18 at 22:34




1




1





It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

– neilfws
Nov 25 '18 at 22:39





It would help to see some example data from the data frame. ggplot2 should have no problem with dates, but it rather depends how dates are defined in your data.

– neilfws
Nov 25 '18 at 22:39













If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

– hrbrmstr
Nov 25 '18 at 22:46





If the dates are "ordered by default" they mustn't have the previous and "current" year set correctly.

– hrbrmstr
Nov 25 '18 at 22:46












2 Answers
2






active

oldest

votes


















1














EDIT: Original answer created fake data since none originally provided in OP. Now uses suggested df example data. (Thanks for adding, btw! Simplifies answering.)



I'd suggest adjusting your dates so they are a continuous range from one Oct 1 through the next Sept 30. That way you can plot in ggplot using dates, but with the alignment you prefer.



For instance, taking your data, we could adjust it to all go into one Oct-Sep year (ending 2020 so we capture Feb 29's).



df2b <- df %>%
mutate(date_hydro = lubridate::ymd(paste(
if_else(month(Date) < 10, 2020, 2019), # 2020 is leap year
month(Date), day(Date))
)) %>%
group_by(date_hydro) %>%
summarize(multiyearmean = mean(Val))


Then we can plot the daily averages within the hydrological year.



ggplot(df2b, aes(date_hydro, multiyearmean)) + 
geom_point() +
scale_x_date(date_labels = "%b", date_breaks = "1 month",
minor_breaks = NULL) +
theme(axis.text.x = element_text(hjust = 0))


enter image description here






share|improve this answer


























  • Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

    – Clayton Glasser
    Nov 25 '18 at 23:24











  • Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

    – Jon Spring
    Nov 26 '18 at 2:57











  • Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

    – Clayton Glasser
    Nov 26 '18 at 17:44











  • Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

    – Clayton Glasser
    Nov 26 '18 at 17:51



















1














Somewhat similar to the other answer but using your simulation:



set.seed(2018 - 11 - 25) # reproducible data

data.frame(
dates = seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by = "days"),
values = runif(3653)
) -> xdf

mutate(
xdf,
day = lubridate::day(dates),
month = lubridate::month(dates)
) %>%
group_by(month, day) %>%
summarize(multi_year_mean = mean(values)) %>%
ungroup() %>%
mutate(plot_date = case_when( # use "real" date axis and wrap-around
month >= 10 ~ as.Date(sprintf("2019-%02s-%02s", month, day)),
TRUE ~ as.Date(sprintf("2020-%02s-%02s", month, day)) # account for leap year(s)
)) %>%
ggplot(aes(plot_date, multi_year_mean)) +
geom_point() +
scale_x_date(expand=c(0,0.75), date_breaks = "1 month", date_labels = "%b") # adjust aesthetics as necessary


enter image description here






share|improve this answer























    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%2f53472578%2fhow-to-shift-the-starting-point-of-the-x-axis-in-ggplot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    EDIT: Original answer created fake data since none originally provided in OP. Now uses suggested df example data. (Thanks for adding, btw! Simplifies answering.)



    I'd suggest adjusting your dates so they are a continuous range from one Oct 1 through the next Sept 30. That way you can plot in ggplot using dates, but with the alignment you prefer.



    For instance, taking your data, we could adjust it to all go into one Oct-Sep year (ending 2020 so we capture Feb 29's).



    df2b <- df %>%
    mutate(date_hydro = lubridate::ymd(paste(
    if_else(month(Date) < 10, 2020, 2019), # 2020 is leap year
    month(Date), day(Date))
    )) %>%
    group_by(date_hydro) %>%
    summarize(multiyearmean = mean(Val))


    Then we can plot the daily averages within the hydrological year.



    ggplot(df2b, aes(date_hydro, multiyearmean)) + 
    geom_point() +
    scale_x_date(date_labels = "%b", date_breaks = "1 month",
    minor_breaks = NULL) +
    theme(axis.text.x = element_text(hjust = 0))


    enter image description here






    share|improve this answer


























    • Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

      – Clayton Glasser
      Nov 25 '18 at 23:24











    • Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

      – Jon Spring
      Nov 26 '18 at 2:57











    • Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

      – Clayton Glasser
      Nov 26 '18 at 17:44











    • Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

      – Clayton Glasser
      Nov 26 '18 at 17:51
















    1














    EDIT: Original answer created fake data since none originally provided in OP. Now uses suggested df example data. (Thanks for adding, btw! Simplifies answering.)



    I'd suggest adjusting your dates so they are a continuous range from one Oct 1 through the next Sept 30. That way you can plot in ggplot using dates, but with the alignment you prefer.



    For instance, taking your data, we could adjust it to all go into one Oct-Sep year (ending 2020 so we capture Feb 29's).



    df2b <- df %>%
    mutate(date_hydro = lubridate::ymd(paste(
    if_else(month(Date) < 10, 2020, 2019), # 2020 is leap year
    month(Date), day(Date))
    )) %>%
    group_by(date_hydro) %>%
    summarize(multiyearmean = mean(Val))


    Then we can plot the daily averages within the hydrological year.



    ggplot(df2b, aes(date_hydro, multiyearmean)) + 
    geom_point() +
    scale_x_date(date_labels = "%b", date_breaks = "1 month",
    minor_breaks = NULL) +
    theme(axis.text.x = element_text(hjust = 0))


    enter image description here






    share|improve this answer


























    • Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

      – Clayton Glasser
      Nov 25 '18 at 23:24











    • Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

      – Jon Spring
      Nov 26 '18 at 2:57











    • Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

      – Clayton Glasser
      Nov 26 '18 at 17:44











    • Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

      – Clayton Glasser
      Nov 26 '18 at 17:51














    1












    1








    1







    EDIT: Original answer created fake data since none originally provided in OP. Now uses suggested df example data. (Thanks for adding, btw! Simplifies answering.)



    I'd suggest adjusting your dates so they are a continuous range from one Oct 1 through the next Sept 30. That way you can plot in ggplot using dates, but with the alignment you prefer.



    For instance, taking your data, we could adjust it to all go into one Oct-Sep year (ending 2020 so we capture Feb 29's).



    df2b <- df %>%
    mutate(date_hydro = lubridate::ymd(paste(
    if_else(month(Date) < 10, 2020, 2019), # 2020 is leap year
    month(Date), day(Date))
    )) %>%
    group_by(date_hydro) %>%
    summarize(multiyearmean = mean(Val))


    Then we can plot the daily averages within the hydrological year.



    ggplot(df2b, aes(date_hydro, multiyearmean)) + 
    geom_point() +
    scale_x_date(date_labels = "%b", date_breaks = "1 month",
    minor_breaks = NULL) +
    theme(axis.text.x = element_text(hjust = 0))


    enter image description here






    share|improve this answer















    EDIT: Original answer created fake data since none originally provided in OP. Now uses suggested df example data. (Thanks for adding, btw! Simplifies answering.)



    I'd suggest adjusting your dates so they are a continuous range from one Oct 1 through the next Sept 30. That way you can plot in ggplot using dates, but with the alignment you prefer.



    For instance, taking your data, we could adjust it to all go into one Oct-Sep year (ending 2020 so we capture Feb 29's).



    df2b <- df %>%
    mutate(date_hydro = lubridate::ymd(paste(
    if_else(month(Date) < 10, 2020, 2019), # 2020 is leap year
    month(Date), day(Date))
    )) %>%
    group_by(date_hydro) %>%
    summarize(multiyearmean = mean(Val))


    Then we can plot the daily averages within the hydrological year.



    ggplot(df2b, aes(date_hydro, multiyearmean)) + 
    geom_point() +
    scale_x_date(date_labels = "%b", date_breaks = "1 month",
    minor_breaks = NULL) +
    theme(axis.text.x = element_text(hjust = 0))


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 '18 at 18:08

























    answered Nov 25 '18 at 23:18









    Jon SpringJon Spring

    6,9181829




    6,9181829













    • Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

      – Clayton Glasser
      Nov 25 '18 at 23:24











    • Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

      – Jon Spring
      Nov 26 '18 at 2:57











    • Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

      – Clayton Glasser
      Nov 26 '18 at 17:44











    • Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

      – Clayton Glasser
      Nov 26 '18 at 17:51



















    • Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

      – Clayton Glasser
      Nov 25 '18 at 23:24











    • Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

      – Jon Spring
      Nov 26 '18 at 2:57











    • Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

      – Clayton Glasser
      Nov 26 '18 at 17:44











    • Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

      – Clayton Glasser
      Nov 26 '18 at 17:51

















    Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

    – Clayton Glasser
    Nov 25 '18 at 23:24





    Much appreciated, but please see my edit as I believe it indicates that this solution will not suffice.

    – Clayton Glasser
    Nov 25 '18 at 23:24













    Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

    – Jon Spring
    Nov 26 '18 at 2:57





    Updated. I think the simplest thing is to re-express each date as its equivalent within the 2019-2020 hydrological year, then average according to those adjusted dates.

    – Jon Spring
    Nov 26 '18 at 2:57













    Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

    – Clayton Glasser
    Nov 26 '18 at 17:44





    Your "multiyearmean" solution works perfectly and basically obviates the problem that prompted me to ask this question about shifting the start point of the X-axis. However, that is probably the appropriate way to deal with this issue, so I am giving you the answer.

    – Clayton Glasser
    Nov 26 '18 at 17:44













    Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

    – Clayton Glasser
    Nov 26 '18 at 17:51





    Reputation or something is preventing me from answering my other "multiyearmean" question, but your code above is the preferred solution, so I'll give you the answer over there if you post it, @JonSpring. stackoverflow.com/questions/53463146/…

    – Clayton Glasser
    Nov 26 '18 at 17:51













    1














    Somewhat similar to the other answer but using your simulation:



    set.seed(2018 - 11 - 25) # reproducible data

    data.frame(
    dates = seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by = "days"),
    values = runif(3653)
    ) -> xdf

    mutate(
    xdf,
    day = lubridate::day(dates),
    month = lubridate::month(dates)
    ) %>%
    group_by(month, day) %>%
    summarize(multi_year_mean = mean(values)) %>%
    ungroup() %>%
    mutate(plot_date = case_when( # use "real" date axis and wrap-around
    month >= 10 ~ as.Date(sprintf("2019-%02s-%02s", month, day)),
    TRUE ~ as.Date(sprintf("2020-%02s-%02s", month, day)) # account for leap year(s)
    )) %>%
    ggplot(aes(plot_date, multi_year_mean)) +
    geom_point() +
    scale_x_date(expand=c(0,0.75), date_breaks = "1 month", date_labels = "%b") # adjust aesthetics as necessary


    enter image description here






    share|improve this answer




























      1














      Somewhat similar to the other answer but using your simulation:



      set.seed(2018 - 11 - 25) # reproducible data

      data.frame(
      dates = seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by = "days"),
      values = runif(3653)
      ) -> xdf

      mutate(
      xdf,
      day = lubridate::day(dates),
      month = lubridate::month(dates)
      ) %>%
      group_by(month, day) %>%
      summarize(multi_year_mean = mean(values)) %>%
      ungroup() %>%
      mutate(plot_date = case_when( # use "real" date axis and wrap-around
      month >= 10 ~ as.Date(sprintf("2019-%02s-%02s", month, day)),
      TRUE ~ as.Date(sprintf("2020-%02s-%02s", month, day)) # account for leap year(s)
      )) %>%
      ggplot(aes(plot_date, multi_year_mean)) +
      geom_point() +
      scale_x_date(expand=c(0,0.75), date_breaks = "1 month", date_labels = "%b") # adjust aesthetics as necessary


      enter image description here






      share|improve this answer


























        1












        1








        1







        Somewhat similar to the other answer but using your simulation:



        set.seed(2018 - 11 - 25) # reproducible data

        data.frame(
        dates = seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by = "days"),
        values = runif(3653)
        ) -> xdf

        mutate(
        xdf,
        day = lubridate::day(dates),
        month = lubridate::month(dates)
        ) %>%
        group_by(month, day) %>%
        summarize(multi_year_mean = mean(values)) %>%
        ungroup() %>%
        mutate(plot_date = case_when( # use "real" date axis and wrap-around
        month >= 10 ~ as.Date(sprintf("2019-%02s-%02s", month, day)),
        TRUE ~ as.Date(sprintf("2020-%02s-%02s", month, day)) # account for leap year(s)
        )) %>%
        ggplot(aes(plot_date, multi_year_mean)) +
        geom_point() +
        scale_x_date(expand=c(0,0.75), date_breaks = "1 month", date_labels = "%b") # adjust aesthetics as necessary


        enter image description here






        share|improve this answer













        Somewhat similar to the other answer but using your simulation:



        set.seed(2018 - 11 - 25) # reproducible data

        data.frame(
        dates = seq(as.Date("2000-10-01"), as.Date("2010-10-01"), by = "days"),
        values = runif(3653)
        ) -> xdf

        mutate(
        xdf,
        day = lubridate::day(dates),
        month = lubridate::month(dates)
        ) %>%
        group_by(month, day) %>%
        summarize(multi_year_mean = mean(values)) %>%
        ungroup() %>%
        mutate(plot_date = case_when( # use "real" date axis and wrap-around
        month >= 10 ~ as.Date(sprintf("2019-%02s-%02s", month, day)),
        TRUE ~ as.Date(sprintf("2020-%02s-%02s", month, day)) # account for leap year(s)
        )) %>%
        ggplot(aes(plot_date, multi_year_mean)) +
        geom_point() +
        scale_x_date(expand=c(0,0.75), date_breaks = "1 month", date_labels = "%b") # adjust aesthetics as necessary


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 23:35









        hrbrmstrhrbrmstr

        61.4k691152




        61.4k691152






























            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%2f53472578%2fhow-to-shift-the-starting-point-of-the-x-axis-in-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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'