使用ggplot2在x轴上的月份之间添加刻度

时间:2022-11-30 09:35:58

I'm trying to add ticks between the months on the x-axis using ggplot2 - something like this:

我正在尝试使用ggplot2在x轴上的月份之间添加刻度 - 类似这样:

使用ggplot2在x轴上的月份之间添加刻度

instead of the standard ticks at the months.

而不是几个月的标准蜱。

My process thus far has been as follows (for example):

到目前为止,我的流程如下(例如):

y=(1:1:12)
x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
df=data.frame(x,y)
g<-ggplot(df,aes(x,y))
g<-g+geom_line()
g<-g+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))

Which gives me:

这给了我:

使用ggplot2在x轴上的月份之间添加刻度

I'd like those x-axis ticks to be between the months. I've tried using

我希望那些x轴刻度在几个月之间。我试过用过

 minor_breaks=date_breaks("2 weeks")

in the scale_x_date line (along with several other variations), but the minor_breaks doesn't seem to be working for me. I've also tried variations of

在scale_x_date行(以及其他几个变体),但minor_breaks似乎不适合我。我也试过各种各样的

 myminor=seq(from=1,to=365,by=15)

and using that for minor_breaks, but that didn't work either. Finally, I tried

并将其用于minor_breaks,但这也不起作用。最后,我试过了

g<-g+theme(axis.ticks.x=element_line(myminor))

but that also proved ineffective. I feel like it's a simple fix but I'm stuck.

但这也证明是无效的。我觉得这是一个简单的修复,但我被卡住了。

1 个解决方案

#1


1  

Fairly hacky, but somewhat closer to what you seem to want:

相当hacky,但有点接近你似乎想要的:

library(ggplot2)
library(scales)
library(lubridate)
y=(1:12)
x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
df=data.frame(x,y)
g<-ggplot(df,aes(x,y))
g<-g+geom_line()

labels <- date_format("%b")(x)
breaks <- as.Date(sort(c(as.POSIXct(x), 
                         as.POSIXct(seq(as.Date("2014-01-15"), 
                                        as.Date("2014-12-31"), by="months")),
                         ymd("2015-1-1"))))
labels <- c("", 
            as.vector(rbind(labels, 
                            rep("", length(labels)))))

g + scale_x_date(labels = labels, breaks = breaks, limits=range(breaks)) + 
    theme(axis.ticks.x=element_line(colour=c("black", 
                                             rep(c(NA, "black"), t=12))),
          panel.grid.major.x=element_line(colour=c("white", 
                                                   rep(c(NA, "white"), t=12))),
          panel.grid.minor.x=element_blank())

使用ggplot2在x轴上的月份之间添加刻度

#1


1  

Fairly hacky, but somewhat closer to what you seem to want:

相当hacky,但有点接近你似乎想要的:

library(ggplot2)
library(scales)
library(lubridate)
y=(1:12)
x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
df=data.frame(x,y)
g<-ggplot(df,aes(x,y))
g<-g+geom_line()

labels <- date_format("%b")(x)
breaks <- as.Date(sort(c(as.POSIXct(x), 
                         as.POSIXct(seq(as.Date("2014-01-15"), 
                                        as.Date("2014-12-31"), by="months")),
                         ymd("2015-1-1"))))
labels <- c("", 
            as.vector(rbind(labels, 
                            rep("", length(labels)))))

g + scale_x_date(labels = labels, breaks = breaks, limits=range(breaks)) + 
    theme(axis.ticks.x=element_line(colour=c("black", 
                                             rep(c(NA, "black"), t=12))),
          panel.grid.major.x=element_line(colour=c("white", 
                                                   rep(c(NA, "white"), t=12))),
          panel.grid.minor.x=element_blank())

使用ggplot2在x轴上的月份之间添加刻度