【问题标题】:Drawing minor ticks (not grid ticks) in ggplot2 in a date format axis在 ggplot2 中以日期格式轴绘制次要刻度(不是网格刻度)
【发布时间】:2014-08-12 15:26:30
【问题描述】:

我正在尝试为黑白出版物制作一些图形,但我在轴上遇到了一些问题。

我需要在轴(而不是网格)中绘制数据的子刻度,并且我发现了一个技巧,即使用基数为 2 的 annotation_logticks(base=2, sides="trbl"),因为我的数据在日志中不相关。

但这似乎不适用于 y 轴,它是日期格式类型 (xts)。

到目前为止,我得到的图表如下所示,我只需要删除背景网格(这部分应该很容易)并在 y 轴上添加刻度(这对我来说是困难的部分)。

这是我用来绘制图表的代码:

ratio_plot_start_time = "01:45"
ratio_plot_end_time = "02:10"
channelNo = 1
p <- ggplot(onsetratios, aes(x=ratio*100, y=onset)) + geom_point(aes(y=onset))
p <- p + ylim(as.POSIXct(paste(date, ratio_plot_start_time, sep=" ")), as.POSIXct(paste(date, ratio_plot_end_time, sep=" ")))
p <- p + geom_errorbar(aes(ymin=onset-error, ymax=onset+error), width=0.0)
p <- p + xlab(expression("percent of peak counts"))+ ylab(expression("UT Time (2012 May 17)")) + theme(aspect.ratio = 2/(1+sqrt(5)))
p <- p + theme_bw(base_size = 14, base_family = "Helvetica") + annotate("text",x=max(onsetratios$ratio),y=as.POSIXct(paste(date, ratio_plot_end_time, sep=" ")),vjust=4,hjust=2,label=paste("F", channelNo, "'"))
p <- p + annotation_logticks(base=2, sides="trbl")

而用于构建图表的数据是:

> onsetratios
        ratio               onset    error
1  0.01092106 2012-05-17 01:49:03 19.05407
2  0.02092106 2012-05-17 01:49:35 14.89534
3  0.03092106 2012-05-17 01:51:01 12.82914
4  0.04092106 2012-05-17 01:50:22 21.45099
5  0.05092106 2012-05-17 01:50:22 21.45099
6  0.06092106 2012-05-17 01:51:13 20.09877
7  0.07092106 2012-05-17 01:51:13 20.09877
8  0.08092106 2012-05-17 01:51:13 20.09877
9  0.09092106 2012-05-17 01:51:13 20.09877
10 0.10092106 2012-05-17 01:52:34 15.53187
11 0.11092106 2012-05-17 01:53:53 14.79303
12 0.12092106 2012-05-17 01:53:53 14.79303
13 0.13092106 2012-05-17 01:53:32 17.12521
14 0.14092106 2012-05-17 01:53:32 17.12521
15 0.15092106 2012-05-17 01:53:32 17.12521
16 0.16092106 2012-05-17 01:53:32 17.12521
17 0.17092106 2012-05-17 01:46:30 36.96600
18 0.18092106 2012-05-17 01:46:30 36.96600
19 0.19092106 2012-05-17 01:46:30 36.96600

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    我不建议使用annotation_logticks。这是这样做的预期方法。因为我没有安装字体,所以我删除了标题。

    编辑:

    我从here借用了这个功能,它工作正常,由手动中断构造提供:

    onsetratios <- read.table("clipboard", head=T)
    onsetratios$onset <- as.POSIXct(onsetratios$onset)
    ratio_plot_start_time = "01:45"
    ratio_plot_end_time = "02:10"
    channelNo = 1
    start_date <- as.POSIXct(paste(date, ratio_plot_start_time, sep=" "))
    end_date <- as.POSIXct(paste(date, ratio_plot_end_time, sep=" "))
    date <- "2012-05-17"
    p <- ggplot(onsetratios, aes(x=ratio*100, y=onset)) + geom_point(aes(y=onset)) +
      geom_errorbar(aes(ymin=onset-error, ymax=onset+error), width=0.0) +
      xlab(expression("percent of peak counts"))+ ylab(expression("UT Time (2012 May 17)")) + theme(aspect.ratio = 2/(1+sqrt(5))) + 
      theme_bw(base_size = 14) + 
      annotate("text",x=max(onsetratios$ratio),y=as.POSIXct(paste(date, ratio_plot_end_time, sep=" ")),vjust=4,hjust=2,label=paste("F", channelNo, "'")) +
      ylim(c(start_date, end_date))
    
    insert_minor <- function(major_labs, n_minor) {labs <- 
                                                     c( sapply( major_labs, function(x) c(x, rep("", 4) ) ) )
                                                   labs[1:(length(labs)-n_minor)]}
    
    date_br1 <- seq(from = start_date, to = end_date, by = "1 min")
    date_br5 <- seq(from = start_date, to = end_date, by = "5 min")
    p + scale_x_continuous(breaks = 0:20, labels = insert_minor(seq(0, 20, by=5), 4)) + 
        scale_y_datetime(breaks = date_br1, labels = insert_minor(format(date_br5, "%H:%M"), 4)) + 
      theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())
    

    【讨论】:

    • 您好,谢谢。但这并不是我真正需要的。我只想要每 5 分钟一次的主要滴答声,每 5 次用标签中的数字计数,然后每分钟和每 1 次计数只需要没有标签中数字的次要滴答声。所以,就像我在 X(计数)轴的图中一样,但是一种将其应用于 Y(时间)轴的方法。
    • 从我之后的研究来看,我认为这在 ggplot2 中是不可能的(也许在正常的 R 图中?),但我看到了一种解决方法,可以为小刻度画线段,但不幸的是有一个从 2013 年初开始,ggplot2 中仍未解决的错误在您尝试在时间轴上使用时失败:github.com/hadley/ggplot2/issues/859
    • @jbssm 抱歉。我希望编辑是您所需要的。
    猜你喜欢
    • 2015-10-26
    • 1970-01-01
    • 2013-01-04
    • 2015-03-10
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2020-06-06
    相关资源
    最近更新 更多