使用scale_x_break的轴刻度:更多刻度,只有一些标记

时间:2018-06-04 21:07:01

标签: r date ggplot2 axis

所以我有一个情节,我希望x轴显示从16-06-2016到04-08-2016的特定日期,其中周日标记在x轴上。到目前为止,我已经设法做到了 - 但是,我还希望每天都有空白刻度标记,以及周标签 - 但我不确定我是否可以应用多个scale_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 个答案:

答案 0 :(得分:1)

好的,谢谢你的输入!这是我从评论中得到的内容,虽然我确信可能有更简洁的方法来做到这一点 - 但它确实起了作用!

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