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

  • SEARCH
  • Home
  • 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 8629761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:53:16+00:00 2026-06-12T08:53:16+00:00

I have data that can be mimicked in the following manner: set.seed(1234) foo <-

  • 0

I have data that can be mimicked in the following manner:

set.seed(1234)

foo <- data.frame(month = rep(month.name, each = 24),
              hour = rep(seq(1:24), 12),
              value1 = rnorm(nrow(foo), 60, 1),
              value2 = rnorm(nrow(foo), 60, 1))

foo <- melt(foo, id = c('month', 'hour'))

I would like to create a plot for the entire year using ggplot that displays the 24 hour cycle of each variable per month.

Here’s what I’ve tried so far:

t.plot <- ggplot(foo,
             aes(interaction(month,hour), value, group = interaction(variable,hour)))

t.plot <- t.plot + geom_line(aes(colour = variable))
print(t.plot)

I get this, which throws the data into misalignment. For such a small SD you see that the first 24 values should be nearer to 60, but they are all over the place. I don’t understand what’s causing this discrepancy.

https://www.dropbox.com/s/rv6uxhe7wk7q35w/foo.png

when I plot:

plot(interaction(foo$month,foo$hour)[1:24], foo$value[1:24])

I get the shape that I would expect however the xaxis is very strange and not what I was expecting.

Any help?

  • 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-12T08:53:17+00:00Added an answer on June 12, 2026 at 8:53 am

    The solution is to set your dates to be dates (not an interaction of a factor)

    eg

    library(lubridate)
    library(reshape2)
    Date <- as.Date(dmy('01-01-2000') + seq_len(24*365)*hours(1))
    foo <- data.frame(Date = Date, 
      value1 = arima.sim(list(order = c(1,1,0), ar = 0.7), n = 24*365-1), 
       value2 = arima.sim(list(order = c(1,1,0), ar = 0.7), n = 24*365-1))
    foo_melt <- melt(foo, id = 'Date')
    
    # then you can use `scale_x_date` and `r` and ggplot2 will know they are dates
    # load scales library to access date_format and date_breaks
    library(scales) 
    ggplot(foo_melt, aes(x=Date, y=value, colour = variable)) + 
     geom_line() +
     scale_x_date(breaks = date_breaks('month'), 
                  labels = date_format('%b'), expand =c(0,0))
    

    enter image description here

    Edit 1 average day per month

    you can use facet_wrap to facet by month

    # using your created foo data set
    levels(foo$month) <- sort(month.abb)
    foo$month <- factor(foo$month, levels = month.abb)
    ggplot(foo, aes(x = hour, y=value, colour = variable)) + 
     facet_wrap(~month) + geom_line() + 
     scale_x_continuous(expand = c(0,0)))
    

    enter image description here

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

Sidebar

Related Questions

I have the following classes: Defect - represents a type of data that can
I have a WCF service that can return large amount of data depending on
I have a defined number of servers that can locally process data in their
Okay, so I want to have a generic method that can read data from
On a Java portal you can have portlets that include data provided by other
I have a table that contains all the data about users . Users can
I have a stored procedure that logs some data, how can I call this
I have data that looks like this: foo 78 xxx bar yyy qux 99
I have to sort a large amount of data that can not fit in
I have data in R that can look like this: USDZAR Curncy R157 Govt

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.