0

I am very new to using r and to programming itself. My major is nutrition. However, I recently started to code in r for my research. I am having trouble in formatting the x-axis for a graph and have searched a lot of forums for answers. I am not sure if I used the right terms to get the answer. Any help or link to the answer is appreciated.

My data consists of wheel counts that has been acquired every 5 minutes for a duration of two weeks. Accordingly, I am looking to label my X-axis to contain:

  1. dates as day 1, day 2, day 3, ........ day 4 as major labels.
  2. time as hours of day between two days as minor labels. This means that between two days, I need minor ticks representing the hours of the day.
  3. I want to add a background to the graph that where every 12 hours between days is represented by a white or a gray background.

Right now, the following is the code I have used for the plot and looking for refining it based on the above needs:

p <- ggplot(data=wheel_data, aes(x=date_time, y=rev_counts, group = group, color = group)) + 
     geom_smooth(method = "loess", span = 0.001) +
     scale_x_datetime(breaks = date_breaks("1 day"), label = date_format("%m-%d-%y"))
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Vas
  • 1
  • welcome to SO! your problem sounds solvable, but without sample data, this will require more of our time and therefore we are less likely to help. A sample data frame would contain all the columns that contain information for your plot (not more). Also, don't make a huge data frame if not needed. In your example, maybe make it two days but with only a few hours. This should usually be generalisable. – tjebo Mar 13 '18 at 00:34
  • Also, may I suggest another title - see my edit – tjebo Mar 13 '18 at 00:39
  • Last, but not least, have a look at this post, maybe this is nicer than putting days as major ticks: https://stackoverflow.com/questions/44616530/axis-labels-on-two-lines-with-nested-x-variables-year-below-months – tjebo Mar 13 '18 at 00:41
  • Thanks Tjebo! The data frame contains the following columns: animal_id, wheel_id, group, date_time (this variable consists of the date and time where time is in 5 min bins), only date variable and lastly revolution counts for every 5 mins. – Vas Mar 13 '18 at 17:12
  • That's not really useful. Please have a look at this one, especially "Tip 1". https://www.r-bloggers.com/three-tips-for-posting-good-questions-to-r-help-and-stack-overflow/ – tjebo Mar 13 '18 at 17:50
  • I created a dput output, however I am not able to paste here as it is longer than what can be pasted. Do you have any suggestions? – Vas Mar 14 '18 at 21:04
  • Well done trying dput. If this is too much, create a data frame yourself. Make as many columns as you need for the question. And not too many rows. The columns should have the qualities required for your question: Most importantly, the class (integer, character, etc) should be the same. – tjebo Mar 15 '18 at 01:08
  • my_data <- structure(list(study = "tfex_c1", animal_id = 151, sex = "M", age = "adult", wheel_id = 7, group = "LD-W", date_time = structure(1516508100, class = c("POSIXct", "POSIXt"), tzone = "UTC"), date = structure(1516492800, class = c("POSIXct", "POSIXt"), tzone = "UTC"), time = structure(1516508100, class = c("POSIXct", "POSIXt"), tzone = "UTC"), bin = "1/21/2018 4:15", rev_counts = 0), .Names = c("study", "animal_id", "sex", "age", "wheel_id", "group", "date_time", "date", "time", "bin", "rev_counts"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")) – Vas Mar 16 '18 at 03:33
  • Thanks for your response! I could get only a single row for the dput output. The data represent the actual data. – Vas Mar 16 '18 at 03:34
  • One row is not a helpful sample data in this case. If your question is still active, here a bit more guidance for creating your sample data for us: If you do not want to create sample columns, then use your existing data frame. Cut this one down to the essential: Select only the columns needed for the answer and only certain rows. Subsetting your data frame is an essential skill you need. For example if you want to select rows of date 2018-01-21 and 2018-01-22, and only column 1,2,4,5, one way would be, for your example: `my_data[my_data$date %in% c("2018-01-21", "2018-01-22" ) , c(1:2, 4:5)]` – tjebo Mar 18 '18 at 15:18
  • If the days have too many datapoints , you can also create groups (I am using [tag:dplyr] for that) and `head()` by group the first n rows. – tjebo Mar 18 '18 at 15:18
  • animal_id wheel_id group date_time date rev_counts 145 1 LD-W 2018-01-29 02:05:00 2018-01-29 865 145 1 LD-W 2018-01-28 19:30:00 2018-01-28 860 145 1 LD-W 2018-01-28 23:05:00 2018-01-28 854 146 2 LD-W 2018-01-29 01:35:00 2018-01-29 870 146 2 LD-W 2018-01-28 21:35:00 2018-01-28 869 155 11 SLD-W 2018-01-29 00:20:00 2018-01-29 849 156 12 SLD-W 2018-01-28 21:10:00 2018-01-28 892 159 15 SLD-W 2018-01-28 21:00:00 2018-01-28 844 159 15 SLD-W 2018-01-28 20:20:00 2018-01-28 860 159 15 SLD-W 2018-01-28 22:30:00 2018-01-28 862 – Vas Mar 18 '18 at 20:24

0 Answers0