0

I have the following R dataframe with column Date and column Value, column Date is R chron objects (format <- "m/d/y h:m:s").

What I want to do is to visualize the time series data by ploting histogram or line chart, and I also want to specify the date-time range on x-axis (e.g. from 10/2/16 20:00:00 to 10/3/16 20:00:00). However after searching for the online guides I still do not have a clue.

Would anyone give me a sample code using ggplot or other package? I appreciate your help.

Date                 Value
(10/03/16 09:31:00)  180,912.00
(10/03/16 09:32:00)  112,359.00
(10/03/16 09:35:00)   93,539.00
(10/03/16 09:35:00)  156,283.00
(10/03/16 09:36:00)  226,704.00
(10/03/16 09:37:00)  780,094.00
(10/03/16 09:45:00)  184,632.00
(10/03/16 09:48:00)  144,984.00
(10/03/16 09:49:00)  311,035.00
(10/03/16 09:51:00)  210,653.00
(10/03/16 09:51:00)   72,626.00
(10/03/16 09:52:00)  241,173.00
(10/03/16 09:54:00)  233,416.00
(10/03/16 09:55:00)  146,550.00
(10/03/16 10:24:00)  331,191.00
(10/03/16 10:28:00)  107,015.00
(10/03/16 10:36:00)  196,162.00
(10/03/16 10:41:00)  466,879.00
(10/03/16 10:44:00)  294,589.00
(10/03/16 10:48:00)  164,339.00
(10/03/16 10:52:00)  137,082.00
(10/03/16 10:58:00)  180,667.00
(10/03/16 11:04:00)  259,475.00

Updated on 11/28/2016 6:35 pm With the help of yeedle's codes, I am able to plot the following:

ggplot(temp_merge_sub1, aes(x=Date, y=Value, group=1)) + geom_bar(stat = "identity")

GGPlot2

What I really want to improve from that is:

  • how to correctly set the breaks on y-axis?
  • how to set breaks on x-axis and display the histogram on the added up value in each of the 2-hour intervals?

Thank you for your help! I appreciate it!

4
  • can you post the dput output of your df? Commented Nov 28, 2016 at 16:27
  • You have multiple values for the same date-time. How do you want that to be displayed on the plot? Commented Nov 28, 2016 at 16:50
  • Thank you @yeedle for helping with my question. The data above is only a small portion of my data, and what I would like to show with histogram is kind of adding up the value within certain fixed time ranges (e.g. 2 hour breaks, 10/3/2016 6:00 - 8:00, 8:00-10:00, ... etc) and then how the histogram of added up value over each of the time intervals. I am not sure if ggplot2 could do that? Commented Nov 28, 2016 at 23:21
  • To do that you would can add a new column to your dataframe with the rounded time range (you can use the round_date function from lubridate), and then plot using the new column. Setting breaks and limits is done with scale_x_datetime. Commented Nov 29, 2016 at 0:02

1 Answer 1

1

I converted Date to POSIXct objects, using lubridate's ymd_hms function.

library(ggplot2)
ggplot(df, aes(x=Date, y=Value)) + 
  geom_bar(stat="identity") + 
  scale_x_datetime(limits =c(mdy_hms("10/2/16 20:00:00"),mdy_hms("10/3/16 20:00:00")))

bar plot with 1-day scale

You get a clearer picture without the scale_x_datetime limits:

bar plot with no scale limits

Simply replace geom_bar with geom_line for a line graph:

ggplot(df, aes(x=Date, y=Value)) + 
  geom_line()

line plot

Sign up to request clarification or add additional context in comments.

3 Comments

Or use geom_col instead.
It's new yes. It's equivalent to geom_bar(stat = "identity").
Thanks, I did not know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.