I want to plot occupancy rates for a particular parking garage on a particular day in a line chart making use of ggplot.
My dataframe looks as follows:
head(ParkingSub4)
FreeSpaceShort ShortCapacity DateTime OccupancyRateShort Date Weekday WeekNumber Week
310801 257 373 2017-02-25 00:04:41 0.3109920 2017-02-25 Saturday 08 FALSE
310843 260 373 2017-02-25 00:09:41 0.3029491 2017-02-25 Saturday 08 FALSE
310885 261 373 2017-02-25 00:14:41 0.3002681 2017-02-25 Saturday 08 FALSE
310927 260 373 2017-02-25 00:19:41 0.3029491 2017-02-25 Saturday 08 FALSE
310969 260 373 2017-02-25 00:24:41 0.3029491 2017-02-25 Saturday 08 FALSE
311011 263 373 2017-02-25 00:29:41 0.2949062 2017-02-25 Saturday 08 FALSE
class(ParkingSub4$DateTime)
[1] "POSIXlt" "POSIXt"
When I try to plot an overview of a particular day, let's say 23rd of February, 2017 I use the following code:
ggplot(data = ParkingSub4,
aes(x=DateTime, y=OccupancyRateShort)) + geom_line(size = 1.25) + facet_wrap(~Weekday) +
scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours")) +
theme_linedraw()
Then what I get is the following plot:
As you can see, the plot starts and ends at 23:02. I want the plot to start at 00:00:00 and end at 23:59:59 on that particular day. How can I do that?
Many thanks in advance!
EDIT/UPDATE: Adding the following does lead to the x-axis starting and ending with 00:00:
ggplot(data = ParkingSub4,
aes(x=DateTime, y=OccupancyRateShort)) + geom_line(size = 1.25) + facet_wrap(~Weekday) +
scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours"), expand=c(0,0)) +
xlim(c(as.POSIXct('2017-02-23 00:00:00', format = "%Y-%m-%d %H:%M:%S"),
as.POSIXct('2017-02-24 00:00:00', format = "%Y-%m-%d %H:%M:%S"))) +
theme_linedraw()
Only thing is I get the following message after executing: 'Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.'
This means that scale_x_datetime(labels=date_format("%H:%m"), breaks = date_breaks("2 hours")
is overwritten by:
xlim(c(as.POSIXct('2017-02-23 00:00:00', format = "%Y-%m-%d %H:%M:%S"), as.POSIXct('2017-02-24 00:00:00', format = "%Y-%m-%d %H:%M:%S"))
.
That's also not what I want, as now the breaks are set at 6 hours instead of 2 hours, and I can also not specify what information ("%H:%m") is set on the labels.