0

I am using R-3.2.4. I have been trying to plot the data with only time on x axis and some value on y axis but everytime it shows some date on along with time on x axis, however i need only time interval on x axis.

Here is the code that i am using:

data_plot<-        read.csv("C:/.../Data_pract.csv",header=TRUE,stringsAsFactors=F)
data_plot$Time<-as.POSIXct(strptime(data_plot$Time, format="%H:%M"))
mydata<-ggplot(data_plot,aes(x=Time,y=January))+geom_line()
mydata

Here is the sample data that i am using:

    Time January
    0:00    20
    0:15    30
    0:30    45
    0:45    40
    1:00    28
    1:15    15

I have attached image of graph that i am looking for.

Thanks in advance.enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
NAN
  • 109
  • 3
  • 8
  • 1
    Please provide a small example that demonstrates your problem. A lot of helpful tips on how to do that can be [found here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Mar 23 '16 at 09:00
  • Have a look at [`scale_x_date`](http://docs.ggplot2.org/current/scale_date.html). – Axeman Mar 23 '16 at 09:11
  • Possible duplicate of [Formatting dates with scale\_x\_date in ggplot2](http://stackoverflow.com/questions/10576095/formatting-dates-with-scale-x-date-in-ggplot2) – Axeman Mar 23 '16 at 09:18

1 Answers1

2

You can change the scale in ggplot to datetime by using the function scale_x_datetime

mydata<-ggplot(data_plot,aes(x=Time,y=January)) + geom_line() +
      scale_x_datetime(date_breaks = "1 hour",
                       date_labels = "%I:%M %p")
  • 1
    Thanks very much Robert Plant. It worked. The time format it showed is 24 hr, is there any option to use 12 hr time system? – NAN Mar 23 '16 at 11:19