Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:17:25+00:00 2026-06-19T00:17:25+00:00

Is it possible to use R to add the date onto a ggplot graph

  • 0

Is it possible to use R to add the date onto a ggplot graph (upon which the graph data is based)? I have code which selects the most daily data over a the course of a week, and this subsequently gets plotted. An example dataframe (from a previous answer) is shown below:

set.seed(1234)
df <- data.frame(Date=seq(as.POSIXct("05:00", format="%H:%M"), 
                          as.POSIXct("23:00", format="%H:%M"), by="hours"))
df$Counts <- sample(19)
df <- df[-c(4,7,17,18),]

# generate the groups automatically and plot
idx <- c(1, diff(df$Date))
i2 <- c(1,which(idx != 1), nrow(df)+1)
df$grp <- rep(1:length(diff(i2)), diff(i2))
g <- ggplot(df, aes(x=Date, y=Counts)) + geom_line(aes(group = grp)) + 
  geom_point()
g

I know I can use the following code to get R to work out which day of the week my data is:

d <- as.Date("19/02/2013","%d/%m/%Y") 
format(d, "%A, %b %d, %Y")

But I obviously have to manually input the date for this to work.

Is it possible to get R to read the dataframe and work out the day of the week the data is based on (it will be only one day), and then add this as a legend in the top right hand corner of the graph? Ideally the format would include both the day and date (e.g. Tuesday, Feb 19th 2013).

Edit

I want to add the date as text in the graph. Can I use geom_text for this? This isn’t working at the moment, but so far I have got:

g + geom_text(aes(x=as.POSIXct("18:00:00", format="%H:%M:%S"), y=1500), label=date_format("%A, %b %d, %Y")", size=2)

Can anyone advise me what I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:17:26+00:00Added an answer on June 19, 2026 at 12:17 am

    As suggested by Arun(see comment) you can you can format your input data or add a formatting to the current plot :

     library(scales)
     g+ scale_x_datetime(labels = date_format("%A, %b %d, %Y"))
    

    EDIT

    I guess you want to annotate your graph adding a date indicator

    Since you will add a single date , you can use annotate, It is better than using geom_text which will overlap many dates. For example:

     label <- unique(format(as.Date(df$Date, "%d/%m/%Y"),
                                    "%A, %b %d, %Y"))
     g+ annotate("text", x = mean(df$Date), y = 20, 
           label = label,size=20, col='red')
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: joda time - add weekdays to date How can I use the
Possible Duplicate: What date/time functions to use for PHP 5.2 I want to add
Using JavaScript, is it possible to use prototype to add a method to a
I need a conversion from a date and add +3 days. When I use
Possible Duplicate: Cannot add an entity with a key that is already in use
Is it possible to add validations/restrictions to date element using HTML api,without jquery? I
In our application we have two use cases in which we want to avoid
Is it possible to add a CSS class from DataAnnotations (metadata) when I use
Is it possible to have one controller use another? For example: This HTML document
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.