本文介绍了时间轴上滴答的子集的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 问题 我想格式化我的X轴(时间),以便周末清晰可见。我想显示日期和星期几。 现状 我这样做(完整代码在下面) scale_x_date(breaks = myData $ timestamp, labels = paste($ b $ substr(format(myData $ timestamp,%a),1,1),格式(myData $ timestamp,%d), sep = \\\)) 这给了我 想要的情况 我宁愿在平日里有一个字母缩写, d喜欢用星期日(和假期真的)为红色。这就是我的意思(用GIMP制作)。请注意第一个星期一和上个星期五是如何使用 scale_x_date(breaks =1 day, minor_breaks =1天, labels = date_format(%a\\\%d), name =) 这里是这个例子的完整代码 $ p $ library(ggplot2) library(scales) library(reshape2) minimumTime< - as.Date(2014-07-01) maximumTime< - as.Date(2014-07-31) x< - seq(minimumTime,maximumTime,by =1 day) y1 y2 $ b $ myData< - data.frame(timestamp = x,y1 = y1,y2 = y2) myData< - melt(myData,id.vars =timestamp) rects< - data.frame(saturdays = myData [weekdays(myData $ times tamp)==Saturday,timestamp] - 0.5,sundays = myData [weekdays(myData $ timestamp)==Saturday,timestamp] + 1.5) myPlot< - ggplot()+ geom_rect(data = rects,aes(xmin = saturdays,xmax = sundays,ymin = -Inf,ymax = Inf),alpha = 0.1)+ geom_line(data = myData,aes (x = timestamp,y = value,color = variable,size = 1))+ geom_point(data = myData,aes(x = timestamp,y = value,color = variable,size = 2))+ $ (格式(myData $ timestamp,%a),1,1),格式(myData $ timestamp,%d),sep =\\ b $ b scale_x_date(breaks = myData $ timestamp, \\ n))+ #scale_x_date(breaks =1 day,minor_breaks =1 days,labels = date_format(%a\\\%d),name =)+ scale_size_continuous(range = c(1.5,5),guide = FALSE) 总结: 有没有办法给另一种颜色的特定中断着色? 手动更改标签的方式是星期一还是星期五,还有星期五是开始还是结束? 另外,如果有办法让每个标签的行都居中,那就是 awesome:) 谢谢! 解决方案您可以使用自定义formater对于也使用 breaks =1 day参数的标签,您只需在后使用函数(x) c $ c> labels = 和用 x 替换myDate $ timestamp 。这也将解决第三个问题。 $ b $ pre $ + scale_x_date(breaks =1 day, labels = function( x)paste(substr(format(x,%a),1,1),format(x,%d),sep =\\\)) 或者您可以将您的转换作为单独函数使用,然后将其用于 labels = 。 my_date_trans paste(substr(format(x,%a),1 ,1),format(x,%d),sep =\\\)} + scale_x_date(breaks =1 day,labels = my_date_trans) 要更改标签的颜色,您应该使用 theme()和 axis.text.x = 。在这里,我使用包含6次黑色和红色的颜色矢量,因为您的比例从星期一开始。然后重复这些颜色。 ggplot()+ geom_rect(data = rects,aes(xmin = saturdays, xmax = sundays,ymin = -Inf,ymax = Inf),alpha = 0.1)+ geom_line(data = myData,aes(x = timestamp,y = value,color = variable,size = 1))+ geom_point(data = myData,aes(x = timestamp,y = value,color = variable,size = 2))+ scale_x_date(breaks =1 day,labels = my_date_trans)+ scale_size_continuous(range = c(1.5,5),guide = FALSE)+ theme(axis.text.x = element_text(color = c(rep(black,6),red)))) ProblemI would like to format my X-axis (time) so that the weekends are clearly visible. I would like to display the date as well as the day of the week.Current situationI do this with (full code below)scale_x_date(breaks=myData$timestamp, labels=paste( substr(format(myData$timestamp, "%a"),1,1), format(myData$timestamp, "%d"), sep="\n"))which gives meWanted situationI would rather have a one letter abbreviation for the weekdays since it became a bit tight there.. Also, I'd like to color sundays (and holidays really) in red. Here's what I mean (made with GIMP). Note how the first Monday and last Friday was added by usingscale_x_date(breaks = "1 day", minor_breaks = "1 days", labels = date_format("%a\n%d"), name="")However, then I get a three letter abbreviation of the weekdays, which I removed in GIMP.Here is the complete code for this example.library(ggplot2)library(scales)library(reshape2)minimumTime <- as.Date("2014-07-01")maximumTime <- as.Date("2014-07-31")x <- seq(minimumTime,maximumTime, by="1 day")y1 <- sin(as.numeric(x)/3)y2 <- cos(as.numeric(x)/3)myData <- data.frame(timestamp=x, y1=y1, y2=y2)myData <- melt(myData, id.vars="timestamp")rects <- data.frame(saturdays=myData[weekdays(myData$timestamp) == "Saturday","timestamp"]-0.5, sundays = myData[weekdays(myData$timestamp) == "Saturday","timestamp"]+1.5)myPlot <- ggplot() + geom_rect(data=rects, aes(xmin=saturdays, xmax=sundays,ymin=-Inf, ymax=Inf), alpha=0.1) + geom_line(data=myData, aes(x=timestamp, y=value, colour=variable,size=1)) + geom_point(data=myData, aes(x=timestamp, y=value, colour=variable,size=2)) + scale_x_date(breaks=myData$timestamp, labels=paste(substr(format(myData$timestamp, "%a"),1,1),format(myData$timestamp, "%d"),sep="\n")) + #scale_x_date(breaks = "1 day", minor_breaks = "1 days", labels = date_format("%a\n%d"), name="") + scale_size_continuous(range = c(1.5,5), guide=FALSE)So to sum up:Is there a way to color specific breaks in another color?Is there a way change to the labels manually and still have them for the Monday andthe Friday at the beginning and the end in this case?Also, if there's a way to have the lines of each label centered, that would beawesome :)Thank you! 解决方案 You can use your custom formater for labels also using breaks="1 day" argument, you just have to use function(x) after labels= and replace myDate$timestamp with x. This will also solve the third problem. + scale_x_date(breaks="1 day", labels= function(x) paste(substr(format(x, "%a"),1,1),format(x, "%d"),sep="\n"))Or you can make your transformation as seperate function and then use it for labels=.my_date_trans<-function(x) { paste(substr(format(x, "%a"),1,1),format(x, "%d"),sep="\n")}+ scale_x_date(breaks="1 day",labels=my_date_trans)To change colors for labels you should use theme() and axis.text.x=. Here I using vector of colors that contains 6 time black and then red as your scale starts with Monday. Those colors are then repeated.ggplot() + geom_rect(data=rects, aes(xmin=saturdays, xmax=sundays,ymin=-Inf, ymax=Inf), alpha=0.1) + geom_line(data=myData, aes(x=timestamp, y=value, colour=variable,size=1)) + geom_point(data=myData, aes(x=timestamp, y=value, colour=variable,size=2)) + scale_x_date(breaks="1 day",labels=my_date_trans)+ scale_size_continuous(range = c(1.5,5), guide=FALSE)+ theme(axis.text.x=element_text(color=c(rep("black",6),"red"))) 这篇关于时间轴上滴答的子集的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 03:03