代码之家  ›  专栏  ›  技术社区  ›  Lmm

带刻度线的轴刻度线

  •  0
  • Lmm  · 技术社区  · 6 年前

    所以我有一个图,我希望x轴显示从2016年6月16日到2016年8月4日的具体日期,并在x轴上标记周日期。到目前为止,我已经设法做到了——不过,我也希望每天都有空白的勾号,以及周标签——但我不确定我是否可以应用多个scale_x_x_x date(breaks=)条件。

    如有任何关于如何添加额外勾号的帮助,我们将不胜感激!

    要使用的虚拟数据集:

    library(ggplot2)
    library(reshape2)
    
    #some data
    df <- structure(list(Date = structure(c(16968, 16969, 16970, 16971, 
    16972, 16973, 16974, 16975, 16976, 16977, 16978, 16979, 16980, 
    16981, 16982, 16983, 16984, 16985, 16986, 16987, 16988, 16989, 
    16990, 16991, 16992, 16993, 16994, 16995, 16996, 16997, 16998, 
    16999, 17000, 17001, 17002, 17003, 17004, 17005, 17006, 17007, 
    17008, 17009, 17010, 17011, 17012, 17013, 17014, 17015, 17016
    ), class = "Date"), Tc = c("0.0964", "0.0780", "0.1265", "0.1503", 
    "0.1548", "0.1028", "0.1112", "0.1283", "0.0956", "0.0847", "0.0785", 
    "0.0859", "0.0879", "0.1203", "0.1677", "0.2174", "", "", "0.1496", 
    "0.1080", "0.1101", "0.1289", "0.0942", "0.0835", "0.0851", "0.0881", 
    "0.1216", "0.0766", "0.0744", "0.0626", "", "0.1116", "", "0.0862", 
    "", "0.1210", "", "", "0.1074", "", "0.1527", "", "0.1513", "", 
    "0.1246", "", "0.1415", "", "0.0827")), .Names = c("Date", "Tc"
    ), class = "data.frame", row.names = 3:51)
    
    
    # melt data frame
    df <- melt(df, id.vars = c("Date"))
    
    #basic plot
    plot1 <-  ggplot(df[!is.na(df$value), ], 
                aes(x=Date, y=value, color=variable, group = variable,shape 
    = variable, linetype = variable, fill = variable))
    
    # points
    plot1 <- plot1 + geom_line(lwd =3)+geom_point(size=17, stroke =2) 
    
    break.vec <- c(as.Date("2016-06-16"),
               seq(from=as.Date("2016-06-16"), to=as.Date("2016-08-04"), 
    by="week"))
    plot1 <- plot1 + scale_x_date(breaks = break.vec, date_labels = "%d-%m", limits=range(break.vec))
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Lmm    6 年前

    好的,谢谢你的输入!这就是我从评论中得到的结论,虽然我相信有一种更简洁的方法可以做到这一点——但它确实起到了作用!

     break.vec <- seq(from=as.Date("2016-06-16"), to=as.Date("2016-08-04"), 
     by="day")
     plot1 <- plot1 + scale_x_date(breaks = break.vec, 
                                labels=c("16-06","","","","","","","23-06","","","","","","", "30-06",
                                         "","","","","","", "07-07", "","","","","","", "14-07",
                                         "","","","","","","21-07", "","","","","","", "28-07", 
                                         "","","","","","", "04-08"),expand = c(0.05,0))