R lubridate间隔类用于标记轴

时间:2017-10-23 09:01:16

标签: r dplyr label axis lubridate

这是我创建标签矢量的“旧”方法,用于标记图的轴。

require(lubridate)
require(dplyr)

#old
version <- "201710" 
endDate = as.Date(paste0(version,"01"), "%Y%m%d")                                         
startDateString = paste0(as.integer(substr(version, 1, 4)) - 3,substr(version, 5, 6))  
startDate = as.Date(paste0(startDateString,"01"), "%Y%m%d")                               
datehv <- format(seq(startDate, endDate, "3 months"), "%Y-%m")                            

zeitbereich = c()                                    
for(i in 1:(length(datehv)-1)) {              
  zeitbereich = c(zeitbereich, datehv[i], "", "")       
}
  zeitbereich = c(zeitbereich, datehv[length(datehv)])
labels <- substr(zeitbereich,1,7)

以下是新版本。我们的想法是使用lubridate和dplyr以获得更好的可读性和更简单的语法

#new
version2 <- "201710"  
endDate2 = ymd(paste0(version,"01"))
startDate2 = endDate2 - dyears(3)
zeitbereich2 <- interval(startDate2, endDate2)

如何从新时间间隔生成旧代码中获取向量“标签”?

a)忽略#old

标签向量中的“”点

b)在标签向量中加入空“”

2 个答案:

答案 0 :(得分:1)

我会将日期保留为日期。使用var timeperiodcss = "div.k-animation-container div.k-list-container.k-popup.k-group.k-reset div ul#selectDefaultTimePeriod li.k-item"; var elements = element.FindElements(By.CssSelector(timeperiodcss)); if (elements.Count >= 1) break; 功能添加季度信息,然后在最后更改格式。

我不会改变你的循环,从我能看到它是添加空白的最佳解决方案,说,我会评估为什么你需要这些空白,可能会更好这种方式并不需要它们。

<div class="k-animation-container" style="width: 150px; height: 206px; margin-left: -10px; padding-left: 10px; padding-right: 10px; padding-bottom: 15px; box-sizing: content-box; overflow: hidden; display: none; position: absolute; top: 1039.55px; z-index: 10002; left: 87.45px;">

<div id="selectDefaultTimePeriod-list" class="k-list-container k-popup k-group k-reset" data-role="popup" style="position: absolute; height: 200px; display: none; font-size: 13px; font-family: "Roboto",sans-serif; font-stretch: normal; font-style: normal; font-weight: 400; line-height: 18px; width: 144px; transform: translateY(204px);">
<div class="k-list-optionlabel">Select time period</div>
<div class="k-group-header" style="display:none"></div>
<div unselectable="on" style="overflow: auto; position: relative; height: 175px;">
<ul id="selectDefaultTimePeriod_listbox" class="k-list k-reset" unselectable="on" tabindex="-1" aria-hidden="true" aria-live="off" data-role="staticlist" role="listbox">
<li class="k-item" data-offset-index="0" unselectable="on" role="option" tabindex="-1">Last 30 days</li>
<li class="k-item" data-offset-index="1" unselectable="on" role="option" tabindex="-1">Last 60 days</li>
<li id="0936dfb5-4f8c-4dd4-826f-f802b6a719ff" class="k-item k-state-selected k-state-focused" data-offset-index="2" unselectable="on" role="option" tabindex="-1">Last 90 days</li>
<li class="k-item" data-offset-index="3" unselectable="on" role="option" tabindex="-1">Last month</li>
<li class="k-item" data-offset-index="4" unselectable="on" role="option" tabindex="-1">Last quarter</li>
<li class="k-item" data-offset-index="5" unselectable="on" role="option" tabindex="-1">Last 6 months</li>
<li class="k-item" data-offset-index="6" unselectable="on" role="option" tabindex="-1">Last 12 months</li>
<li class="k-item" data-offset-index="7" unselectable="on" role="option" tabindex="-1">Last 365 days</li>
<li class="k-item" data-offset-index="8" unselectable="on" role="option" tabindex="-1">Full year 2016</li>
</ul>
</div>
</div>

答案 1 :(得分:1)

所以最后我做了。

df2 <- data.frame(
  date = seq(Sys.Date(), len=100, by="1 day")[sample(100, 50)],
  price = runif(50)
)
df2 <- df2[order(df$date), ]

st2 <- ymd("19960101")
en2 <- ymd("20000201")
months2 <- seq.Date(from = st2, to = en2, by = "month")

mutate(df2, date=months2)

b <- ggplot(df, aes(x=date, y=price)) + geom_line()
b + scale_x_date(date_breaks = "2 months")

datecolumn需要是scale_x_date的日期类才能工作。但是就像这样可以将日期brekas设置为任何需要的(例如“3年”)。