Drawing minor ticks (not grid ticks) in ggplot2 in a date format axis

六月ゝ 毕业季﹏ 提交于 2019-12-01 05:49:42
tonytonov

I do not advise to use annotation_logticks. Here's the intended way to do that. I removed the title since I do not have the font installed.

Edit:

I borrowed the function from here, and it works fine, provided by manual breaks construction:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!