0
votes

I'm trying to label just some breaks of the x axis. What I've done so far is the following:

data <- data.frame(time = c(13, 25, 78, 130), event = 1)
roundDown <- function(x) 10 ^ trunc(log10(x))

xSeqBreaks <- sapply(log10(roundDown(min(data$time))):log10(roundDown(max(data$time))), function(x)(seq(10 ^ x, 10 ^ (x + 1), 10 ^ x)))

xSeqLabels <- sapply(log10(roundDown(min(data$time))):log10(roundDown(max(data$time))), function(x)(seq(10 ^ x, 5 * 10 ^ x, 10 ^ x)))

        # ... calling ggplot(..)        
scale_x_log10(breaks = xSeqBreaks, labels = xSeqLabels, expand = c(.05, .1)) +
        # ... some other ggplot functions 

xSeqBreaks gives me a matrix which breaks the x axis such that between every range of a decimal power there are 10 lines. For example the minimum of my data time is 13 and the maximum time is 130 I would get breaks at c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000).

xSeqLabels generates a matrix which should label the x axis such that between every range of a decimal power only the first 5 lines are labelled. Continuing the above example I want to have the labels at c(10, 20, 30, 40, 50, 100, 200, 300, 400, 500)

Problem is that the following error message is printed to the console:

"Error: breaks and labels must have the same length"

I've tried several things like writing an own function for both, breaks and labels, but this also won't work.

It is very important that the solution to this problem is reproducible for any kind of time data. That is why this older post Only label selected breaks has not fixed my problem.

Furthermore it would be very nice if I could use the "normal" numbers for labelling and not the scientific notation (100000 instead of 1e+05 and so forth).

Best regards

Tim

1

1 Answers

0
votes

Apologies for having misunderstood your question previously. Here's what you're looking for:

xSeqBreaks <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)

//Replace the last 4 labels for every power of 10 with empty strings
xSeqLabels <- xSeqBreaks
xSeqLabels[c(rep(F,5),rep(T,4))] <- ''

Which gives:

> xSeqBreaks
[1]   10   20   30   40   50   60   70   80   90  100  200  300  400
[14]  500  600  700  800  900 1000
> xSeqLabels
[1] "10"   "20"   "30"   "40"   "50"   ""     ""     ""     ""    
[10] "100"  "200"  "300"  "400"  "500"  ""     ""     ""     ""    
[19] "1000"