我知道使用scale_x_date(date_labels="%b"...)
會創建包含每月前三個字母的刻度標籤。有沒有辦法只包括每個月的第一個字母?因此,而不是"Jan Feb March..."
,標籤將是"J F M..."
。使用每月的第一個字母創建ggplot2 x軸月標籤
下面是一個例子:月份標籤重疊,所以我希望能夠將標籤更改爲第一個字母。
library(lubridate)
library(ggplot2)
set.seed(2)
df = data.frame(Date=seq(as.Date("2010-01-01"),as.Date("2015-12-31p
"), by="1 day"))
df$value = cumsum(rnorm(nrow(df)))
p = ggplot(df, aes(Date, value)) +
geom_vline(xintercept=as.numeric(df$Date[yday(df$Date)==1]), colour="grey60") +
geom_line() +
scale_x_continuous(labels = year_labels <- rep(c('J','F','M','A','M','J','J','A','S','O','N','D'),5)) +
theme_bw() +
theme(panel.grid.minor.x = element_blank()) +
labs(x="")
p
我試圖改變線scale_x_date(date_labels="%b", date_breaks="month", expand=c(0,0))
到scale_x_continuous(labels = year_labels <- rep(c('J','F','M','A','M','J','J','A','S','O','N','D'),5))
但由此產生下面的錯誤
錯誤as.Date.numeric(值):「原點'必須提供
可以請你提供一個可重複的例子 –
你的例子是不可複製的 –
我只是跳過'geom_vline(xintercept = as.numeric(df $ Date [yday(df $ Date)== 1]),color =「 grey60「)',我認爲只是*裝飾的一部分*,並且它工作正常 – Marco