R 将x图例中的月份替换为年份

R 将x图例中的月份替换为年份,r,R,如何在同一绘图中接收具有多条时间线的时间线绘图,如下所示: df3 <- structure(list(id = c(6L, 6L, 2L, 3L, 6L, 7L, 3L, 5L, 7L, 4L, 6L, 7L, 7L, 3L, 6L, 6L, 6L, 7L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 7L, 6L, 6L, 7L, 6L, 7L, 6L, 3L

如何在同一绘图中接收具有多条时间线的时间线绘图,如下所示:

df3 <- structure(list(id = c(6L, 6L, 2L, 3L, 6L, 7L, 3L, 5L, 7L, 
                            4L, 6L, 7L, 7L, 3L, 6L, 6L, 6L, 7L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 
                            7L, 6L, 6L, 7L, 6L, 7L, 6L, 3L, 5L, 6L), month = c("2011-01", 
                                                                                       "2011-02", "2011-04", "2011-04", "2011-04", "2011-04", "2011-05", 
                                                                                       "2011-05", "2011-05", "2011-06", "2011-06", "2011-06", "2011-07", 
                                                                                       "2011-08", "2011-08", "2011-09", "2011-10", "2011-11", "2012-03", 
                                                                                       "2012-04", "2012-05", "2012-05", "2012-08", "2012-09", "2012-09", 
                                                                                       "2012-09", "2012-10", "2012-11", "2012-11", "2012-12", "2012-12", 
                                                                                       "2013-01", "2013-02", "2013-02", "2013-02"), frq = c(1, 
                                                                                                                                                  1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 
                                                                                                                                                  1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2)), row.names = c(NA, 35L
                                                                                                                                                  ), class = "data.frame")
library(ggplot2)
df3$id <- as.factor(df3$id)
ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()

你的意思是这样的:

df3 <- structure(list(id = c(6L, 6L, 2L, 3L, 6L, 7L, 3L, 5L, 7L, 
                            4L, 6L, 7L, 7L, 3L, 6L, 6L, 6L, 7L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 
                            7L, 6L, 6L, 7L, 6L, 7L, 6L, 3L, 5L, 6L), month = c("2011-01", 
                                                                                       "2011-02", "2011-04", "2011-04", "2011-04", "2011-04", "2011-05", 
                                                                                       "2011-05", "2011-05", "2011-06", "2011-06", "2011-06", "2011-07", 
                                                                                       "2011-08", "2011-08", "2011-09", "2011-10", "2011-11", "2012-03", 
                                                                                       "2012-04", "2012-05", "2012-05", "2012-08", "2012-09", "2012-09", 
                                                                                       "2012-09", "2012-10", "2012-11", "2012-11", "2012-12", "2012-12", 
                                                                                       "2013-01", "2013-02", "2013-02", "2013-02"), frq = c(1, 
                                                                                                                                                  1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 
                                                                                                                                                  1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2)), row.names = c(NA, 35L
                                                                                                                                                  ), class = "data.frame")
library(ggplot2)
df3$id <- as.factor(df3$id)
ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()
库(ggplot2)

用于读取前(n)个字符的df3$id函数:

firstfew <- function(x, n){
x=as.character(x)
substr(x, 1, n)}

years=firstfew(df3$month,4)
df3$years=years

ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()+ scale_x_discrete(labels= years)
ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()+theme(legend.position="top",axis.ticks.x=element_blank(),axis.title.x=element_blank(),axis.text.x=element_blank(),panel.spacing.x=unit(0, "lines"),panel.grid.major = element_blank())+facet_wrap(~years,strip.position = "bottom",scales = "free_x")

你的问题解决了吗?。。。您可能希望设置为日期格式:
df3$Date