在日期格式轴中绘制ggplot2中的次要刻度线(不是网格刻度线) [英] Drawing minor ticks (not grid ticks) in ggplot2 in a date format axis

查看:1339
本文介绍了在日期格式轴中绘制ggplot2中的次要刻度线(不是网格刻度线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以黑白方式为出版物完成一些图形,并且在轴上遇到了一些问题。



我需要绘图轴(不是网格)中的数据的子标签,我通过使用 annotation_logticks(base = 2,sides =trbl)基2,因为我的数据是不相关的日志。



但似乎不适用于日期格式类型(xts)的y轴。



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



是我用来做图的代码:

  ratio_plot_start_time =01:45
ratio_plot_end_time =02: (x =比率* 100,y =发病))+ geom_point(aes(y =发病))
p < - ggplot(onsetratios,aes (粘贴(date,ratio_plot_start_time,sep =)),as.POSIXct(paste (表达(分钟=起始误差,ymax =开始+误差),宽度= 0.0) 17)))+主题(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 
比例发病误差
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


解决方案

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



编辑:



我借用函数此处,它可以正常工作,通过手工拆分结构提供:

  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 =))
日期< - 2012-05-17
p < - ggplot(onsetratios,aes(x = ratio * 100,y = onset))+ geom_point(aes(y = onset))+
geom_errorbar(aes(ymin =发作误差,ymax =发作+误差),宽度= 0.0)+
xlab(表达式(峰值计数的百分比))+ ylab(表达式(UT Time(2012 May 17 )))+主题(方面.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())


I'm trying to get some graphics done for a publication in black and white, and I'm having some trouble with the axis.

I need to plot the subticks for the data in the axis (not the grid), and I found a trick around it by using the annotation_logticks(base=2, sides="trbl") with base 2, since my data is not relevant in log.

But that doesn't seem to work for the y axis, which is in date format type (xts).

The graph I got so far is the one bellow, all I need is to remove the background grid (that part should be easy) and to add the ticks on the y axis (that's the difficult part for me).

This is the code I used to do the graph:

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

And the data used for building the graph is:

> 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

解决方案

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

这篇关于在日期格式轴中绘制ggplot2中的次要刻度线(不是网格刻度线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆