R ggplot scale:显示休息时间和小休息时间

时间:2016-11-23 09:50:47

标签: r plot ggplot2

我目前的图表如下:

Sorry, not enough rep for embedding

我想在规模上实现的目标是这样的:

Sorry, not enough rep for embedding

所以基本上显示间隔中间的日期和日期以及那些小休息的所有单个小时。

这是一个可重现的情节示例:

vars <- c("RunDateTime", "EndRunDateTime")
dataset[vars] <- lapply(dataset[vars], as.POSIXct, format = "%Y-%m-%dT%H:%M:%S")
df <- with(dataset, data.frame(Name = JobName, Start = RunDateTime, End = EndRunDateTime, Status = Status))
library(ggplot2)
lims <- with(df, c(min(Start), max(End)))
library(dplyr)
library(lubridate)
dates <- data.frame(Date = df$Start)
minute(dates$Date) <- 0
second(dates$Date) <- 0
hour(dates$Date) <- 12
dates <- distinct(dates) %>%
  mutate(Label = paste(format(Date, "%a - %d.%m.%y")),
         DateRound = Date)
hour(dates$DateRound) <- 0

ggplot(df) +
  geom_segment(aes(x = Start, xend = End, y = Name, yend = Name, color = Status), size = 3) +
  # Add the major labels as a geom, this does limit to the plot area so have to put them above axis
geom_text(data = dates, aes(x = dates$Date, label = dates$Label, y = 0, vjust = -0.5),check_overlap = TRUE, size = 3.5) +
  # Add vertical lines to separate the days visually
geom_vline(data = dates, aes(xintercept = as.numeric(dates$DateRound)),linetype = "longdash") +
  scale_x_datetime(date_breaks = "2 hour", date_minor_breaks = "1 hour", limits = lims, labels = function(x) paste(format(x, "%H"))) +
  xlab(NULL) +
  ylab(NULL) +
  scale_colour_manual(values = c("successfull" = "#a1d99b", "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
theme_bw() +
theme(axis.text.x = element_text(size = 7))

很想听听如何获得这样的音阶的建议!

解决方案:

我选择了@Marijn Stevering解决方案并稍微重新安排了代码:

{
  "response" : "pending" 
}

所以现在我的规模看起来像这样,我可以很好地生活。

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为方面可能会帮助你实现你想要的目标。

    # Original Script. Please update your script content here and once completed             copy below section back to the original editing window #
vars <- c("RunDateTime", "EndRunDateTime")
dataset[vars] <- lapply(dataset[vars], as.POSIXct, format = "%Y-%m-%dT%H:%M:%S")
df <- with(dataset, data.frame(Name = JobName, Start = RunDateTime, End = EndRunDateTime, Status = Status))
library(ggplot2)
library(lubridate)
df <- transform(df,
                Date = as.Date(Start),
                DateStart = as.Date(Start),
                DateEnd = as.Date(End),
                HourStart = hour(Start),
                HourEnd = hour(End)) 

if(sum(df$DateStart != df$DateEnd) != 0){
  stop("At least one job runs in two different days.")
}

ggplot(df, aes(color = Status)) +
  geom_segment(aes(x = HourStart, xend = HourEnd, y = Name, yend = Name), size = 3) +
  xlab(NULL) +
  ylab(NULL) +
  scale_x_continuous(breaks = 0:24, limits = c(0,24)) +
  scale_colour_manual(values = c("successfull" = "#a1d99b",
  "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
  theme_bw()+
  facet_grid(~Date)

这使您可以将每一天分组到一列中,然后将小时作为轴上的标签。我知道你并不完全是这样绘制的,但它是我展示它的方式。

注意在一天内开始并在另一天完成的工作的处理。

我希望这会有所帮助。

答案 1 :(得分:0)

我尝试通过使轴标记小时数然后手动添加日期标签来解决它。我使用geom_text完成了这项工作,但仅限于绘图区域,因此它显示在轴上方。另外,我为geom_text创建数据的方式可能不是最佳的。情节看起来像这样: Plot

使用此代码生成:

# Fiddling with dates, there is probably a better way to do this
library(dplyr)
library(lubridate)
dates <- data.frame(Date = df$Start)
minute(dates$Date) <- 0
second(dates$Date) <- 0
hour(dates$Date) <- 12
dates <- distinct(dates) %>% 
  mutate(Label = paste(format(Date, "%a \n %d.%m.%y")),
         DateRound = Date)
hour(dates$DateRound) <- 0


ggplot(df) +
  geom_segment(aes(x = Start, xend = End, y = Name, yend = Name, color = Status), size = 3) +
  # Add the major labels as a geom, this does limit to the plot area so have to put them above axis
  geom_text(data = dates, aes(x = Date, label = Label, y = 0, vjust = -0.2)) +
  # Add vertical lines to separate the days visually
  geom_vline(data = dates, aes(xintercept = as.numeric(DateRound))) +
  scale_x_datetime(date_breaks = "1 hour", date_minor_breaks = "1 hour", limits = lims, labels = function(x) paste(format(x, "%H"))) +
  xlab(NULL) +
  ylab(NULL) +
  scale_colour_manual(values = c("successfull" = "#a1d99b", "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
  theme_bw()