使用 ggplot2 包标记选定的中断

Label selected breaks with ggplot2 package

我试图标记 x 轴的一些中断。到目前为止我所做的如下:

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 给了我一个矩阵,它打破了 x 轴,使得在十进制幂的每个范围之间有 10 行。例如,我的数据时间的最小值是 13,最长时间是 130,我会在 c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500 , 600, 700, 800, 900, 1000)。

xSeqLabels 生成一个矩阵,该矩阵应标记 x 轴,以便在小数幂的每个范围之间仅标记前 5 行。继续上面的例子,我希望标签位于 c(10, 20, 30, 40, 50, 100, 200, 300, 400, 500)

问题是以下错误消息被打印到控制台:

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

我已经尝试了几种方法,例如为中断和标签编写自己的函数,但这也行不通。

这个问题的解决方案对于任何类型的时间数据都是可重现的,这一点非常重要。 这就是为什么这个较旧的 post Only label selected breaks 没有解决我的问题。

此外,如果我可以使用 "normal" 数字而不是科学计数法(100000 而不是 1e+05 等等)来标记,那就太好了。

此致

蒂姆

抱歉之前误解了您的问题。这是您要查找的内容:

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))] <- ''

给出:

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