2

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")

this is what the graph currently looks like

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
j.harv3y
  • 171
  • 1
  • 4
  • 3
    The most helpful thing you can share in a question is actual data, via `dput(data)` or at least the actual structure of your data via `str(data)`. (or the equivalent on a small portion of your data) – joran May 30 '19 at 21:38
  • Following joran's comment, you can find some insight about what's needed in a question so we can reproduce your problem and provide you with an answer can be found here [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- May 30 '19 at 22:00
  • What are you trying to do with `breaks=c(seq(16:00,17:00,18:00))`? Those will get read as incorrectly formatted numbers. Generally `seq` takes from, to, and by arguments. – camille May 31 '19 at 00:19

1 Answers1

0

Ok so without having the data to run, I can see a few problems which will hopefully help:

scale_x_continuous(breaks=c(seq(16:00,17:00,18:00)))

is throwing the same error both times as seq needs three parameters seq(initial value, final value, step), whereas you currently have it as seq(time 1, time 2, time 3).

Looking at the other ggplot call:

scale_x_datetime(breaks = date_breaks("1 hour"), labels = date_format("%H:%M:%S"))

Should read

scale_x_datetime(date_breaks="1 hour", date_labels("%H:%M:%S"))

The latter option is preferable given that you are dealing with a time/date variable.

The key then, is to make sure the variable is in the correct format. This is where we would need to see an example of that column to advise on that! It may be okay already - ggplot does that with date formats I have found, so that may be an indicator it is in the right format already.

If you add an example of the data in the column (following @joran 's comment) then we could progress.

JMilner
  • 491
  • 4
  • 12
  • Thanks everyone for the advice, apologies it's my first time posting here so it slipped my mind to actually post the data! Have now edited this into the original question. @JMilner, thanks so much for the suggestions, will give this a go! – j.harv3y Jun 05 '19 at 12:47