I'm having problems trying to get the x-axis on my ggplot to only show specific values. The X axis is a range of times, however there are so many at the moment it is currently unreadable (see image), ideally I'd like one tick every hour or so but I just cant work out how to.
Any suggestions would be incredibly appreciated.
I've tried suggestions for similar problems and haven't had any luck (please see below for most of the codes I have tried and the errors)
This is my graph code;
ggplot(data, aes(x=time, y=binary)) +
geom_bar(stat='identity') +
facet_wrap(~id,scales="free_x", ncol=1,strip.position = "right")
and here are most of the things I have tried to fix the problem;
scale_x_continuous(breaks=c(seq(16:00,17:00,18:00)))
>Error in seq.default(16:0, 17:0, 18:0, : 'from' must be of length 1 In addition: Warning message: In seq.default(16:0, 17:0, 18:0,) : extra arguments will be disregarded
scale_x_continuous(breaks=c(seq("16:00","17:00","18:00")))
> Error in seq.default("16:00", "17:00", "18:00") : 'from' must be a finite number In addition: Warning message: In seq.default("16:00", "17:00", "18:00") : NAs
introduced by coercion
data$time <- gsub('\"', "", as.character(data$time), fixed=TRUE)
data$time <- as.Date(data$time, "%H-%M-%S")
> Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf 4: Removed 7290 rows containing missing values (position_stack).
x=data$time
as.numeric(gsub(",","",x,fixed=TRUE))
> Warning message: NAs introduced by coercion
scale_x_datetime(breaks = date_breaks("1 hour"), labels = date_format("%H:%M:%S"))
<ScaleContinuousDatetime>
Range:
Limits: 0 -- 1
Here is a sample of the original data set (as it's huge), apologies for not posting this originally. Thanks so much for the help!
> data
time id binary
1 15:49 267 2
2 13:58 269 0
3 15:51 231 0
4 16:00 263 1
5 15:51 237 2
6 15:53 236 2
7 16:00 235 2
> dput(data)
structure(list(time = structure(c(2L, 1L, 3L, 5L, 3L, 4L, 5L), .Label = c("13:58",
"15:49", "15:51", "15:53", "16:00"), class = "factor"), id = c(267L,
269L, 231L, 263L, 237L, 236L, 235L), binary = c(2L, 0L, 0L, 1L,
2L, 2L, 2L)), .Names = c("time", "id", "binary"), class = "data.frame", row.names = c(NA,
-7L))
> str(data)
'data.frame': 7 obs. of 3 variables:
$ time : Factor w/ 5 levels "13:58","15:49",..: 2 1 3 5 3 4 5
$ id : int 267 269 231 263 237 236 235
$ binary: int 2 0 0 1 2 2 2